Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Unknown method parameter type: QEvent* ?
Forum Updated to NodeBB v4.3 + New Features

Unknown method parameter type: QEvent* ?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
12 Posts 3 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    filipdns
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • GPBetaG Offline
      GPBetaG Offline
      GPBeta
      wrote on last edited by GPBeta
      #2

      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)
      

      \BAKA BAKA/

      F 1 Reply Last reply
      2
      • GPBetaG GPBeta

        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)
        
        F Offline
        F Offline
        filipdns
        wrote on last edited by
        #3

        @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...

        GPBetaG 1 Reply Last reply
        0
        • F filipdns

          @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...

          GPBetaG Offline
          GPBetaG Offline
          GPBeta
          wrote on last edited by GPBeta
          #4

          @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
          }
          

          \BAKA BAKA/

          F 1 Reply Last reply
          4
          • GPBetaG GPBeta

            @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
            }
            
            F Offline
            F Offline
            filipdns
            wrote on last edited by filipdns
            #5

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

            DiracsbracketD 1 Reply Last reply
            0
            • F filipdns

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

              DiracsbracketD Offline
              DiracsbracketD Offline
              Diracsbracket
              wrote on last edited by Diracsbracket
              #6

              @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.

              F 1 Reply Last reply
              4
              • DiracsbracketD Diracsbracket

                @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.

                F Offline
                F Offline
                filipdns
                wrote on last edited by
                #7

                @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.

                DiracsbracketD 1 Reply Last reply
                0
                • F filipdns

                  @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.

                  DiracsbracketD Offline
                  DiracsbracketD Offline
                  Diracsbracket
                  wrote on last edited by Diracsbracket
                  #8

                  @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!

                  GPBetaG 1 Reply Last reply
                  1
                  • DiracsbracketD Diracsbracket

                    @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!

                    GPBetaG Offline
                    GPBetaG Offline
                    GPBeta
                    wrote on last edited by GPBeta
                    #9

                    @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.

                    \BAKA BAKA/

                    F 1 Reply Last reply
                    1
                    • GPBetaG GPBeta

                      @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.

                      F Offline
                      F Offline
                      filipdns
                      wrote on last edited by
                      #10

                      @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

                      DiracsbracketD 1 Reply Last reply
                      0
                      • F filipdns

                        @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

                        DiracsbracketD Offline
                        DiracsbracketD Offline
                        Diracsbracket
                        wrote on last edited by
                        #11

                        @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

                        F 1 Reply Last reply
                        2
                        • DiracsbracketD Diracsbracket

                          @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

                          F Offline
                          F Offline
                          filipdns
                          wrote on last edited by
                          #12

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

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved