Inside Qt for Symbian QApplication/zh: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
作者: [http://developer.qt.nokia.com/member/3776 habert] ''[developer.qt.nokia.com]''
作者: "habert":http://developer.qt.nokia.com/member/3776


==Qt for Symbian之程序启动==
== Qt for Symbian之程序启动 ==


我们都知道Qt中只要构造出QApplication就启动了,可启动细节就很少有人知道了,当然我们编写Qt的程序无需关心这么多,这也是为什么要用Qt的原因,但如果能够了解则可以加深对Qt的认识。今天看了一下Qt for Symbian的源码,整理如下,希望对有兴趣的朋友有帮助:<br /> 第一步构造了QApplicationPrivate的实例,其中GuiClient是一个枚举常量,表示应用程序的类型,这里我们不用关心。说道 QApplicationPrivate,如果你看过Qt的源码,你会发现很多类都配对有相应的Private类,这是Qt在封装底层实现时经常用的一种模式,Handle-Body Design Pattern,也就是说你在xx类中看不到具体的实现,具体的实现都封装在xxPrivate类中,而在xx类的方法只是调用xxPrivate中相应的方法。
我们都知道Qt中只要构造出QApplication就启动了,可启动细节就很少有人知道了,当然我们编写Qt的程序无需关心这么多,这也是为什么要用Qt的原因,但如果能够了解则可以加深对Qt的认识。今天看了一下Qt for Symbian的源码,整理如下,希望对有兴趣的朋友有帮助:<br /><code>QApplication::QApplication(int &amp;argc, char **argv)<br /> : QCoreApplication('''new QApplicationPrivate(argc, argv, GuiClient) //第一步) //第二步<br />{<br /> Q_D(QApplication); //第三步<br /> d-&gt;construct(); //第四步<br />}</code><br />第一步构造了QApplicationPrivate的实例,其中GuiClient是一个枚举常量,表示应用程序的类型,这里我们不用关心。说道 QApplicationPrivate,如果你看过Qt的源码,你会发现很多类都配对有相应的Private类,这是Qt在封装底层实现时经常用的一种模式,Handle-Body Design Pattern,也就是说你在xx类中看不到具体的实现,具体的实现都封装在xxPrivate类中,而在xx类的方法只是调用xxPrivate中相应的方法。
<br />第二步用QApplicationPrivate的实例构造了QCoreApplication,这里调用的是QCoreApplication的QCoreApplication::QCoreApplication(QCoreApplicationPrivate &amp;p)构造函数,代码如下:<br /><code>QCoreApplication::QCoreApplication(QCoreApplicationPrivate &amp;p)<br /> : QObject(p, 0)<br />{<br /> init();<br /> // note: it is the subclasses' job to call<br /> // QCoreApplicationPrivate::eventDispatcher-&gt;startingUp();<br />}</code><br />上面代码中的init初始化了一些资源,其中包括事件分发器。
<br />第三步定义了d,这里是通过宏Q_D来实现的,其中宏Q_D的定义如下:<br /><code>#define Q_D(Class) Class##Private''' const d = d_func()<code><br />所以这里的d即是调用父类构造函数QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient)时创建的QApplicationPrivate实例。


第二步用QApplicationPrivate的实例构造了QCoreApplication,这里调用的是QCoreApplication的QCoreApplication::QCoreApplication(QCoreApplicationPrivate &amp;p)构造函数,代码如下:<br /> 上面代码中的init初始化了一些资源,其中包括事件分发器。
第四步调用d的construct函数,也就是QApplicationPrivate的实例的construct函数。代码如下所示:<br /></code>void QApplicationPrivate::construct(<br />#ifdef Q_WS_X11<br /> Display *dpy, Qt::HANDLE visual, Qt::HANDLE cmap<br />#endif<br /> )<br />{<br /> initResources();


第三步定义了d,这里是通过宏Q_D来实现的,其中宏Q_D的定义如下:<br /> 所以这里的d即是调用父类构造函数QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient)时创建的QApplicationPrivate实例。
qt_is_gui_used = (qt_appType != QApplication::Tty);<br /> process_cmdline();<br /> // the environment variable has the lowest precedence of runtime graphicssystem switches<br /> if (graphics_system_name.isEmpty())<br /> graphics_system_name = QString::fromLocal8Bit(qgetenv(&quot;QT_GRAPHICSSYSTEM&amp;quot;));<br /> // Must be called before initialize()<br /> //这里调用了qt_init<br /> qt_init(this, qt_appType<br />#ifdef Q_WS_X11<br /> , dpy, visual, cmap<br />#endif<br /> );<br /> initialize();<br /> eventDispatcher-&gt;startingUp();


第四步调用d的construct函数,也就是QApplicationPrivate的实例的construct函数。代码如下所示:<br /> 这里也是初始化了很多资源,其中要要说的是qt_init,因为就是它创建了程序,代码如下:<br /> 代码很多,哈哈。上面代码中标有注释“这里就是创建程序的地方” 的部分:<br /> 这里将newS60Application赋值给S60-&gt;s60ApplicationFactory,而newS60Application是什么呢,我们看看它的定义:<br /> 由此可知newS60Application就是一个函数指针,它创建了一个QS60MainApplication实例,这里QS60MainApplication类封装了CAknApplication的实现。<br /> 另外上面代码中s60是一个全局数据,里面放了很多东西,主要是方便直接查询,其类结构如下:<br />
#ifdef QT_EVAL<br /> extern void qt_gui_eval_init(uint);<br /> qt_gui_eval_init(application_type);<br />#endif


更多Qt中文资料请访问“Qt中文论坛”:http://www.thisisqt.com/forum
#if defined(Q_OS_SYMBIAN) &amp;&amp; !defined(QT_NO_SYSTEMLOCALE)<br /> symbianInit();<br />#endif
 
#ifndef QT_NO_LIBRARY<br /> if(load_testability) {<br /> QLibrary testLib(QLatin1String(&quot;qttestability&amp;quot;));<br /> if (testLib.load()) {<br /> typedef void ('''TasInitialize)(void);<br /> TasInitialize initFunction = (TasInitialize)testLib.resolve(&quot;qt_testability_init&amp;quot;);<br /> if (initFunction) {<br /> initFunction();<br /> } else {<br /> qCritical(&quot;Library qttestability resolve failed!&quot;);<br /> }<br /> } else {<br /> qCritical(&quot;Library qttestability load failed!&quot;);<br /> }<br /> }
<br /> //make sure the plugin is loaded<br /> if (qt_is_gui_used)<br /> qt_guiPlatformPlugin();<br />#endif<br />}<code><br />这里也是初始化了很多资源,其中要要说的是qt_init,因为就是它创建了程序,代码如下:<br /></code>void qt_init(QApplicationPrivate''' /* priv */, int)<br />{<br /> if (!CCoeEnv::Static()) {<br /> // The S60 framework creates a new trap handler which will render any existing traps<br /> // invalid as long as it is active. This means that all code in main() that occurs after<br /> // the QApplication construction needs to be surrounded by a new trap, despite having<br /> // an outer one already. To avoid this, we save the original trap handler here, and set<br /> // it back after the S60 framework is constructed. Then we restore it right before the S60<br /> // framework destruction.<br /> TTrapHandler '''origTrapHandler = User::TrapHandler();
<br /> // The S60 framework has not been initialized. We need to do it.<br /> //这里就是创建程序的地方<br /> TApaApplicationFactory factory(S60-&gt;s60ApplicationFactory ?<br /> S60-&gt;s60ApplicationFactory : newS60Application);<br /> CApaCommandLine''' commandLine = 0;<br /> TInt err = CApaCommandLine::GetCommandLineFromProcessEnvironment(commandLine);<br /> // After this construction, CEikonEnv will be available from CEikonEnv::Static().<br /> // (much like our qApp).<br /> CEikonEnv* coe = new CEikonEnv;<br /> //not using QT_TRAP_THROWING, because coe owns the cleanupstack so it can't be pushed there.<br /> if(err == KErrNone)<br /> TRAP (err, coe-&amp;gt;ConstructAppFromCommandLineL(factory,*commandLine)); //这里通过factory创建了程序<br /> delete commandLine;<br /> if(err != KErrNone) {<br /> qWarning() &lt;&lt; &quot;qt_init: Eikon application construct failed (&quot;<br /> &lt;&lt; err<br /> &lt;&lt; &quot;), maybe missing resource file on S60 3.1?&quot;;<br /> delete coe;<br /> qt_symbian_throwIfError(err);<br /> }
 
S60-&gt;s60InstalledTrapHandler = User::SetTrapHandler(origTrapHandler);
 
S60-&gt;qtOwnsS60Environment = true;<br /> } else {<br /> S60-&gt;qtOwnsS60Environment = false;<br /> }
 
