Copyright notice
This article is an original work, please respect the author's labor results. Reprint must maintain the integrity of the article, and in the form of hyperlinks to the original author "Tingsking18" and the main site address, convenient for other friends to ask questions and correct.
QT Source Parsing (a) qt creation window program, message loop, and WinMain function
QT Source Parsing (ii) deep analysis of QT Meta-object system and signal slot mechanism
QT Source Parsing (iii) deep analysis of QT Meta-object system and signal slot mechanism (cont.)
Qt Source Parsing (iv) Anatomy of the event mechanism of QT
Qt Source Parsing (v) Implementation of Qlibrary cross-platform invoke dynamic library
QT Source Code Analysis (vi) the relationship between QT signal slot mechanism and event mechanism
QT Source Parsing (vii) QT process for creating forms
QT Source parsing (eight) how QT handles Windows messages
Qt Source Parsing (ix) parsing qdatetime
Preface: Analysis QT Code also has a period of time, previously in the QT source parsing always use UE, a function name in the QTDIR/SRC directory repeated search, and then analysis function between the call relationship, efficiency is too low, recently summed up a more convenient way, is to use QT Creator this IDE.
The benefits are:
1. Qt Creator can easily track code calls, which greatly improves the speed of parsing code.
2. The call relationship between functions can be found more intuitively.
3. Easy to grasp the longitudinal relationship of the code.
The disadvantages:
1. Only shows the relationship of the called function or class.
2. Lack of the overall grasp of the relationship between a class, a group of classes, functions.
The above summarizes their own in the QT source parsing time to use the method, the following began to enter the topic. QT Create the form process, because I am not very familiar with Linux, below all my analysis is based on Windows.
Create a form with the API under Windows. I don't have much to explain here, just give the code, and then combine the following code to analyze the process of QT creating the form.
For a detailed explanation, please refer to:
John Chen Daniel's blog:WIN32 SDK Interface Programming
[CPP]View Plaincopy
- #include <windows.h>
- LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
- int WINAPI WinMain (hinstance hinstance, HInstance hprevinstance,
- PSTR szcmdline, int icmdshow)
- {
- Static TCHAR szappname[] = TEXT ("Hellowin");
- HWND hwnd;
- MSG msg;
- WNDCLASS WC;
- Wc.style = Cs_hredraw | Cs_vredraw;
- Wc.lpfnwndproc = WndProc;
- Wc.cbclsextra = 0;
- Wc.cbwndextra = 0;
- Wc.hinstance = hinstance;
- Wc.hicon = LoadIcon (NULL, idi_application);
- Wc.hcursor = LoadCursor (NULL, Idc_arrow);
- Wc.hbrbackground = (hbrush) getstockobject (White_brush);
- Wc.lpszmenuname = NULL;
- Wc.lpszclassname = Szappname;
- if (! RegisterClass (&WC))
- {
- MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, Mb_iconerror);
- return 0;
- }
- hwnd = CreateWindow (szappname,//window class name
- TEXT ("Hello"),//Window caption
- Ws_overlappedwindow,//Window style
- Cw_usedefault,//initial X position
- Cw_usedefault,//initial y position
- Cw_usedefault,//initial x size
- Cw_usedefault,//Initial y size
- NULL,//parent window Handle
- NULL,//Window menu handle
- HINSTANCE,//program instance handle
- NULL); Creation parameters
- ShowWindow (hwnd, icmdshow);
- UpdateWindow (HWND);
- while (GetMessage (&msg, NULL, 0, 0))
- {
- TranslateMessage (&MSG);
- DispatchMessage (&MSG);
- }
- return msg.wparam;
- }
- LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
- {
- HDC hdc;
- Paintstruct PS;
- Rect rect;
- Switch (message)
- {
- Case WM_PAINT:
- HDC = BeginPaint (hwnd, &PS);
- GetClientRect (hwnd, &rect);
- DrawText (HDC, TEXT ("The Wm_paintmessage"),-1, &rect,dt_singleline | Dt_center | Dt_vcenter);
- EndPaint (hwnd, &PS);
- return 0;
- Case Wm_destroy:
- PostQuitMessage (0);
- return 0;
- }
- Return DefWindowProc (HWND, message, WParam, LParam);
- }
First, write the simplest QT program:
[CPP]View Plaincopy
- #include <QtGui/QApplication>
- #include <QPushButton>
- int main (int argc, char *argv[])
- {
- Qapplication A (argc, argv);
- Qpushbutton w ("Hello Kitty");
- W.show ();
- return A.exec ();
- }
To analyze how this form program was created.
First of all about the main function and the WinMain function, why the QT window program is using the main function instead of the WinMain, in my other blog post is explained:QT source Parsing (a) qt creation window program, message loop and WinMain function is no longer explained
Windows Forms creation will call registerclass This function, we search inside the QTDIR/SRC, there are two files have this function one is qapplication_win.cpp another is Qeventdispatcher_ Win.cpp, two different roles, this time we first study qapplication_win.cpp in the RegisterClass function, because this is related to form creation, the next QT Source code parsing ( Eight) howQt handles windows messages will be introduced in Qeventdispatcher_ The role of registerclass in Win.cpp.
We first set the breakpoint in the Qt_reg_winclass function in Qapplication_win.cpp, then start debugging, run to the breakpoint, and then we look at the call stack such as:
Below the red box for call stack, we can see the order of function calls, the real creation of Qpushbutton is in the show () method, and the show () method calls the SetVisible method ....
Qtwndproc is the callback function of the form, which is passed to the WNDCLASS structure when registerclass, qtwndproc the function WndProc with the above API to create the form.
Let's take a look at the Qtwndproc code, which is also a switch (message) and then a bunch of case to process the message, and finally call DefWindowProc to return the message to the system that is not processed by him.
Reference: http://blog.csdn.net/tingsking18/article/details/5528666
QT process for creating forms