Qt's nativeEvent() is never called

非 Y 不嫁゛ 提交于 2020-01-16 18:06:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!