#ifdef QT_NO_DEBUG<br /> if (!qgetenv(&quot;QT_S60_AUTO_FLUSH_WSERV&amp;quot;).isEmpty())<br />#endif<br /> S60-&gt;wsSession().SetAutoFlush(ETrue);
 
#ifdef Q_SYMBIAN_WINDOW_SIZE_CACHE<br /> TRAP_IGNORE(S60-&gt;wsSession().EnableWindowSizeCacheL());<br />#endif
 
S60-&gt;updateScreenSize();
 
TDisplayMode mode = S60-&gt;screenDevice()<s>&gt;DisplayMode();<br /> S60</s>&gt;screenDepth = TDisplayModeUtils::NumDisplayModeBitsPerPixel(mode);
 
//NB: RWsSession::GetColorModeList tells you what window modes are supported,<br /> //not what bitmap formats.<br /> if(QSysInfo::symbianVersion() == QSysInfo::SV_9_2)<br /> S60-&gt;supportsPremultipliedAlpha = 0;<br /> else<br /> S60-&gt;supportsPremultipliedAlpha = 1;
 
RProcess me;<br /> TSecureId securId = me.SecureId();<br /> S60-&gt;uid = securId.operator TUid();
 
// enable focus events - used to re-enable mouse after focus changed between mouse and non mouse app,<br /> // and for dimming behind modal windows<br /> S60-&gt;windowGroup().EnableFocusChangeEvents();
 
