Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Unknown method parameter type: QEvent* ?



  • Hello,

    testing QEvent::WinEventAct
    I did :
    applicationdata.h

    #ifndef APPLICATIONDATA_H
    #define APPLICATIONDATA_H
    
    #include <QObject>
    #include <QEvent>
    
    class ApplicationData : public QObject
    {
        Q_OBJECT
    
    public:
        Q_INVOKABLE QString wineventFilter(QEvent *event) const
        {
          if (event->type() == QEvent::WinEventAct)
          {
            return "true";
          }
          return "false";
        }
    };
    
    #endif // APPLICATIONDATA_H
    

    main.cpp

    #include <QGuiApplication>
    #include <QQmlApplicationEngine>
    #include <QQmlContext>
    #include <applicationdata.h>
    
    
    int main(int argc, char *argv[]) {
    
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
    
        ApplicationData data;
        engine.rootContext()->setContextProperty("applicationData", &data);
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        return app.exec();
    }
    

    main.qml

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
    
        Item{
            id:item
           Text { id:nb ;text: applicationData.wineventFilter()}
        }
    }
    

    but I received error Error: Unknown method parameter type: QEvent*

    What mean this error? What I did wrong?



  • Hi, QEvent is not derived from QObject and is not registered with Qt's meta type system.
    Therefore QEvent * is completely not accessible with the meta type system and QML engine don't know how to deal with it.

    Here's the solution I knew about:

    • If you need to access these events' properties with QML scripts, wrap the QEvent or QEvent * with a QObject deriving class.
    • If you only need to pass them through C++ > QML > C++, wrap the QEvent * as gadget type:
    struct QmlEvent {
       Q_GADGET
       QEvent *payload;
    };
    
    Q_DECLARE_METATYPE(QmlEvent)
    


  • @GPBeta OK, as I understand, your solution is to work in QML with the event.

    My goal is not this one, I want just return the String "true" in c++ if an windows event is detected and after that, with context, I will send this String to Text in QML.

    I'm just trying to receive usb driver path to QML, so I'm unable to use QDeviceWatcher, that, I will try to run .bat with windows event and system("H:/test.bat") to scan connected devices and read the file to get the path.

    It's probably crazy to do that like that but I don't found other solution...



  • @filipdns So you probably need a Q_PROPERTY infrastructure rather than the Q_INVOKABLE like:

    applicationdata.h

    class ApplicationData : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(bool winEventAct READ winEventAct NOTIFY winEventActChanged FINAL)
    
    public:
    
        bool winEventAct() const { return m_winEventAct; }
    
        bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE
        {
            Q_UNUSED(object);
    
            const bool isWinEventAct = event->type() == QEvent::WinEventAct;
    
            if (m_winEventAct != isWinEventAct) {
                m_winEventAct = isWinEventAct;
                emit winEventActChanged();
            }
    
            return false;
        }
    
    Q_SIGNALS:
    
        void winEventActChanged();
    
    private:
    
        bool m_winEventAct;
    }
    

    main.cpp

    int main(int argc, char *argv[]) {
        // ...
        app.installEventFilter(&data);
        // ...
    }
    

    main.qml

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        // ...
        readonly property bool winEventAct: applicationData.winEventAct
    }
    


  • @GPBeta I'm very sorry to disturb you again for that but with provided code I receive errors..
    0_1536519894505_error.png



  • @filipdns
    First, let me remind you, as a matter of courtesy, that it is good form to vote up the answers of
    people who help you, especially if they literally do the work for you, like @GPBeta did.

    Having said that, you should really look at the warning/error descriptions that the compiler/Qt Creator
    provides to you. Otherwise, what's the point?

    virtual function 'eventFilter' has a different return type

    When you see a warning like this, you must look up the function's definition:
    http://doc.qt.io/qt-5/qobject.html#eventFilter
    If you would have done that, you would have found out for yourself that the signature is:

    bool eventFilter(...)
    

    Then, since it has a return type other than void, you must also return a bool, which your current function is not currently doing. Otherwise you will get this warning:

    control reaches end of non-void function
    

    In your case, since you are not actually handling the event, return false.

    return false;
    

    If the warning concerning the unused parameter bothers you, you can add

    Q_UNUSED(object)
    

    at the start of your function's body (no semicolon needed at the end).

    Finally, look at the last warning concerning the signal:

    C++ requires a type specifier ...

    Declare the signal with a return specifier, here void

    void winEventActChanged();
    

    Good luck.



  • @Diracsbracket said in Unknown method parameter type: QEvent* ?:
    Hello Diracsbracket , thank you very much for theses explanations.

    Regarding voting, I thought we had to vote only for answers that worked 100% to avoid others from using incorrect answers, please excuse me if I was not courteous it was not my intention.



  • @filipdns said in Unknown method parameter type: QEvent* ?:

    Regarding voting, I thought we had to vote only for answers that worked 100% to avoid others from using incorrect answers

    I thought @GPBeta's answer was a nice one, despite the small omissions, probably because he/she just wrote it from memory ^^;

    As for the voting, no apologies needed of course. When I joined this forum, I wasn't even aware at first that there was such a thing, given that it looks so very small on my screen. I only started voting myself after having been made aware of that by other people, and I find it's a nice thing to do.

    Cheers!



  • @Diracsbracket Thank you for the fixes and completed explanation.
    Sorry for the late response, I just woke up and have corrected the mistakes in my previous reply so that it won't break the compiler any more.



  • @GPBeta
    Thank you very much for your help, I appreciate the time given to try to help me !!!

    Now, I do not have any great mistakes !! unfortunately, the result is "false" and is always "false" regardless of my action on windows, inserting usb keys or opening windows or other. Maybe winEvent does not work as I thought



  • @filipdns said in Unknown method parameter type: QEvent* ?:

    Maybe winEvent does not work as I thought

    To have an idea of which types of events are reported, you can do:

    bool eventFilter(QObject *object, QEvent *event) Q_DECL_OVERRIDE
    {
            Q_UNUSED(object);
            qDebug() << event->type();
    }
    

    It does not report USB device state changes:
    http://doc.qt.io/qt-5/qevent.html



  • @Diracsbracket thanks a lot, too bad for usb event ;-)


Log in to reply