我正在尝试在我的 Qt 应用程序中接收 Windows 消息。我首先从 winEvent()
函数开始,但它从未被调用,我在 Qt 5.4 中了解到建议使用 nativeEvent()
但它也从未被调用?以下是我的代码,它是简单的应用程序,我只想捕获 WM_PAINT 等消息以及插入 USB 设备时的系统消息。
//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);
};
//主窗口.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;
}
最佳答案
你的nativeEvent方法签名是错误的,它应该是:
bool nativeEvent(const QByteArray & eventType, void * message, long * result);
将 Q_DECL_OVERRIDE(或 C++11 中的 override 关键字)添加到方法声明中以捕获这些内容非常有用。
关于c++ - Qt 的 nativeEvent() 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30384037/