Problem with the connect method in Qt5



  • 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



  • Hi,

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


  • Moderators

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



  • @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?



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


  • Moderators

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



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



  • My bad, sorry folks!



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


  • Moderators

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



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



  • @ 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?



  • @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?



  • 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?



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


  • Qt Champions 2016

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



  • @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?


  • Moderators



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

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


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.