理解的关键节点是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 ( ) ;
QCoreApplicationPrivate:: eventDispatcher-> startingUp ( ) ;
}
初始化的时候创建事件分发器
void QGuiApplicationPrivate :: init ( )
{
QCoreApplicationPrivate :: init ( ) ;
}
void QCoreApplicationPrivate :: init ( )
{
if ( ! eventDispatcher)
eventDispatcher = threadData-> eventDispatcher. load ( ) ;
if ( ! eventDispatcher)
createEventDispatcher ( ) ;
}
void QCoreApplicationPrivate :: createEventDispatcher ( )
{
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 ( ) ;
}
void QGuiApplicationPrivate :: createEventDispatcher ( )
{
Q_ASSERT ( ! eventDispatcher) ;
if ( platform_integration == 0 )
createPlatformIntegration ( ) ;
Q_ASSERT ( ! eventDispatcher) ;
eventDispatcher = platform_integration-> createEventDispatcher ( ) ;
}
GUI应用私有类 创建一个窗口系统平台相关的集成管理器
窗口系统平台相关的集成管理器中用来创建事件分发器
void QGuiApplicationPrivate :: createPlatformIntegration ( )
{
QString platformPluginPath = QLatin1String ( qgetenv ( "QT_QPA_PLATFORM_PLUGIN_PATH" ) ) ;
const bool isXcb = platformName == "xcb" ;
init_platform ( QLatin1String ( platformName) , platformPluginPath, platformThemeName, argc, argv) ;
}
初始化平台 是一个纯粹的函数
函数中对一个平台集成环境管理器的全局或者静态变量进行赋值初始化
static void init_platform ( const QString & pluginArgument, const QString & platformPluginPath, const QString & platformThemeName, int & argc, char * * argv)
{
QStringList arguments = pluginArgument. split ( QLatin1Char ( ':' ) ) ;
const QString name = arguments. takeFirst ( ) . toLower ( ) ;
QGuiApplicationPrivate:: platform_integration = QPlatformIntegrationFactory :: create ( name, arguments, argc, argv, platformPluginPath) ;
加载平台集成环境 即XCB
使用平台继承管理器的工厂模式进行创建
QPlatformIntegration * QPlatformIntegrationFactory :: create ( const QString & platform, const QStringList & paramList, int & argc, char * * argv, const QString & platformPluginPath)
{
if ( QPlatformIntegration * ret = loadIntegration ( loader ( ) , platform, 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, const QStringList & args,