QT process for creating forms

Source: Internet
Author: User

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
  1. #include <windows.h>
  2. LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
  3. int WINAPI WinMain (hinstance hinstance, HInstance hprevinstance,
  4. PSTR szcmdline, int icmdshow)
  5. {
  6. Static TCHAR szappname[] = TEXT ("Hellowin");
  7. HWND hwnd;
  8. MSG msg;
  9. WNDCLASS WC;
  10. Wc.style = Cs_hredraw | Cs_vredraw;
  11. Wc.lpfnwndproc = WndProc;
  12. Wc.cbclsextra = 0;
  13. Wc.cbwndextra = 0;
  14. Wc.hinstance = hinstance;
  15. Wc.hicon = LoadIcon (NULL, idi_application);
  16. Wc.hcursor = LoadCursor (NULL, Idc_arrow);
  17. Wc.hbrbackground = (hbrush) getstockobject (White_brush);
  18. Wc.lpszmenuname = NULL;
  19. Wc.lpszclassname = Szappname;
  20. if (! RegisterClass (&WC))
  21. {
  22. MessageBox (NULL, TEXT ("This program requires Windows nt!"), Szappname, Mb_iconerror);
  23. return 0;
  24. }
  25. hwnd = CreateWindow (szappname,//window class name
  26. TEXT ("Hello"),//Window caption
  27. Ws_overlappedwindow,//Window style
  28. Cw_usedefault,//initial X position
  29. Cw_usedefault,//initial y position
  30. Cw_usedefault,//initial x size
  31. Cw_usedefault,//Initial y size
  32. NULL,//parent window Handle
  33. NULL,//Window menu handle
  34. HINSTANCE,//program instance handle
  35. NULL); Creation parameters
  36. ShowWindow (hwnd, icmdshow);
  37. UpdateWindow (HWND);
  38. while (GetMessage (&msg, NULL, 0, 0))
  39. {
  40. TranslateMessage (&MSG);
  41. DispatchMessage (&MSG);
  42. }
  43. return msg.wparam;
  44. }
  45. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM WPARAM, LPARAM LPARAM)
  46. {
  47. HDC hdc;
  48. Paintstruct PS;
  49. Rect rect;
  50. Switch (message)
  51. {
  52. Case WM_PAINT:
  53. HDC = BeginPaint (hwnd, &PS);
  54. GetClientRect (hwnd, &rect);
  55. DrawText (HDC, TEXT ("The Wm_paintmessage"),-1, &rect,dt_singleline | Dt_center | Dt_vcenter);
  56. EndPaint (hwnd, &PS);
  57. return 0;
  58. Case Wm_destroy:
  59. PostQuitMessage (0);
  60. return 0;
  61. }
  62. Return DefWindowProc (HWND, message, WParam, LParam);
  63. }

First, write the simplest QT program:

[CPP]View Plaincopy
    1. #include <QtGui/QApplication>
    2. #include <QPushButton>
    3. int main (int argc, char *argv[])
    4. {
    5. Qapplication A (argc, argv);
    6. Qpushbutton w ("Hello Kitty");
    7. W.show ();
    8. return A.exec ();
    9. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.