August 18, 2011

mehoggan mehoggan
Lab Rat
13 posts

Can Someone Explain “this” Portion of the Qt Framework?

 

In the tutorial found at ( :http://doc.qt.nokia.com/latest/widgets-tutorial.html ):

You have the following code.

  1. #include <QtGui>
  2.  
  3. int main(int argc, char *argv[])
  4.  {
  5.      QApplication app(argc, argv);
  6.      QWidget window;
  7.      window.resize(320, 240);
  8.      window.show();
  9.      window.setWindowTitle(
  10.          QApplication::translate("toplevel", "Top-level widget"));
  11.      return app.exec();
  12.  }

Comming from a Win32 perspective is it the application that has all mesages directed to it from WndProc, and then it redirects those to the “top level” widget?

If this is the case how does QApplication (QCoreApplication) know about the top level widget? What happens if the user never sets a top level widget?

The way that I have normally redirect messages to my applications is by using the following code.

  1. LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  2. {
  3.  HDC hDC;
  4.  PAINTSTRUCT ps;
  5.  
  6.  static IController *cntrl;
  7.  cntrl = (IController*)::GetWindowLongPtr(hWnd, GWL_USERDATA);
  8.  
  9.  if(uMsg == WM_NCCREATE)
  10.  {
  11.   cntrl = (IController*)(((CREATESTRUCT*)lParam)->lpCreateParams);
  12.                             ::SetWindowLongPtr(hWnd, GWL_USERDATA, (LONG_PTR)cntrl);
  13.   cntrl->CheckStatus();
  14.                             return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
  15.  }
  16. ...

What is Qt doing differently from what I am doing to redirect calls to WndProc back into the class framework provided by Qt?

 Signature 

Matthew Hoggan
http://matthewh.me
“It’s not wise to violate rules until you know how to observe them.” (t.s. eliot)

8 replies

August 18, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

Forget WIN32 for that, WIN32 API is a C based message API and not a C++ Object oriented API.

Qt is object oriented.
The important thing to do is:

  1.     QString text = ...;
  2.     window.setWindowTitle(text);

This will handle setting the window title for you, however it is implemented internally (if you are interested, lok at the Qt source, it is different for the platforms and if the widget is top level or not).

The second thing that happens is:

  1.     QString text = QApplication::translate("toplevel", "Top-level widget");

This is Qt’s way of NLS handling. You could also write

  1.     QString text = "Top-level widget";

but then the text is never translated. By the way, the text is just text, nothing else. If it is top level or not, who cares?

For Qt’s NLS handling, look here [doc.qt.nokia.com]
There yopu see tr() method is used. tr is part of QObject. If you are in main, you are not inside a QObject, so how to call this function? The workaround is: QApplication::translate [doc.qt.nokia.com]

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 18, 2011

mehoggan mehoggan
Lab Rat
13 posts

Forget WIN32 for that, WIN32 API is a C based message API and not a C++ Object oriented API

I have been looking through the source, specifically at the portion of the code the deals with the Windows’ windowing system, and Win32 is all over the place.

…(if you are interested, lok at the Qt source, it is different for the platforms and if the widget is top level or not).

I am interested in the internals, there is alot of source to go through. I find it well organized, however the level of abstraction to make Qt cross platform can be a bit of a headache. Could you point me to a good walkthrough of the Qt “kernel” (as the source calls it)?

 Signature 

Matthew Hoggan
http://matthewh.me
“It’s not wise to violate rules until you know how to observe them.” (t.s. eliot)

August 18, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

mehoggan wrote:
bq. Forget WIN32 for that, WIN32 API is a C based message API and not a C++ Object oriented API

I have been looking through the source, specifically at the portion of the code the deals with the Windows’ windowing system, and Win32 is all over the place.

sure, internally, for the top level widget, it uses win32 calls. I don’t know why you try to redirect calls? setting the window title in win32 SDK is done by WM_SETWINDOWTEXT

mehoggan wrote:

bq. …(if you are interested, lok at the Qt source, it is different for the platforms and if the widget is top level or not).

I am interested in the internals, there is alot of source to go through. I find it well organized, however the level of abstraction to make Qt cross platform can be a bit of a headache. Could you point me to a good walkthrough of the Qt “kernel” (as the source calls it)?

You have to study it on your own. I have already looked into many of the files, but only, If I need some info.
The windows specific stuff for widgets is all in QWidget_win.cpp

in this special part, it’s:

  1. void QWidgetPrivate::setWindowTitle_sys(const QString &caption)
  2. {
  3.     Q_Q(QWidget);
  4.     if (!q->isWindow())
  5.         return;
  6.  
  7.     Q_ASSERT(q->testAttribute(Qt::WA_WState_Created));
  8.     SetWindowText(q->internalWinId(), (wchar_t*)caption.utf16());
  9. }

SetWindowText is the win32 API to do it, no need of message forwarding. This call is send to an HWND, normal windows behavior.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 18, 2011

mehoggan mehoggan
Lab Rat
13 posts

You have to study it on your own.

I am trying, like I said in the first post there is a lot to work through.

I am working on trying to figure out how the system initializes itself. For example, how does Qt allow you to bypass WinMain etc. If you could provide a “good” starting link that would be greatly appreciated.

Thank you,

 Signature 

Matthew Hoggan
http://matthewh.me
“It’s not wise to violate rules until you know how to observe them.” (t.s. eliot)

August 18, 2011

peppe peppe
Ant Farmer
1026 posts

mehoggan wrote:
bq. You have to study it on your own.

I am trying, like I said in the first post there is a lot to work through.

I am working on trying to figure out how the system initializes itself. For example, how does Qt allow you to bypass WinMain etc. If you could provide a “good” starting link that would be greatly appreciated.

Thank you,

It does NOT bypass WinMain.

https://qt.gitorious.org/qt/qt/blobs/master/src/winmain/qtmain_win.cpp

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

August 18, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts

peppe wrote:

It does NOT bypass WinMain.

https://qt.gitorious.org/qt/qt/blobs/master/src/winmain/qtmain_win.cpp

Which means, you link the library qmain, which implements WinMain for you and then calls your custom main.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

August 18, 2011

mehoggan mehoggan
Lab Rat
13 posts

Okay thanks for the input. So this behaves “somewhat” like MFC, in the since that the message pump and the WinMain site inside a DLL. Thank you for the clarification.

 Signature 

Matthew Hoggan
http://matthewh.me
“It’s not wise to violate rules until you know how to observe them.” (t.s. eliot)

August 18, 2011

Gerolf Gerolf
Area 51 Engineer
3213 posts
mehoggan wrote:
Okay thanks for the input. So this behaves “somewhat” like MFC, in the since that the message pump and the WinMain site inside a DLL. Thank you for the clarification.

yep
The message pump is your qApp->exec() call.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

 
  ‹‹ JavaScript override      [SOLVED] connect to the server message ››

You must log in to post a reply. Not a member yet? Register here!