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. New signals & slots connection syntax with Qt5

New signals & slots connection syntax with Qt5

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 4.9k Views
  • 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.
  • K Offline
    K Offline
    kevina67
    wrote on last edited by
    #1

    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 ;)

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, the reason is that

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

      setFocus() is an inline function.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kevina67
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kevina67
          wrote on last edited by
          #4

          Up please ;)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kevina67
              wrote on last edited by
              #6

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

              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