PDA

View Full Version : [Solved] QMainWindow subclass not receiving winEvents unless QMessageBox is called



fruchtquark
25th July 2012, 10:10
Hello all,

I'm new to Qt (4.8.1 for Desktop + MSVC2010) and also just starting to get a handle on C++, and I'm trying to interact with a 3rd party driver DLL that sends Events to a HWDN (which I attempt to set via MainWindow::effectiveWinId() ). This has been working, though apparently only by chance, as inside my MainWindow, before setting said HWND, I called created a QMessageBox.
My MainWindow is a regular UI-Designer subclassed QMainWindow:


namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
virtual bool winEvent( MSG* message, long* result );
~MainWindow();

private:
Ui::MainWindow *ui;
....
void installmessages();

private slots:
void on_freezeButton_clicked();

};


The code in MainWindow::installmessages();


qDebug() << "before";
qDebug() << MainWindow::effectiveWinId() ;

// *
qDebug() << "messagebox";
QMessageBox::information( this,
tr("Messagebox title"),
tr("WinID %1").arg( (long) MainWindow::effectiveWinId() )
);

// */
qDebug() << "after";
qDebug() << MainWindow::effectiveWinId() ;

....

[call to proprietary library with MainWindow::effectiveWinId() as parameter]




results in this output



before
0x0
messagebox [the output is "WinID 0"]
after
0x17046a


So with message box, my program works since I get a valid ID and thus receive messages.
Why is the WinID valid only when the MessageBox is run?

high_flyer
25th July 2012, 11:00
in which method are your qDebug() outputs?
And show the rest, the code (where the debug outputs are) only shows that you are outputing debug messages - show the rest of the code there.

fruchtquark
25th July 2012, 12:03
Apparently windows / widgets don't get a system window ID by default and are called non-native or alien.
While setAttribute(Qt::WA_NativeWindow); did not have an effect, winId did: http://qt-project.org/doc/qt-4.8/qwidget.html#winId

I'm not sure if this is a workaround or a permanent solution, so any comments are still welcome..