QT(7)—— 事件系统之窗口系统事件 Unix 操作系统、XCB平台、Glib函数库

本文深入探讨QT事件系统,重点关注窗口系统事件在Unix操作系统上的处理,特别是XCB平台和Glib函数库的运用。从QApplication开始,详细讲解事件分发器的创建,包括QCoreApplicationPrivate和QGuiApplicationPrivate的初始化,以及QPAEventDispatcherGlib如何处理用户自定义和窗口系统事件。最后,阐述了事件如何从windowSystemEventsQueued队列传递到QGuiApplication的processWindowSystemEvent函数,进而转化为Qt内部的Spontaneous QEvent。
摘要由CSDN通过智能技术生成

理解的关键节点是windowSystemEventsQueued的添加与减少

static gboolean userEventSourcePrepare(GSource *s, gint *timeout)
{
   
    return QWindowSystemInterface::windowSystemEventsQueued() > 0;
}

创建事件分发器 从QApplication开始追踪


class Q_GUI_EXPORT QGuiApplication : public QCoreApplication
{
   
    QGuiApplication(int &argc, char **argv, int = ApplicationFlags);
 }

QGuiApplication::QGuiApplication(int &argc, char **argv, int flags)
    : QCoreApplication(*new QGuiApplicationPrivate(argc, argv, flags))
{
   
    d_func()->init(); //私有函数  Q_DECLARE_PRIVATE(QGuiApplication)   Class##Private  QGuiApplicationPrivate
    
//QAbstractEventDispatcher *QCoreApplicationPrivate::eventDispatcher = 0;

//这里是给QCoreApplicationPrivate 的 eventDispatcher 进行初始化
    QCoreApplicationPrivate::eventDispatcher->startingUp();  //应该在这里发现蛛丝马迹的 追踪 QCoreApplicationPrivate啊 傻了
}

初始化的时候创建事件分发器



void QGuiApplicationPrivate::init()
{
   
    QCoreApplicationPrivate::init();
}

//这里是给QCoreApplicationPrivate 的 eventDispatcher 进行初始化
void QCoreApplicationPrivate::init()
{
   
    // use the event dispatcher created by the app programmer (if any)
    if (!eventDispatcher)
        eventDispatcher = threadData->eventDispatcher.load();
    // otherwise we create one
    if (!eventDispatcher)
        createEventDispatcher();
}

void QCoreApplicationPrivate::createEventDispatcher()
{
   
//这里是给QCoreApplicationPrivate 的 eventDispatcher 进行初始化
  if (qEnvironmentVariableIsEmpty("QT_NO_GLIB") && QEventDispatcherGlib::versionSupported())
        eventDispatcher = new QEventDispatcherGlib(q);
    else
        eventDispatcher = new QEventDispatcherUNIX(q);
}

这个是Widget中的创建事件分发器,是界面 从平台集成器创建

QApplication::QApplication(int &argc, char **argv, int _internal)
    : QGuiApplication(*new QApplicationPrivate(argc, argv, _internal))
{
   
    Q_D(QApplication);
    d->init(); //在这就有事件分发器了
}

既然QCoreApplicationPrivate 的 eventDispatcher 初始化了,为什么还要给 QGuiApplicationPrivate 的 eventDispatcher 进行初始化

void QApplicationPrivate::createEventDispatcher()
{
   
    QGuiApplicationPrivate::createEventDispatcher();
}

//这里是给QGuiApplicationPrivate 的 eventDispatcher 进行初始化
void QGuiApplicationPrivate::createEventDispatcher()
{
   
    Q_ASSERT(!eventDispatcher);

    if (platform_integration == 0)
        createPlatformIntegration();

    // The platform integration should not mess with the event dispatcher
    Q_ASSERT(!eventDispatcher);

    eventDispatcher = platform_integration->createEventDispatcher();
}

GUI应用私有类 创建一个窗口系统平台相关的集成管理器

窗口系统平台相关的集成管理器中用来创建事件分发器

void QGuiApplicationPrivate::createPlatformIntegration()
{
   
 		// Load the platform integration
   	QString platformPluginPath = QLatin1String(qgetenv("QT_QPA_PLATFORM_PLUGIN_PATH"));

   	const bool isXcb = platformName == "xcb";
        
      
    init_platform(QLatin1String(platformName),/*key*/ platformPluginPath, platformThemeName, argc, argv);

}

初始化平台 是一个纯粹的函数

函数中对一个平台集成环境管理器的全局或者静态变量进行赋值初始化

static void init_platform(const QString &pluginArgument,/*key*/  const QString &platformPluginPath, const QString &platformThemeName, int &argc, char **argv)
{
   
    // Split into platform name and arguments
    QStringList arguments = pluginArgument.split(QLatin1Char(':'));
    const QString name = arguments.takeFirst().toLower();/*key*/ 
   // Create the platform integration.
    QGuiApplicationPrivate::platform_integration = QPlatformIntegrationFactory::create(name,/*key*/ arguments, argc, argv, platformPluginPath);
   

加载平台集成环境 即XCB

使用平台继承管理器的工厂模式进行创建

QPlatformIntegration *QPlatformIntegrationFactory::create(const QString &platform,/*key*/ const QStringList &paramList, int &argc, char **argv, const QString &platformPluginPath)
{
   
    if (QPlatformIntegration *ret = loadIntegration(loader(), platform,/*key*/ paramList, argc, argv))
        return ret;
}

QPlatformIntegrationFactory 平台继承器的工厂

class Q_GUI_EXPORT QPlatformIntegrationFactory
{
   
public:
    static QStringList keys(const QString &platformPluginPath = QString());
    static QPlatformIntegration *create(const QString &name,/*key*/ const QStringList &args, 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值