2

I am trying to recieve windows messages in my Qt App. I first started with winEvent() function but it was never called and I learned in Qt 5.4 it is recommended to use nativeEvent()however it is also never called as well? The following is my code, it is bare bone application, I just want to catch messages like WM_PAINT and also system message when USB device is plugged in.

// mainwindow.h

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

    bool nativeEvent(QByteArray & eventType, void * message, long * result);
};

// mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

bool MainWindow::nativeEvent(QByteArray & eventType, void * message, long * result)
{
    MSG *msg = static_cast< MSG * >( message );
    // TODO: filter out or modify msg struct here
    // ...

    qDebug() << "Message recieved"; // it never comes here, never breaks in this function with debugger

    return false;
}
p.i.g.
  • 2,815
  • 2
  • 24
  • 41
zar
  • 11,361
  • 14
  • 96
  • 178
  • For insight into how to prevent such errors in the future, see [this answer](http://stackoverflow.com/questions/3887674/is-there-a-way-to-flag-at-compile-time-overriden-methods-whose-signatures-do). For Qt, you should use Q_DECL_OVERRIDE at the end of the method declaration - this will use the correct platform-specific keywords. – Kuba hasn't forgotten Monica May 22 '15 at 13:00

1 Answers1

3

Your method signature for nativeEvent is wrong, it should be:

bool nativeEvent(const QByteArray & eventType, void * message, long * result);

It's useful to add the Q_DECL_OVERRIDE (or override keyword in C++11) to the method declaration to catch these.

Hamish Moffatt
  • 826
  • 4
  • 12