Skip to content

Commit 1b27736

Browse files
author
Andy Shaw
committed
Make the QtWinMigrate solution work with Qt 5.0
Change-Id: Ia4957fc75e89665c8b187a7932bd6b41e9230ee7 Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
1 parent 49d3fe9 commit 1b27736

File tree

9 files changed

+116
-32
lines changed

9 files changed

+116
-32
lines changed

qtwinmigrate/examples/winhost/main.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@
3838
**
3939
****************************************************************************/
4040

41-
#include <QtGui>
42-
41+
#include <QApplication>
42+
#include <QLineEdit>
43+
#include <QMainWindow>
44+
#include <QMenuBar>
45+
#include <QMessageBox>
46+
#include <QStatusBar>
47+
#include <QVBoxLayout>
4348
#include <qwinhost.h>
4449

4550
#include <windows.h>
@@ -48,7 +53,7 @@ class HostWindow : public QWinHost
4853
{
4954
Q_OBJECT
5055
public:
51-
HostWindow(QWidget *parent = 0, Qt::WFlags f = 0)
56+
HostWindow(QWidget *parent = 0, Qt::WindowFlags f = 0)
5257
: QWinHost(parent, f)
5358
{
5459
setFocusPolicy(Qt::StrongFocus);
@@ -93,7 +98,7 @@ public slots:
9398
protected:
9499
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
95100
{
96-
QWidget *widget = QWidget::find(GetParent(hWnd));
101+
QWidget *widget = QWidget::find((WId)GetParent(hWnd));
97102
HostWindow *window = qobject_cast<HostWindow*>(widget);
98103

99104
if (window) switch (message) {

qtwinmigrate/examples/winwidget/main.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@
3838
**
3939
****************************************************************************/
4040

41-
#include <QtGui>
41+
#include <QHBoxLayout>
42+
#include <QLabel>
43+
#include <QLineEdit>
44+
#include <QMessageBox>
45+
#include <QPushButton>
4246
#include <qwinwidget.h>
4347

4448
#include <windows.h>
@@ -91,7 +95,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
9195
case WM_SETFOCUS:
9296
{
9397
QString str("Got focus");
94-
QWidget *widget = QWidget::find(HWND(wParam));
98+
QWidget *widget = QWidget::find((WId)HWND(wParam));
9599
if (widget)
96100
str += QString(" from %1 (%2)").arg(widget->objectName()).arg(widget->metaObject()->className());
97101
str += "\n";
@@ -102,7 +106,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
102106
case WM_KILLFOCUS:
103107
{
104108
QString str("Lost focus");
105-
QWidget *widget = QWidget::find(HWND(wParam));
109+
QWidget *widget = QWidget::find((WId)HWND(wParam));
106110
if (widget)
107111
str += QString(" to %1 (%2)").arg(widget->objectName()).arg(widget->metaObject()->className());
108112
str += "\n";
@@ -153,7 +157,7 @@ int APIENTRY wWinMain(HINSTANCE hInstance,
153157
QApplication a(argc, 0);
154158

155159
QWinWidget win(hWnd);
156-
winId = win.winId();
160+
winId = (HWND)win.winId();
157161
QHBoxLayout hbox(&win);
158162
hbox.setSpacing(5);
159163
hbox.setMargin(0);

qtwinmigrate/src/qmfcapp.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ int QMfcApp::mfc_argc = 0;
6868

6969
#if QT_VERSION >= 0x050000
7070
#define QT_WA(unicode, ansi) unicode
71+
72+
QMfcAppEventFilter::QMfcAppEventFilter() : QAbstractNativeEventFilter()
73+
{
74+
}
75+
76+
bool QMfcAppEventFilter::nativeEventFilter(const QByteArray &, void *message, long *result)
77+
{
78+
return static_cast<QMfcApp*>(qApp)->winEventFilter((MSG*)message, result);
79+
}
7180
#endif
7281

7382
/*! \class QMfcApp qmfcapp.h
@@ -200,6 +209,10 @@ bool QMfcApp::pluginInstance(Qt::HANDLE plugin)
200209
return TRUE;
201210
}
202211

212+
#if QT_VERSION >= 0x050000
213+
Q_GLOBAL_STATIC(QMfcAppEventFilter, qmfcEventFilter);
214+
#endif
215+
203216
#ifdef QTWINMIGRATE_WITHMFC
204217
/*!
205218
Runs the event loop for both Qt and the MFC application object \a
@@ -349,11 +362,21 @@ QMfcApp::QMfcApp(CWinApp *mfcApp, int &argc, char **argv)
349362
: QApplication(argc, argv), idleCount(0), doIdle(FALSE)
350363
{
351364
mfc_app = mfcApp;
365+
#if QT_VERSION >= 0x050000
366+
QAbstractEventDispatcher::instance()->installNativeEventFilter(qmfcEventFilter());
367+
#else
352368
QAbstractEventDispatcher::instance()->setEventFilter(qmfc_eventFilter);
369+
#endif
353370
setQuitOnLastWindowClosed(false);
354371
}
355372
#endif
356373

374+
QMfcApp::QMfcApp(int &argc, char **argv) : QApplication(argc, argv)
375+
{
376+
#if QT_VERSION >= 0x050000
377+
QAbstractEventDispatcher::instance()->installNativeEventFilter(qmfcEventFilter());
378+
#endif
379+
}
357380
/*!
358381
Destroys the QMfcApp object, freeing all allocated resources.
359382
*/
@@ -388,10 +411,10 @@ bool QMfcApp::winEventFilter(MSG *msg, long *result)
388411

389412
recursion = true;
390413

391-
QWidget *widget = QWidget::find(msg->hwnd);
414+
QWidget *widget = QWidget::find((WId)msg->hwnd);
392415
HWND toplevel = 0;
393416
if (widget) {
394-
HWND parent = widget->winId();
417+
HWND parent = (HWND)widget->winId();
395418
while(parent) {
396419
toplevel = parent;
397420
parent = GetParent(parent);
@@ -432,5 +455,10 @@ bool QMfcApp::winEventFilter(MSG *msg, long *result)
432455
#endif
433456

434457
recursion = false;
458+
#if QT_VERSION < 0x050000
435459
return QApplication::winEventFilter(msg, result);
460+
#else
461+
Q_UNUSED(result);
462+
return false;
463+
#endif
436464
}

qtwinmigrate/src/qmfcapp.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ class CWinApp;
6767
# define QT_QTWINMIGRATE_EXPORT
6868
#endif
6969

70+
#if QT_VERSION >= 0x050000
71+
#include <QAbstractNativeEventFilter>
72+
73+
class QT_QTWINMIGRATE_EXPORT QMfcAppEventFilter : public QAbstractNativeEventFilter
74+
{
75+
public:
76+
QMfcAppEventFilter();
77+
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result);
78+
};
79+
#endif
80+
7081
class QT_QTWINMIGRATE_EXPORT QMfcApp : public QApplication
7182
{
7283
public:
@@ -77,6 +88,7 @@ class QT_QTWINMIGRATE_EXPORT QMfcApp : public QApplication
7788
static QApplication *instance(CWinApp *mfcApp);
7889
QMfcApp(CWinApp *mfcApp, int &argc, char **argv);
7990
#endif
91+
QMfcApp(int &argc, char **argv);
8092
~QMfcApp();
8193

8294
bool winEventFilter(MSG *msg, long *result);

qtwinmigrate/src/qtwinmigrate.pri

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ win32 {
2020
qtwinmigrate-buildlib:DEFINES += QT_QTWINMIGRATE_EXPORT
2121
else:qtwinmigrate-uselib:DEFINES += QT_QTWINMIGRATE_IMPORT
2222
}
23+
contains(QT_MAJOR_VERSION, 5): QT += widgets gui-private

qtwinmigrate/src/qwinhost.cpp

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
after the native window has been created, i.e. do not call
8585
QWidget::setParent or move the QWinHost into a different layout.
8686
*/
87-
QWinHost::QWinHost(QWidget *parent, Qt::WFlags f)
87+
QWinHost::QWinHost(QWidget *parent, Qt::WindowFlags f)
8888
: QWidget(parent, f), wndproc(0),own_hwnd(false), hwnd(0)
8989
{
9090
setAttribute(Qt::WA_NoBackground);
@@ -153,12 +153,12 @@ void QWinHost::fixParent()
153153
hwnd = 0;
154154
return;
155155
}
156-
if (::GetParent(hwnd) == winId())
156+
if (::GetParent(hwnd) == (HWND)winId())
157157
return;
158158
long style = GetWindowLong(hwnd, GWL_STYLE);
159159
if (style & WS_OVERLAPPED)
160160
return;
161-
::SetParent(hwnd, winId());
161+
::SetParent(hwnd, (HWND)winId());
162162
}
163163

164164
/*!
@@ -201,7 +201,7 @@ void *getWindowProc(QWinHost *host)
201201

202202
LRESULT CALLBACK WinHostProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
203203
{
204-
QWinHost *widget = qobject_cast<QWinHost*>(QWidget::find(::GetParent(hwnd)));
204+
QWinHost *widget = qobject_cast<QWinHost*>(QWidget::find((WId)::GetParent(hwnd)));
205205
WNDPROC oldproc = (WNDPROC)getWindowProc(widget);
206206
if (widget) {
207207
switch(msg) {
@@ -214,18 +214,18 @@ LRESULT CALLBACK WinHostProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
214214
case WM_SYSKEYDOWN:
215215
case WM_SYSKEYUP:
216216
QT_WA({
217-
SendMessage(widget->winId(), msg, wParam, lParam);
217+
SendMessage((HWND)widget->winId(), msg, wParam, lParam);
218218
}, {
219-
SendMessageA(widget->winId(), msg, wParam, lParam);
219+
SendMessageA((HWND)widget->winId(), msg, wParam, lParam);
220220
})
221221
break;
222222

223223
case WM_KEYDOWN:
224224
if (wParam == VK_TAB) {
225225
QT_WA({
226-
SendMessage(widget->winId(), msg, wParam, lParam);
226+
SendMessage((HWND)widget->winId(), msg, wParam, lParam);
227227
}, {
228-
SendMessageA(widget->winId(), msg, wParam, lParam);
228+
SendMessageA((HWND)widget->winId(), msg, wParam, lParam);
229229
})
230230
}
231231
break;
@@ -254,11 +254,11 @@ bool QWinHost::event(QEvent *e)
254254
switch(e->type()) {
255255
case QEvent::Polish:
256256
if (!hwnd) {
257-
hwnd = createWindow(winId(), qWinAppInst());
257+
hwnd = createWindow((HWND)winId(), qWinAppInst());
258258
fixParent();
259259
own_hwnd = hwnd != 0;
260260
}
261-
if (hwnd && !wndproc && GetParent(hwnd) == winId()) {
261+
if (hwnd && !wndproc && GetParent(hwnd) == (HWND)winId()) {
262262
#if defined(GWLP_WNDPROC)
263263
QT_WA({
264264
wndproc = (void*)GetWindowLongPtr(hwnd, GWLP_WNDPROC);
@@ -335,8 +335,15 @@ void QWinHost::resizeEvent(QResizeEvent *e)
335335
/*!
336336
\reimp
337337
*/
338+
#if QT_VERSION >= 0x050000
339+
bool QWinHost::nativeEvent(const QByteArray &eventType, void *message, long *result)
340+
#else
338341
bool QWinHost::winEvent(MSG *msg, long *result)
342+
#endif
339343
{
344+
#if QT_VERSION >= 0x050000
345+
MSG *msg = (MSG *)message;
346+
#endif
340347
switch (msg->message)
341348
{
342349
case WM_SETFOCUS:
@@ -347,6 +354,9 @@ bool QWinHost::winEvent(MSG *msg, long *result)
347354
default:
348355
break;
349356
}
350-
357+
#if QT_VERSION >= 0x050000
358+
return QWidget::nativeEvent(eventType, message, result);
359+
#else
351360
return QWidget::winEvent(msg, result);
361+
#endif
352362
}

qtwinmigrate/src/qwinhost.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class QT_QTWINMIGRATE_EXPORT QWinHost : public QWidget
6666
{
6767
Q_OBJECT
6868
public:
69-
QWinHost(QWidget *parent = 0, Qt::WFlags f = 0);
69+
QWinHost(QWidget *parent = 0, Qt::WindowFlags f = 0);
7070
~QWinHost();
7171

7272
void setWindow(HWND);
@@ -80,7 +80,11 @@ class QT_QTWINMIGRATE_EXPORT QWinHost : public QWidget
8080
void focusInEvent(QFocusEvent*);
8181
void resizeEvent(QResizeEvent*);
8282

83+
#if QT_VERSION >= 0x050000
84+
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
85+
#else
8386
bool winEvent(MSG *msg, long *result);
87+
#endif
8488

8589
private:
8690
void fixParent();

qtwinmigrate/src/qwinwidget.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
#include <qt_windows.h>
6262

6363
#if QT_VERSION >= 0x050000
64+
#include <QWindow>
65+
#include <qpa/qplatformnativeinterface.h>
6466
#define QT_WA(unicode, ansi) unicode
6567
#endif
6668

@@ -88,7 +90,7 @@
8890
the native Win32 parent. If a \a parent is provided the object is
8991
owned by that QObject. \a f is passed on to the QWidget constructor.
9092
*/
91-
QWinWidget::QWinWidget(HWND hParentWnd, QObject *parent, Qt::WFlags f)
93+
QWinWidget::QWinWidget(HWND hParentWnd, QObject *parent, Qt::WindowFlags f)
9294
: QWidget(0, f), hParent(hParentWnd), prevFocus(0), reenable_parent(false)
9395
{
9496
if (parent)
@@ -105,7 +107,7 @@ QWinWidget::QWinWidget(HWND hParentWnd, QObject *parent, Qt::WFlags f)
105107
MFC window object. If a \a parent is provided the object is owned
106108
by that QObject. \a f is passed on to the QWidget constructor.
107109
*/
108-
QWinWidget::QWinWidget(CWnd *parentWnd, QObject *parent, Qt::WFlags f)
110+
QWinWidget::QWinWidget(CWnd *parentWnd, QObject *parent, Qt::WindowFlags f)
109111
: QWidget(0, f), hParent(parentWnd ? parentWnd->m_hWnd : 0), prevFocus(0), reenable_parent(false)
110112
{
111113
if (parent)
@@ -123,12 +125,20 @@ void QWinWidget::init()
123125
if (hParent) {
124126
// make the widget window style be WS_CHILD so SetParent will work
125127
QT_WA({
126-
SetWindowLong(winId(), GWL_STYLE, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
128+
SetWindowLong((HWND)winId(), GWL_STYLE, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
127129
}, {
128-
SetWindowLongA(winId(), GWL_STYLE, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
130+
SetWindowLongA((HWND)winId(), GWL_STYLE, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
129131
})
130-
SetParent(winId(), hParent);
131-
132+
#if QT_VERSION >= 0x050000
133+
QWindow *window = windowHandle();
134+
window->setProperty("_q_embedded_native_parent_handle", (WId)hParent);
135+
HWND h = static_cast<HWND>(QGuiApplication::platformNativeInterface()->
136+
nativeResourceForWindow("handle", window));
137+
SetParent(h, hParent);
138+
window->setFlags(Qt::FramelessWindowHint);
139+
#else
140+
SetParent(winId(), hParent);
141+
#endif
132142
QEvent e(QEvent::EmbeddingControl);
133143
QApplication::sendEvent(this, &e);
134144
}
@@ -205,7 +215,7 @@ void QWinWidget::show()
205215
*/
206216
void QWinWidget::center()
207217
{
208-
const QWidget *child = qFindChild<QWidget*>(this);
218+
const QWidget *child = findChild<QWidget*>();
209219
if (child && !child->isWindow()) {
210220
qWarning("QWinWidget::center: Call this function only for QWinWidgets with toplevel children");
211221
}
@@ -240,8 +250,15 @@ void QWinWidget::resetFocus()
240250

241251
/*! \reimp
242252
*/
253+
#if QT_VERSION >= 0x050000
254+
bool QWinWidget::nativeEvent(const QByteArray &, void *message, long *)
255+
#else
243256
bool QWinWidget::winEvent(MSG *msg, long *)
257+
#endif
244258
{
259+
#if QT_VERSION >= 0x050000
260+
MSG *msg = (MSG *)message;
261+
#endif
245262
if (msg->message == WM_SETFOCUS) {
246263
Qt::FocusReason reason;
247264
if (::GetKeyState(VK_SHIFT) < 0)

qtwinmigrate/src/qwinwidget.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ class QT_QTWINMIGRATE_EXPORT QWinWidget : public QWidget
6969
{
7070
Q_OBJECT
7171
public:
72-
QWinWidget( HWND hParentWnd, QObject *parent = 0, Qt::WFlags f = 0 );
72+
QWinWidget( HWND hParentWnd, QObject *parent = 0, Qt::WindowFlags f = 0 );
7373
#ifdef QTWINMIGRATE_WITHMFC
74-
QWinWidget( CWnd *parnetWnd, QObject *parent = 0, Qt::WFlags f = 0 );
74+
QWinWidget( CWnd *parnetWnd, QObject *parent = 0, Qt::WindowFlags f = 0 );
7575
#endif
7676
~QWinWidget();
7777

@@ -87,8 +87,11 @@ class QT_QTWINMIGRATE_EXPORT QWinWidget : public QWidget
8787

8888
bool focusNextPrevChild(bool next);
8989
void focusInEvent(QFocusEvent *e);
90-
90+
#if QT_VERSION >= 0x050000
91+
bool nativeEvent(const QByteArray &eventType, void *message, long *result);
92+
#else
9193
bool winEvent(MSG *msg, long *result);
94+
#endif
9295

9396
private:
9497
void init();

0 commit comments

Comments
 (0)