//Check if mouse interaction is supported (either EMouse=1 in the HAL, or EMachineUID is one of the phones known to support this)<br /> const TInt KMachineUidSamsungI8510 = 0x2000C51E;<br /> // HAL::Get(HALData::EPen, TInt&amp;amp; result) may set 'result' to 1 on some 3.1 systems (e.g. N95).<br /> // But we know that S60 systems below 5.0 did not support touch.<br /> static const bool touchIsUnsupportedOnSystem =<br /> QSysInfo::s60Version()  QSysInfo::SV_S60_3_1
        || QSysInfo::s60Version()  QSysInfo::SV_S60_3_2;<br /> TInt machineUID;<br /> TInt mouse;<br /> TInt touch;<br /> TInt err;<br /> err = HAL::Get(HALData::EMouse, mouse);<br /> if (err != KErrNone)<br /> mouse = 0;<br /> err = HAL::Get(HALData::EMachineUid, machineUID);<br /> if (err != KErrNone)<br /> machineUID = 0;<br /> err = HAL::Get(HALData::EPen, touch);<br /> if (err != KErrNone || touchIsUnsupportedOnSystem)<br /> touch = 0;<br />#ifdef ''WINS''<br /> if(QSysInfo::symbianVersion() &lt;= QSysInfo::SV_9_4) {<br /> //for symbian SDK emulator, force values to match typical devices.<br /> mouse = 0;<br /> touch = touchIsUnsupportedOnSystem ? 0 : 1;<br /> }<br />#endif<br /> if (mouse || machineUID == KMachineUidSamsungI8510) {<br /> S60-&gt;hasTouchscreen = false;<br /> S60-&gt;virtualMouseRequired = false;<br /> }<br /> else if (!touch) {<br /> S60-&gt;hasTouchscreen = false;<br /> S60-&gt;virtualMouseRequired = true;<br /> }<br /> else {<br /> S60-&gt;hasTouchscreen = true;<br /> S60-&gt;virtualMouseRequired = false;<br /> }
 
S60-&gt;avkonComponentsSupportTransparency = false;<br /> S60-&gt;menuBeingConstructed = false;
 
#ifdef Q_WS_S60<br /> TUid KCRUidAvkon = { 0x101F876E };<br /> TUint32 KAknAvkonTransparencyEnabled = 0x0000000D;
 
CRepository* repository = 0;<br /> TRAP (err, repository = CRepository::NewL(KCRUidAvkon));
 
if(err  KErrNone) &amp;#123;
        TInt value = 0;
        err = repository-&amp;gt;Get(KAknAvkonTransparencyEnabled, value);
        if(err  KErrNone) {<br /> S60-&gt;avkonComponentsSupportTransparency = (value==1) ? true : false;<br /> }<br /> }<br /> delete repository;<br /> repository = 0;<br />#endif
 
