PDA

View Full Version : Converting from Qt's WId to Windows HWND?



ComServant
17th December 2012, 00:31
What's the proper method of converting the result from QWidget::winID(), to a HWND__*?

QWidget's WId is defined as:

typedef QIntegerForSizeof<void*>::Unsigned quintptr;
typedef QT_PREPEND_NAMESPACE(quintptr) WId;

Currently, I'm just doing:

HWND handle = (HWND)this->windID();

Is that the correct method?

wysota
17th December 2012, 00:36
Does it work?

ComServant
27th December 2012, 16:00
It appears to, but I've programmed long enough to know that things that "appear" to work are not always correct. Who knows? Maybe my cast truncates the int in some way, and though it happens coincidentally to work on my machine, it may completely fail on someone else's. =)

I just switched from Qt 4.8 to Qt 5.0, and am dealing with a number of minor problems in my not-so-simple application, so I'm chipping away piece by piece to get everything working again properly.

One of the possible problem sources is getting a HWND for a child-widget. If I am only guessing that "HWND handle = (HWND)this->windID();" is the right solution, I want to get it confirmed as correct to eliminate it as a problem suspect. I read the documentation for the winID() function, and I walked through a few of the related Qt headers, but I don't know for sure if this is the proper usage.

Do you or anyone know how it works under the hood, and can confirm or deny that this is the right way to get a HWND? A sentence or so could also be added to the documentation.

In Qt 4.8 it worked without needing to explitily cast. In Qt 5, I think the function name changed, and the return result requires a cast. The window is a child widget of another window.

Should I be using effectiveWinID()? I'm rather put off by the remark: "Note: We recommend that you do not store this value as it is likely to change at run-time."

I have a main window, with child widgets with Qt-related buttons and things. In the center of the main window is a child widget that is drawn by a different API (SFML). I want to give this non-Qt API the central child widget's HWND so it can draw on it properly. Am I supposed to be watching for QEvent::WinIdChange? How to I process that on Qt's side? Am I supposed to use effectiveWInID() or just winID(), and what is the official Qt-approved way to convert it to HWND?

amleto
27th December 2012, 19:26
should always prefer c++ casts over c casts.