Qt widgets in Win32
This examples shows how to use the QWinWidget class to use Qt widgets inside a native Win32 user interface.
The Window procedure for the native Win32 window implements a message handlers for left and right mouse button clicks.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_LBUTTONUP: { QWinWidget w( hWnd, 0, 0 ); w.showCentered(); QMessageBox mb( "Qt on Win32 - modal", "Is this dialog modal?", QMessageBox::NoIcon, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape, QMessageBox::NoButton, &w ); int result = mb.exec(); } break;When the left button is clicked a modal message box is opened. The QWinWidget class is used to provide a bridge between the Win32 window and the QMessageBox, and ensures that the Win32 window is modally blocked by the message box.
case WM_RBUTTONUP: { QWinWidget *w = new QWinWidget( hWnd, 0, 0 ); w->showCentered(); QMessageBox *mb = new QMessageBox( "Qt on Win32 - modeless", "Is this dialog modal?", QMessageBox::NoIcon, QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape, QMessageBox::NoButton, w, 0, FALSE, Qt::WDestructiveClose ); mb->show(); } break; case WM_KEYDOWN: if (wParam != VK_TAB) return DefWindowProc(hWnd, message, wParam, lParam); SetFocus(winId); break; case WM_SETFOCUS: { QString str("Got focus"); QWidget *widget = QWidget::find(HWND(wParam)); if (widget) str += QString(" from %1 (%2)").arg(widget->name()).arg(widget->className()); str += "\n"; OutputDebugStringA(str.latin1()); } break; case WM_KILLFOCUS: { QString str("Lost focus"); QWidget *widget = QWidget::find(HWND(wParam)); if (widget) str += QString(" to %1 (%2)").arg(widget->name()).arg(widget->className()); str += "\n"; OutputDebugStringA(str.latin1()); } break;When the right button is clicked a modeless message box is opened. The QWinWidget class is used again to provide proper placement and stacking of the message box. Note that this time both the QWinWidget and the QMessageBox are created on the heap using operator new. Since the WDestructiveClose flag is passed to the QMessageBox constructor it is however not necessary to delete either of those objects.
case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, message, wParam, lParam); } return 0; }When the Win32 window is closed the application is terminated. Unhandled messages are processed by the default window procedure.
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = NULL; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = L"qtest"; wcex.hIconSm = NULL; ATOM windowClass = RegisterClassEx(&wcex); HWND hWnd = CreateWindow((TCHAR*)windowClass, L"Windows Migration Framework Example", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, 0, 0, hInstance, 0); if (!hWnd) return FALSE;The application's entry point function wWinMain registers a window class and creates a window using the CreateWindow API. Note that the UNICODE versions of all Win32 APIs are used.
int argc = 0; QApplication a( argc, 0 );Before the Qt based user interface can be created a QApplication object must exist. The translation of the command line arguments is omitted for brevity.
QWinWidget win( hWnd ); winId = win.winId(); QHBoxLayout hbox( &win ); hbox.setSpacing(5); hbox.setMargin(11); QPushButton *pb = new QPushButton( "Qt command button", &win, "pb" ); hbox.addWidget( pb ); QLabel *label = new QLabel( "Some label", &win, "label" ); hbox.addWidget( label ); QLineEdit *le1 = new QLineEdit(&win, "le1"); hbox.addWidget( le1 ); QLineEdit *le2 = new QLineEdit(&win, "le2"); hbox.addWidget( le2 ); QLineEdit *le3 = new QLineEdit(&win, "le3"); hbox.addWidget( le3 ); win.move( 0, 0 );The QWinWidget class is once again used as a bridge between the Win32 window and a Qt widget, QPushButton this time. Since the QWinWidget is a proper QWidget it can be layouted and positioned like any other QWidget.
QMenuData *menu = win.menuBar(); if ( menu ) { QPopupMenu *action = new QPopupMenu( &win ); action->insertItem( "&Click", pb, SLOT(animateClick()) ); menu->insertItem( "&Action", action ); QPopupMenu *file = new QPopupMenu( &win ); file->insertItem( "&Quit", &a, SLOT(quit()) ); menu->insertItem( "&File", file ); }The menuBar() function returns a QMenuData object that wraps the native menu of the Win32 window. The QMenuData API can then be used to insert QPopupMenus into that native menu bar. Connections of items in those popupmenus to Qt slots work like in Qt.
win.show(); ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); return a.exec(); }Finally the Win32 user interface is displayed, and control is passed to the QApplication event loop. Since Windows doesn't show child windows recoursively the Qt widget has to be shown explicitly.
See also Windows Migration - Win32 Examples.