#ifdef QT_KEYPAD_NAVIGATION<br /> if (touch) {<br /> QApplicationPrivate::navigationMode = Qt::NavigationModeNone;<br /> } else {<br /> QApplicationPrivate::navigationMode = Qt::NavigationModeKeypadDirectional;<br /> }<br />#endif
 
#ifndef QT_NO_CURSOR<br /> //Check if window server pointer cursors are supported or not<br />#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS<br /> //In generic binary, use the HAL and OS version<br /> //Any other known good phones should be added here.<br /> if (machineUID == KMachineUidSamsungI8510 || (QSysInfo::symbianVersion() != QSysInfo::SV_9_4<br /> &amp;&amp; QSysInfo::symbianVersion() != QSysInfo::SV_9_3 &amp;&amp; QSysInfo::symbianVersion()<br /> != QSysInfo::SV_9_2)) {<br /> S60-&gt;brokenPointerCursors = false;<br /> qt_symbian_setWindowGroupCursor(Qt::ArrowCursor, S60-&gt;windowGroup());<br /> }<br /> else<br /> S60-&gt;brokenPointerCursors = true;<br />#endif
 
if (S60-&gt;mouseInteractionEnabled) {<br />#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS<br /> if (S60-&gt;brokenPointerCursors) {<br /> qt_symbian_set_pointer_sprite(Qt::ArrowCursor);<br /> qt_symbian_show_pointer_sprite();<br /> }<br /> else<br />#endif<br /> S60-&gt;wsSession().SetPointerCursorMode(EPointerCursorNormal);<br /> }<br />#endif
 
QFont systemFont;<br /> systemFont.setFamily(systemFont.defaultFamily());<br /> QApplicationPrivate::setSystemFont(systemFont);
 
#ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS<br /> QObject::connect(qApp, SIGNAL (aboutToQuit()), qApp, SLOT (_q_aboutToQuit()));<br />#endif
 
/*<br /> ### Commented out for now as parameter handling not needed in SOS (yet). Code below will break testlib with <s>o flag<br /> int argc = priv</s>&gt;argc;<br /> char **argv = priv-&gt;argv;
 
// Get command line params<br /> int j = argc ? 1 : 0;<br /> for (int i=1; i&amp;lt;argc; i+'') {<br /> if (argv[i] &amp;&amp; *argv[i] != '-') {<br /> argv[j''+] = argv[i];<br /> continue;<br /> }
 
#if defined(QT_DEBUG)<br /> if (qstrcmp(argv[i], &quot;<s>nograb&amp;quot;) == 0)<br /> appNoGrab = !appNoGrab;<br /> else<br />#endif // QT_DEBUG<br /> ;<br /> }<br />*/
<br /> // Register WId with the metatype system. This is to enable<br /> // QWidgetPrivate::create_sys to used delayed slot invocation in order<br /> // to destroy WId objects during reparenting.<br /> qRegisterMetaType&amp;lt;WId&amp;gt;(&quot;WId&amp;quot;);<br />}<code><br />代码很多,哈哈。上面代码中标有注释“这里就是创建程序的地方” 的部分:<br /></code>TApaApplicationFactory factory(S60</s>&gt;s60ApplicationFactory ?<br /> S60-&gt;s60ApplicationFactory : newS60Application);<code><br />这里将newS60Application赋值给S60-&gt;s60ApplicationFactory,而newS60Application是什么呢,我们看看它的定义:<br /></code>CApaApplication '''newS60Application()<br />{<br /> return new QS60MainApplication;<br />}<code><br />由此可知newS60Application就是一个函数指针,它创建了一个QS60MainApplication实例,这里QS60MainApplication类封装了CAknApplication的实现。<br />另外上面代码中s60是一个全局数据,里面放了很多东西,主要是方便直接查询,其类结构如下:<br /></code>class QS60Data<br />{<br />public:<br /> QS60Data();<br /> QThreadStorage&amp;lt;QS60ThreadLocalData'''&gt; tls;<br /> TUid uid;<br /> int screenDepth;<br /> QPoint lastCursorPos;<br /> QPoint lastPointerEventPos;<br /> QPointer&amp;lt;QWidget&amp;gt; lastPointerEventTarget;<br /> QPointer&amp;lt;QWidget&amp;gt; mousePressTarget;<br /> int screenWidthInPixels;<br /> int screenHeightInPixels;<br /> int screenWidthInTwips;<br /> int screenHeightInTwips;<br /> int defaultDpiX;<br /> int defaultDpiY;<br /> WId curWin;<br /> enum PressedKeys {<br /> Select = 0x1,<br /> Right = 0x2,<br /> Down = 0x4,<br /> Left = 0x8,<br /> Up = 0x10,<br /> LeftUp = 0x20,<br /> RightUp = 0x40,<br /> RightDown = 0x80,<br /> LeftDown = 0x100<br /> };<br /> int virtualMousePressedKeys; // of the above type, but avoids casting problems<br /> int virtualMouseAccelDX;<br /> int virtualMouseAccelDY;<br /> QElapsedTimer virtualMouseAccelTimeout;<br /> int virtualMouseMaxAccel;<br />#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS<br /> int brokenPointerCursors : 1;<br />#endif<br /> int hasTouchscreen : 1;<br /> int mouseInteractionEnabled : 1;<br /> int virtualMouseRequired : 1;<br /> int qtOwnsS60Environment : 1;<br /> int supportsPremultipliedAlpha : 1;<br /> int avkonComponentsSupportTransparency : 1;<br /> int menuBeingConstructed : 1;<br /> QApplication::QS60MainApplicationFactory s60ApplicationFactory; // typedef'ed pointer type
 
enum ScanCodeState {<br /> Unpressed,<br /> KeyDown,<br /> KeyDownAndKey<br /> };<br /> QHash&amp;lt;TInt, ScanCodeState&amp;gt; scanCodeStates;
 
static inline void updateScreenSize();<br /> inline RWsSession&amp;amp; wsSession();<br /> static inline RWindowGroup&amp;amp; windowGroup();<br /> inline CWsScreenDevice* screenDevice();<br /> static inline CCoeAppUi* appUi();<br /> static inline CEikMenuBar* menuBar();<br />#ifdef Q_WS_S60<br /> static inline CEikStatusPane* statusPane();<br /> static inline CCoeControl* statusPaneSubPane(TInt aPaneId);<br /> static inline CAknTitlePane* titlePane();<br /> static inline CAknContextPane* contextPane();<br /> static inline CEikButtonGroupContainer* buttonGroupContainer();<br /> static void setStatusPaneAndButtonGroupVisibility(bool statusPaneVisible, bool buttonGroupVisible);<br />#endif<br /> static void controlVisibilityChanged(CCoeControl *control, bool visible);
 
#ifdef Q_OS_SYMBIAN<br /> TTrapHandler *s60InstalledTrapHandler;<br />#endif<br />};<code>

Revision as of 11:25, 24 February 2015

作者: "habert&quot;:http://developer.qt.nokia.com/member/3776

Qt for Symbian之程序启动

我们都知道Qt中只要构造出QApplication就启动了,可启动细节就很少有人知道了,当然我们编写Qt的程序无需关心这么多,这也是为什么要用Qt的原因,但如果能够了解则可以加深对Qt的认识。今天看了一下Qt for Symbian的源码,整理如下,希望对有兴趣的朋友有帮助:

QApplication::QApplication(int &amp;argc, char **argv)<br /> : QCoreApplication('''new QApplicationPrivate(argc, argv, GuiClient) //第一步) //第二步<br />{<br /> Q_D(QApplication); //第三步<br /> d-&gt;construct(); //第四步<br />}


第一步构造了QApplicationPrivate的实例,其中GuiClient是一个枚举常量,表示应用程序的类型,这里我们不用关心。说道 QApplicationPrivate,如果你看过Qt的源码,你会发现很多类都配对有相应的Private类,这是Qt在封装底层实现时经常用的一种模式,Handle-Body Design Pattern,也就是说你在xx类中看不到具体的实现,具体的实现都封装在xxPrivate类中,而在xx类的方法只是调用xxPrivate中相应的方法。
第二步用QApplicationPrivate的实例构造了QCoreApplication,这里调用的是QCoreApplication的QCoreApplication::QCoreApplication(QCoreApplicationPrivate &p)构造函数,代码如下:

QCoreApplication::QCoreApplication(QCoreApplicationPrivate &amp;p)<br /> : QObject(p, 0)<br />{<br /> init();<br /> // note: it is the subclasses' job to call<br /> // QCoreApplicationPrivate::eventDispatcher-&gt;startingUp();<br />}


上面代码中的init初始化了一些资源,其中包括事件分发器。
第三步定义了d,这里是通过宏Q_D来实现的,其中宏Q_D的定义如下:

#define Q_D(Class) Class##Private''' const d = d_func()<code><br />所以这里的d即是调用父类构造函数QCoreApplication(*new QApplicationPrivate(argc, argv, GuiClient)时创建的QApplicationPrivate实例。

第四步调用d的construct函数也就是QApplicationPrivate的实例的construct函数代码如下所示<br />

void QApplicationPrivate::construct(
#ifdef Q_WS_X11
Display *dpy, Qt::HANDLE visual, Qt::HANDLE cmap
#endif
)
{
initResources();

qt_is_gui_used = (qt_appType != QApplication::Tty);
process_cmdline();
// the environment variable has the lowest precedence of runtime graphicssystem switches
if (graphics_system_name.isEmpty())
graphics_system_name = QString::fromLocal8Bit(qgetenv("QT_GRAPHICSSYSTEM&quot;));
// Must be called before initialize()
//这里调用了qt_init
qt_init(this, qt_appType
#ifdef Q_WS_X11
, dpy, visual, cmap
#endif
);
initialize();
eventDispatcher->startingUp();

  1. ifdef QT_EVAL
    extern void qt_gui_eval_init(uint);
    qt_gui_eval_init(application_type);
    #endif
  1. if defined(Q_OS_SYMBIAN) && !defined(QT_NO_SYSTEMLOCALE)
    symbianInit();
    #endif
  1. ifndef QT_NO_LIBRARY
    if(load_testability) {
    QLibrary testLib(QLatin1String("qttestability&quot;));
    if (testLib.load()) {
    typedef void (TasInitialize)(void);
    TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init&quot;);
    if (initFunction) {
    initFunction();
    } else {
    qCritical("Library qttestability resolve failed!");
    }
    } else {
    qCritical("Library qttestability load failed!");
    }
    }


//make sure the plugin is loaded
if (qt_is_gui_used)
qt_guiPlatformPlugin();
#endif
}

<br />这里也是初始化了很多资源其中要要说的是qt_init因为就是它创建了程序代码如下<br />

void qt_init(QApplicationPrivate /* priv */, int)
{
if (!CCoeEnv::Static()) {
// The S60 framework creates a new trap handler which will render any existing traps
// invalid as long as it is active. This means that all code in main() that occurs after
// the QApplication construction needs to be surrounded by a new trap, despite having
// an outer one already. To avoid this, we save the original trap handler here, and set
// it back after the S60 framework is constructed. Then we restore it right before the S60
// framework destruction.
TTrapHandler
origTrapHandler = User::TrapHandler();


// The S60 framework has not been initialized. We need to do it.
//这里就是创建程序的地方
TApaApplicationFactory factory(S60->s60ApplicationFactory ?
S60->s60ApplicationFactory : newS60Application);
CApaCommandLine commandLine = 0;
TInt err = CApaCommandLine::GetCommandLineFromProcessEnvironment(commandLine);
// After this construction, CEikonEnv will be available from CEikonEnv::Static().
// (much like our qApp).
CEikonEnv* coe = new CEikonEnv;
//not using QT_TRAP_THROWING, because coe owns the cleanupstack so it can't be pushed there.
if(err == KErrNone)
TRAP (err, coe-&gt;ConstructAppFromCommandLineL(factory,*commandLine)); //这里通过factory创建了程序
delete commandLine;
if(err != KErrNone) {
qWarning() << "qt_init: Eikon application construct failed ("
<< err
<< "), maybe missing resource file on S60 3.1?";
delete coe;
qt_symbian_throwIfError(err);
}

S60->s60InstalledTrapHandler = User::SetTrapHandler(origTrapHandler);

S60->qtOwnsS60Environment = true;
} else {
S60->qtOwnsS60Environment = false;
}

  1. ifdef QT_NO_DEBUG
    if (!qgetenv("QT_S60_AUTO_FLUSH_WSERV&quot;).isEmpty())
    #endif
    S60->wsSession().SetAutoFlush(ETrue);
  1. ifdef Q_SYMBIAN_WINDOW_SIZE_CACHE
    TRAP_IGNORE(S60->wsSession().EnableWindowSizeCacheL());
    #endif

S60->updateScreenSize();

TDisplayMode mode = S60->screenDevice()>DisplayMode();
S60
>screenDepth = TDisplayModeUtils::NumDisplayModeBitsPerPixel(mode);

//NB: RWsSession::GetColorModeList tells you what window modes are supported,
//not what bitmap formats.
if(QSysInfo::symbianVersion() == QSysInfo::SV_9_2)
S60->supportsPremultipliedAlpha = 0;
else
S60->supportsPremultipliedAlpha = 1;

RProcess me;
TSecureId securId = me.SecureId();
S60->uid = securId.operator TUid();

// enable focus events - used to re-enable mouse after focus changed between mouse and non mouse app,
// and for dimming behind modal windows
S60->windowGroup().EnableFocusChangeEvents();

//Check if mouse interaction is supported (either EMouse=1 in the HAL, or EMachineUID is one of the phones known to support this)
const TInt KMachineUidSamsungI8510 = 0x2000C51E;
// HAL::Get(HALData::EPen, TInt&amp; result) may set 'result' to 1 on some 3.1 systems (e.g. N95).
// But we know that S60 systems below 5.0 did not support touch.
static const bool touchIsUnsupportedOnSystem =
QSysInfo::s60Version() QSysInfo::SV_S60_3_1

       || QSysInfo::s60Version()  QSysInfo::SV_S60_3_2;
TInt machineUID;
TInt mouse;
TInt touch;
TInt err;
err = HAL::Get(HALData::EMouse, mouse);
if (err != KErrNone)
mouse = 0;
err = HAL::Get(HALData::EMachineUid, machineUID);
if (err != KErrNone)
machineUID = 0;
err = HAL::Get(HALData::EPen, touch);
if (err != KErrNone || touchIsUnsupportedOnSystem)
touch = 0;
#ifdef WINS
if(QSysInfo::symbianVersion() <= QSysInfo::SV_9_4) {
//for symbian SDK emulator, force values to match typical devices.
mouse = 0;
touch = touchIsUnsupportedOnSystem ? 0 : 1;
}
#endif
if (mouse || machineUID == KMachineUidSamsungI8510) {
S60->hasTouchscreen = false;
S60->virtualMouseRequired = false;
}
else if (!touch) {
S60->hasTouchscreen = false;
S60->virtualMouseRequired = true;
}
else {
S60->hasTouchscreen = true;
S60->virtualMouseRequired = false;
}

S60->avkonComponentsSupportTransparency = false;
S60->menuBeingConstructed = false;

  1. ifdef Q_WS_S60
    TUid KCRUidAvkon = { 0x101F876E };
    TUint32 KAknAvkonTransparencyEnabled = 0x0000000D;

CRepository* repository = 0;
TRAP (err, repository = CRepository::NewL(KCRUidAvkon));

if(err KErrNone) &#123;

       TInt value = 0;
       err = repository-&gt;Get(KAknAvkonTransparencyEnabled, value);
       if(err  KErrNone) {
S60->avkonComponentsSupportTransparency = (value==1) ? true : false;
}
}
delete repository;
repository = 0;
#endif
  1. ifdef QT_KEYPAD_NAVIGATION
    if (touch) {
    QApplicationPrivate::navigationMode = Qt::NavigationModeNone;
    } else {
    QApplicationPrivate::navigationMode = Qt::NavigationModeKeypadDirectional;
    }
    #endif
  1. ifndef QT_NO_CURSOR
    //Check if window server pointer cursors are supported or not
    #ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
    //In generic binary, use the HAL and OS version
    //Any other known good phones should be added here.
    if (machineUID == KMachineUidSamsungI8510 || (QSysInfo::symbianVersion() != QSysInfo::SV_9_4
    && QSysInfo::symbianVersion() != QSysInfo::SV_9_3 && QSysInfo::symbianVersion()
     != QSysInfo::SV_9_2)) {
    S60->brokenPointerCursors = false;
    qt_symbian_setWindowGroupCursor(Qt::ArrowCursor, S60->windowGroup());
    }
    else
    S60->brokenPointerCursors = true;
    #endif

if (S60->mouseInteractionEnabled) {
#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
if (S60->brokenPointerCursors) {
qt_symbian_set_pointer_sprite(Qt::ArrowCursor);
qt_symbian_show_pointer_sprite();
}
else
#endif
S60->wsSession().SetPointerCursorMode(EPointerCursorNormal);
}
#endif

QFont systemFont;
systemFont.setFamily(systemFont.defaultFamily());
QApplicationPrivate::setSystemFont(systemFont);

  1. ifdef SYMBIAN_GRAPHICS_WSERV_QT_EFFECTS
    QObject::connect(qApp, SIGNAL (aboutToQuit()), qApp, SLOT (_q_aboutToQuit()));
    #endif

/*
### Commented out for now as parameter handling not needed in SOS (yet). Code below will break testlib with o flag
int argc = priv
>argc;
char **argv = priv->argv;

// Get command line params
int j = argc ? 1 : 0;
for (int i=1; i&lt;argc; i+) {
if (argv[i] && *argv[i] != '-') {
argv[j
+] = argv[i];
continue;
}

  1. if defined(QT_DEBUG)
    if (qstrcmp(argv[i], "nograb&quot;) == 0)
    appNoGrab = !appNoGrab;
    else
    #endif // QT_DEBUG
     ;
    }
    */


// Register WId with the metatype system. This is to enable
// QWidgetPrivate::create_sys to used delayed slot invocation in order
// to destroy WId objects during reparenting.
qRegisterMetaType&lt;WId&gt;("WId&quot;);
}

<br />代码很多哈哈上面代码中标有注释这里就是创建程序的地方 的部分:<br />

TApaApplicationFactory factory(S60>s60ApplicationFactory ?
S60->s60ApplicationFactory : newS60Application);

<br />这里将newS60Application赋值给S60-&gt;s60ApplicationFactory而newS60Application是什么呢我们看看它的定义<br />

CApaApplication newS60Application()
{
return new QS60MainApplication;
}

<br />由此可知newS60Application就是一个函数指针它创建了一个QS60MainApplication实例这里QS60MainApplication类封装了CAknApplication的实现<br />另外上面代码中s60是一个全局数据里面放了很多东西主要是方便直接查询其类结构如下<br />

class QS60Data
{
public:
QS60Data();
QThreadStorage&lt;QS60ThreadLocalData
> tls;
TUid uid;
int screenDepth;
QPoint lastCursorPos;
QPoint lastPointerEventPos;
QPointer&lt;QWidget&gt; lastPointerEventTarget;
QPointer&lt;QWidget&gt; mousePressTarget;
int screenWidthInPixels;
int screenHeightInPixels;
int screenWidthInTwips;
int screenHeightInTwips;
int defaultDpiX;
int defaultDpiY;
WId curWin;
enum PressedKeys {
Select = 0x1,
Right = 0x2,
Down = 0x4,
Left = 0x8,
Up = 0x10,
LeftUp = 0x20,
RightUp = 0x40,
RightDown = 0x80,
LeftDown = 0x100
};
int virtualMousePressedKeys; // of the above type, but avoids casting problems
int virtualMouseAccelDX;
int virtualMouseAccelDY;
QElapsedTimer virtualMouseAccelTimeout;
int virtualMouseMaxAccel;
#ifndef Q_SYMBIAN_FIXED_POINTER_CURSORS
int brokenPointerCursors : 1;
#endif
int hasTouchscreen : 1;
int mouseInteractionEnabled : 1;
int virtualMouseRequired : 1;
int qtOwnsS60Environment : 1;
int supportsPremultipliedAlpha : 1;
int avkonComponentsSupportTransparency : 1;
int menuBeingConstructed : 1;
QApplication::QS60MainApplicationFactory s60ApplicationFactory; // typedef'ed pointer type

enum ScanCodeState {
Unpressed,
KeyDown,
KeyDownAndKey
};
QHash&lt;TInt, ScanCodeState&gt; scanCodeStates;

static inline void updateScreenSize();
inline RWsSession&amp; wsSession();
static inline RWindowGroup&amp; windowGroup();
inline CWsScreenDevice* screenDevice();
static inline CCoeAppUi* appUi();
static inline CEikMenuBar* menuBar();
#ifdef Q_WS_S60
static inline CEikStatusPane* statusPane();
static inline CCoeControl* statusPaneSubPane(TInt aPaneId);
static inline CAknTitlePane* titlePane();
static inline CAknContextPane* contextPane();
static inline CEikButtonGroupContainer* buttonGroupContainer();
static void setStatusPaneAndButtonGroupVisibility(bool statusPaneVisible, bool buttonGroupVisible);
#endif
static void controlVisibilityChanged(CCoeControl *control, bool visible);

  1. ifdef Q_OS_SYMBIAN
    TTrapHandler *s60InstalledTrapHandler;
    #endif
    };