问题
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;
}
回答1:
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.
来源:https://stackoverflow.com/questions/30384037/qts-nativeevent-is-never-called