0

In short, I want to use win32Api in Qt5.1.1, And I have a simple example.

I want to pass the widget handle to the WinApi function int MessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType).

The problem in the first parameter (HWND hWnd, ...).

My attempt: (Failed)

WId wind = (HWND)w.winId(); // `w` is the instance of my widget.
MessageBox(wind, "Hello world!", "Message", MB_OK);

How to obtain the widget(window) handle, to passed to MessageBox winapi function ?

Lion King
  • 32,851
  • 25
  • 81
  • 143
  • Why is the message outside of the widget? By having the message outside the widget its possible that the widget is closed when you call `MessageBox`. – andre Oct 15 '13 at 15:17

1 Answers1

1

If your in Qt, all Widgets should have a QWidget::winId() function. Simply call winId() and you'll get the windows handle.

andre
  • 7,018
  • 4
  • 43
  • 75
  • Those are link time errors. The compiler does not know where the definition of `MessageBox` and `MB_OK` are. You'll have to include `#include ` at the top of the file that `MessageBox` is used. – andre Oct 15 '13 at 15:20
  • Seems their is a change from Qt4 which I use to Qt5 which stops the convertion from working. Let me research it for a bit – andre Oct 15 '13 at 15:27
  • @LionKing look at [this](http://stackoverflow.com/a/14500265/942596). It uses `::windowForWidget(widget);` which converts a widget to a `QWindow` and allows you access to the `handle()` method witch is what you want. – andre Oct 15 '13 at 15:29
  • Thanks, but is there a function in qt5 like `winId()` to do that directly? – Lion King Oct 15 '13 at 15:32
  • @LionKing Not that I know of, but try a simple cast `MessageBox((HWND)wind, "Hello world!", "Message", MB_OK);`. This should also work. – andre Oct 15 '13 at 15:36
  • Unfortunately, still appears an error `error: C2664: 'MessageBoxW' : cannot convert parameter 2 from const char [13] to LPCWSTR Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast` – Lion King Oct 15 '13 at 15:39
  • @LionKing Your making me work for the rep aren't you. You have UNICODE mode on. Add `#include ` and replace the message with `MessageBox((HWND)wind, _T("Hello world!"), _T("Message"), MB_OK);` – andre Oct 15 '13 at 15:42
  • Thanks for continued help me, but Unfortunately, still the same last error. – Lion King Oct 15 '13 at 15:47
  • @LionKing Same place or a different place? All string in your code should be wrapped with `_T()` macro. – andre Oct 15 '13 at 15:49
  • Same place, `error: C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [13]' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast` – Lion King Oct 15 '13 at 15:50
  • Sorry I couldn't get her to compile for you. But that's the limit of how much I know, Try to research the error, then ask a new question on it on stack about it. – andre Oct 15 '13 at 15:54
  • For future reference, start the string with an `L` (i.e. `L"Hello, world"`) – Qix - MONICA WAS MISTREATED May 27 '14 at 21:03