Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    New signals & slots connection syntax with Qt5

    General and Desktop
    2
    6
    4715
    Loading More Posts
    • 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
      kevina67 last edited by

      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 Reply Quote 0
      • D
        dbzhang800 last edited by

        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 Reply Quote 0
        • K
          kevina67 last edited by

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

          1 Reply Last reply Reply Quote 0
          • K
            kevina67 last edited by

            Up please ;)

            1 Reply Last reply Reply Quote 0
            • D
              dbzhang800 last edited by

              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 Reply Quote 0
              • K
                kevina67 last edited by

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

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post