Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Problem with the connect method in Qt5
Forum Updated to NodeBB v4.3 + New Features

Problem with the connect method in Qt5

Scheduled Pinned Locked Moved Unsolved General and Desktop
20 Posts 5 Posters 8.4k Views 2 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.
  • Joel BodenmannJ Offline
    Joel BodenmannJ Offline
    Joel Bodenmann
    wrote on last edited by
    #2

    Hi,

    You're missing an ampersand:
    connect(openAction, &QAction::triggered, this, &ViewerImage::open);

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    3
    • S Sewing

      I am trying to get this example to run.
      It calls upon this member function of the MainWindow class which derived from the QMainWindow class.

          QObject::connect(openAction, &QAction::triggered, this, MainWindow::open);
      

      Open is a private function

       private slots:
        void open();
      

      However, I get the following error

      error: invalid use of non-static member function
      QObject::connect(openAction, &QAction::triggered, this, ViewerImage::open);
      ^

      What am I missing here? It is a Qt5 example and I am using Qt5

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #3

      @Sewing Downloaded the source of that example and built it on Windows 7 with Qt 5.9 + MinGW - no problem.
      Are you compiling the source or do you copy/paste the code from the example?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • S Sewing

        I am trying to get this example to run.
        It calls upon this member function of the MainWindow class which derived from the QMainWindow class.

            QObject::connect(openAction, &QAction::triggered, this, MainWindow::open);
        

        Open is a private function

         private slots:
          void open();
        

        However, I get the following error

        error: invalid use of non-static member function
        QObject::connect(openAction, &QAction::triggered, this, ViewerImage::open);
        ^

        What am I missing here? It is a Qt5 example and I am using Qt5

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #4

        @Sewing
        So to be clear you say your source code has:
        QObject::connect(openAction, &QAction::triggered, this, MainWindow::open);

        and the compiler error reports the line as:
        QObject::connect(openAction, &QAction::triggered, this, ViewerImage::open);

        ? And the error relates to the source code you show?

        Joel BodenmannJ 1 Reply Last reply
        0
        • JonBJ JonB

          @Sewing
          So to be clear you say your source code has:
          QObject::connect(openAction, &QAction::triggered, this, MainWindow::open);

          and the compiler error reports the line as:
          QObject::connect(openAction, &QAction::triggered, this, ViewerImage::open);

          ? And the error relates to the source code you show?

          Joel BodenmannJ Offline
          Joel BodenmannJ Offline
          Joel Bodenmann
          wrote on last edited by
          #5

          @JNBarchan I assume that's a simple copy-paste issue from example code vs. his own code. In any case he's definitely missing the ampersand - hence the compiler error regarding the non-static member function.

          Industrial process automation software: https://simulton.com
          Embedded Graphics & GUI library: https://ugfx.io

          jsulmJ JonBJ 2 Replies Last reply
          0
          • Joel BodenmannJ Joel Bodenmann

            @JNBarchan I assume that's a simple copy-paste issue from example code vs. his own code. In any case he's definitely missing the ampersand - hence the compiler error regarding the non-static member function.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #6

            @Joel-Bodenmann No, this isn't an issue, & is optional for function/method pointers. And the error message say that connect isn't static - it does not complain about open().
            It is like this in the example source code and it compiles just fine.
            I think he just forgot something like an include.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • Joel BodenmannJ Joel Bodenmann

              @JNBarchan I assume that's a simple copy-paste issue from example code vs. his own code. In any case he's definitely missing the ampersand - hence the compiler error regarding the non-static member function.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #7

              @Joel-Bodenmann
              So if his code is different from the sample code he copies from, we don't get told that?

              I cannot see what you mean about the missing ampersand, since the sample code does not have it:

              QObject::connect(openAction, &QAction::triggered,
              this, MainWindow::open);

              and @jsulm states he can download and build that without problem.

              1 Reply Last reply
              1
              • Joel BodenmannJ Offline
                Joel BodenmannJ Offline
                Joel Bodenmann
                wrote on last edited by
                #8

                My bad, sorry folks!

                Industrial process automation software: https://simulton.com
                Embedded Graphics & GUI library: https://ugfx.io

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sewing
                  wrote on last edited by Sewing
                  #9

                  thank you guys for your help and apologies for my typo. Adding the ampersand did fix the issue, though I dont get why since function names count as their addresses to my knowledge...

                  this is the new connect syntax right? The old one relied on the SIGNAL and SLOT Macros

                  QObject provides the following overloads

                   static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver,  const char *member, Qt::ConnectionType = Qt::AutoConnection);
                  
                  static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method,
                  Qt::ConnectionType type = Qt::AutoConnection);
                  
                  inline QMetaObject::Connection connect(const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type = Qt::AutoConnection) const;
                  

                  According to QtCreator I use the second one (reference to QMetaMethod), but why I need to use an ampersand then, is beyond me...

                  jsulmJ 1 Reply Last reply
                  0
                  • S Sewing

                    thank you guys for your help and apologies for my typo. Adding the ampersand did fix the issue, though I dont get why since function names count as their addresses to my knowledge...

                    this is the new connect syntax right? The old one relied on the SIGNAL and SLOT Macros

                    QObject provides the following overloads

                     static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver,  const char *member, Qt::ConnectionType = Qt::AutoConnection);
                    
                    static QMetaObject::Connection connect(const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method,
                    Qt::ConnectionType type = Qt::AutoConnection);
                    
                    inline QMetaObject::Connection connect(const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type = Qt::AutoConnection) const;
                    

                    According to QtCreator I use the second one (reference to QMetaMethod), but why I need to use an ampersand then, is beyond me...

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by jsulm
                    #10

                    @Sewing Interesting, here the code compiles without any modifications (without &).
                    What compiler do you use?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    JonBJ kshegunovK 2 Replies Last reply
                    1
                    • jsulmJ jsulm

                      @Sewing Interesting, here the code compiles without any modifications (without &).
                      What compiler do you use?

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #11

                      @jsulm
                      Assuming his code is indeed using ViewerImage::open instead of MainWindow::open, don't we need to his declaration of the former?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sewing
                        wrote on last edited by Sewing
                        #12

                        @ jsulm: gcc 5 and Qt-5.5

                        btw: same problem seems to appear for

                          QMenu* fileMenu = menuBar()->addMenu(tr("&File"));
                        
                          QAction* openAct = fileMenu->addAction(tr("&Open..."), this, &ImageViewer::open);
                        

                        which does not match the only viable addAction overload in qmenu.h

                           QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                        

                        although this example is taken from the official QT Webpage. Am I missing something here, does my Qt Version not match the code or what seems to be the issue?

                        JonBJ 1 Reply Last reply
                        0
                        • S Sewing

                          @ jsulm: gcc 5 and Qt-5.5

                          btw: same problem seems to appear for

                            QMenu* fileMenu = menuBar()->addMenu(tr("&File"));
                          
                            QAction* openAct = fileMenu->addAction(tr("&Open..."), this, &ImageViewer::open);
                          

                          which does not match the only viable addAction overload in qmenu.h

                             QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                          

                          although this example is taken from the official QT Webpage. Am I missing something here, does my Qt Version not match the code or what seems to be the issue?

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by JonB
                          #13

                          @Sewing
                          Is your ImageViewer::open a char? Like I said before, don't we need to know what your definition of ImageViewer::open is before we start figuring how it matches declaration prototypes? OK, maybe I'm being a bit pedantic/dumb, can I assume it's a function?

                          That overload doesn't look right as the "only" one available.... What about:

                          QAction *addAction(const QString &text, const QObject *receiver, PointerToMemberFunction method, const QKeySequence &shortcut = 0)
                          

                          http://doc.qt.io/qt-5/qmenu.html#addAction-4
                          ?

                          For PointerToMemberFunction you might want to view https://stackoverflow.com/questions/29218092/where-is-qt-s-pointertomemberfunction-defined

                          I think you may also need to read https://isocpp.org/wiki/faq/pointers-to-members#fnptr-vs-memfnptr-types to describe Is the type of “pointer-to-member-function” different from “pointer-to-function”?

                          All of this may be why we need to know the exact definition of your ImageViewer::open versus the example's MainWindow::open?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Sewing
                            wrote on last edited by
                            #14

                            thank you for your help guys =)

                            open is a function and its signature looks like

                            void ImageViewer::open()
                            

                            in qmenu.h the overloads I have are

                              QAction *addAction(const QString &text);
                                QAction *addAction(const QIcon &icon, const QString &text);
                                QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                                QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                            

                            I dont get it...

                            Also, what is the difference between addAction and connect?

                            JonBJ S 2 Replies Last reply
                            0
                            • S Sewing

                              thank you for your help guys =)

                              open is a function and its signature looks like

                              void ImageViewer::open()
                              

                              in qmenu.h the overloads I have are

                                QAction *addAction(const QString &text);
                                  QAction *addAction(const QIcon &icon, const QString &text);
                                  QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                                  QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                              

                              I dont get it...

                              Also, what is the difference between addAction and connect?

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by JonB
                              #15

                              @Sewing
                              I don't do C++/header files, but are you sure there aren't some additional overloads, perhaps somewhere away from the ones you show, for addAction()? They don't all have to be next to each other....

                              For addAction vs connect:

                              A menu consists of a list of action items. Actions are added with the addAction(), addActions() and insertAction() functions.
                              the QMenu class contains convenience functions for creating actions suitable for use as menu items.
                              A QAction may contain an icon, menu text, a shortcut, status text, "What's This?" text, and a tooltip

                              QAction *QMenu::addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char *member, const QKeySequence &shortcut = 0)

                              This convenience function creates a new action with an icon and some text and an optional shortcut shortcut. The action's triggered() signal is connected to the member slot of the receiver object. The function adds the newly created action to the menu's list of actions, and returns it.

                              An addAction() wraps up the addition of an item on a menu and the association of the slot with clicking (triggered() signal) that item.

                              1 Reply Last reply
                              0
                              • jsulmJ jsulm

                                @Sewing Interesting, here the code compiles without any modifications (without &).
                                What compiler do you use?

                                kshegunovK Offline
                                kshegunovK Offline
                                kshegunov
                                Moderators
                                wrote on last edited by
                                #16

                                @jsulm said in Problem with the connect method in Qt5:

                                What compiler do you use?

                                g++ requires you to write the ampersand, even though it strictly shouldn't be necessary. This:

                                class A
                                {
                                public:
                                    void open(int) {}
                                };
                                
                                int main(int argc, char *argv[])
                                {
                                    void (A::*ptr)(int) = A::open;
                                    return 0;
                                }
                                

                                generates (g++ 7.2):

                                error: invalid use of non-static member function 'void A::open(int)'
                                    void (A::*ptr)(int) = A::open;
                                                             ^~~~
                                

                                and has been like this for as long as I can remember.

                                Read and abide by the Qt Code of Conduct

                                1 Reply Last reply
                                3
                                • S Sewing

                                  thank you for your help guys =)

                                  open is a function and its signature looks like

                                  void ImageViewer::open()
                                  

                                  in qmenu.h the overloads I have are

                                    QAction *addAction(const QString &text);
                                      QAction *addAction(const QIcon &icon, const QString &text);
                                      QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                                      QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                                  

                                  I dont get it...

                                  Also, what is the difference between addAction and connect?

                                  S Offline
                                  S Offline
                                  Sewing
                                  wrote on last edited by
                                  #17

                                  @Sewing said in Problem with the connect method in Qt5:

                                  thank you for your help guys =)

                                  open is a function and its signature looks like

                                  void ImageViewer::open()
                                  

                                  in qmenu.h the overloads I have are

                                    QAction *addAction(const QString &text);
                                      QAction *addAction(const QIcon &icon, const QString &text);
                                      QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                                      QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                                  

                                  I dont get it...

                                  Also, what is the difference between addAction and connect?

                                  Well I guess I found the issue. I am using Qt 5.5.1 via the system Packages on Xubuntu 16.04. However, the necessary overloaded veersion of addAction were introduced in Qt 5.6 according to the API.

                                  How can I get Qt 5.6 without having to install it myself from Source?
                                  Is there an Update on the system Packages yet which includes this more recent Qt Version?

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • S Sewing

                                    @Sewing said in Problem with the connect method in Qt5:

                                    thank you for your help guys =)

                                    open is a function and its signature looks like

                                    void ImageViewer::open()
                                    

                                    in qmenu.h the overloads I have are

                                      QAction *addAction(const QString &text);
                                        QAction *addAction(const QIcon &icon, const QString &text);
                                        QAction *addAction(const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                                        QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char* member, const QKeySequence &shortcut = 0);
                                    

                                    I dont get it...

                                    Also, what is the difference between addAction and connect?

                                    Well I guess I found the issue. I am using Qt 5.5.1 via the system Packages on Xubuntu 16.04. However, the necessary overloaded veersion of addAction were introduced in Qt 5.6 according to the API.

                                    How can I get Qt 5.6 without having to install it myself from Source?
                                    Is there an Update on the system Packages yet which includes this more recent Qt Version?

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #18

                                    @Sewing Just download official Qt Online Installer and install: http://download.qt.io/official_releases/online_installers/qt-unified-linux-x64-online.run

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • S Offline
                                      S Offline
                                      Sewing
                                      wrote on last edited by
                                      #19

                                      All right, not sure how to link against this install in my Cmake Project though =/

                                      with Qt5.5 I just used find_package command...

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • S Sewing

                                        All right, not sure how to link against this install in my Cmake Project though =/

                                        with Qt5.5 I just used find_package command...

                                        jsulmJ Offline
                                        jsulmJ Offline
                                        jsulm
                                        Lifetime Qt Champion
                                        wrote on last edited by
                                        #20

                                        @Sewing Should be the same: just select the Kit for this new Qt version and then run cmake.

                                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        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