PDA

View Full Version : Convert Windows "HWND" to QWidget* pointer?



gerome69
19th September 2006, 09:24
Hi together,
I wrote a screen saver for windows using QT. Everything works fine, but not the preview.

From the commandline I get a parameter like "/p:345678". The number incidicate the WId of the parent window, the "mini monitor" in the windows build-in dialog for setting up the screensaver.

So I tried:

QWidget *parent=QWidget::find((HWND)WId);

Then for debugging purpose:
if (parent==NULL)
QMessageBox::information(NULL,"","No parent "+QString::number(WId));
else QMessageBox::information(NULL,"","Parent found "+QString::number(WId));

Then create the widget:
QWidget *miniMonitor=new QWidget(parent);

But it does not work. parent ist always NULL. Althought the WId is correct, I can't get a pointer to the parent window to create a child window.

In a windows API example they do something like:
if( strncmp( lpszCmdLine, "/p", 2 ) == 0 )
{
// preview screen
lpszCmdLine += 3;
HWND hParentWnd = (HWND)strtoul( lpszCmdLine, '\0', 10 );
GetClientRect( hParentWnd, &rc );

hwndMain = CreateWindow("MainWndClass", "myScreenSaver",
WS_CHILD, rc.left, rc.top, rc.right, rc.bottom, hParentWnd, NULL, hinst, NULL);
}

How to do this in QT?

Thanks for tips, Bernd

Eldritch
19th September 2006, 16:42
Unless the parent window is also a QWidget in your QApplication, QWidget::find() won't work.

I suspect what you may need to do in this case is use ::SetParent() directly on the wID of your own QWidget after you create it.

gerome69
20th September 2006, 11:42
Unless the parent window is also a QWidget in your QApplication, QWidget::find() won't work.

I suspect what you may need to do in this case is use ::SetParent() directly on the wID of your own QWidget after you create it.

Thank you for the tip.
But looking in the documentation:
void QWidget::setParent ( QWidget * parent )

So setParent needs a pointer to a QWidget. There is no setParent(WId) defined :-(

And if my try to do:
QWidget *parent=QWidget::find(WId); fails although WId is definetly a valid HWND, then setParent does not help me. :-(

Greetings from Berlin, Bernd

jpn
20th September 2006, 13:10
He was talking about WinAPI function SetParent(HWND, HWND) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/setparent.asp) declared in windows.h.

gerome69
20th September 2006, 14:03
He was talking about WinAPI function SetParent(HWND, HWND) (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/setparent.asp) declared in windows.h.

Thanks, that's working fine, Bernd