New signals & slots connection syntax with Qt5



  • Hi,

    I have a QLineEdit and a QPushButton. I'd like the button to gain focus when the return key is pressed while typing anything in the QLineEdit.

    Therefore I wrote :

    @QObject::connect(_ui->findExpressionLineEdit, &QLineEdit::returnPressed, _ui->findPushButton, &QWidget::setFocus);@

    But such code doen't want to compile :

    /home/kevina/Documents/editor/src/findWidget.cpp:56:14: error: no matching member function for call to 'connect'
    QObject::connect(_ui->findExpressionLineEdit, &QLineEdit::returnPressed, _ui->findPushButton, &QWidget::setFocus);
    ~~~^
    /usr/include/qt/QtCore/qobject.h:198:36: note: candidate function not viable: no known conversion from 'void (QLineEdit::*)()' to 'const char *' for 2nd argument
    static QMetaObject::Connection connect(const QObject *sender, const char signal,
    ^
    /usr/include/qt/QtCore/qobject.h:201:36: note: candidate function not viable: no known conversion from 'void (QLineEdit::
    )()' to 'const QMetaMethod' for 2nd argument
    static QMetaObject::Connection connect(const QObject sender, const QMetaMethod &signal,
    ^
    /usr/include/qt/QtCore/qobject.h:440:41: note: candidate function not viable: no known conversion from 'void (QLineEdit::
    )()' to 'const char *' for 2nd argument
    inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal,
    ^
    /usr/include/qt/QtCore/qobject.h:214:43: note: candidate template ignored: couldn't infer template argument 'Func2'
    static inline QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal,
    ^
    /usr/include/qt/QtCore/qobject.h:244:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided
    connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
    ^
    /usr/include/qt/QtCore/qobject.h:267:13: note: candidate function template not viable: requires 3 arguments, but 4 were provided
    connect(const typename QtPrivate::FunctionPointer<Func1>::Object *sender, Func1 signal, Func2 slot)
    ^
    1 error generated.

    But this code does work :

    @QObject::connect(_ui->findExpressionLineEdit, &QLineEdit::returnPressed, [this] () {

        _ui->findPushButton->setFocus();
    });@
    

    But I'd like to use the first syntax wich is more logical and concise.

    Any clue about that ? Thx ;)



  • Hi, the reason is that

    @
    class QWidget {
    //...
    public Q_SLOTS:
    inline void setFocus() { setFocus(Qt::OtherFocusReason); }
    @

    setFocus() is an inline function.



  • Ok thanks, but why does it works with the second syntax ?



  • Up please ;)



  • The second works, as you are calling

    @
    QMetaObject::Connection QObject::connect(const QObject * sender, PointerToMemberFunction signal, Functor functor)
    @

    The first doesn't work, as you are trying to call

    @
    QMetaObject::Connection QObject::connect(const QObject * sender, PointerToMemberFunction signal, const QObject * receiver, PointerToMemberFunction method, Qt::ConnectionType type)
    @
    which means that you are trying to get the address of an inline function.



  • Ok, thanks for your explanations 1+1=2.


Log in to reply
 

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