Qt Forum

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

    Solved I can't understand the new syntax of connect

    General and Desktop
    3
    7
    204
    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.
    • M
      Mr Pang last edited by

      The definition of connect is as below:

      inline QMetaObject::Connection QObject::connect(const QObject *asender, const char *asignal,
                                                  const char *amember, Qt::ConnectionType atype) const
      { return connect(asender, asignal, this, amember, atype); }
      

      And I can't understand how it work like

      connect(new QSpinBox, static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged), [=](auto v){});
      

      Why function pointer and lambda function can convert to

      const char *
      

      ?

      JKSH 2 Replies Last reply Reply Quote 0
      • Chris Kawa
        Chris Kawa Moderators last edited by

        Why function pointer and lambda function can convert to const char *

        Directly they can't and they don't in this example. You've got the wrong overload. Here's the list: connect overloads.

        The one used here is

        template<typename PointerToMemberFunction, typename Functor>
        QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
        
        M 1 Reply Last reply Reply Quote 3
        • JKSH
          JKSH Moderators @Mr Pang last edited by

          This post is deleted!
          1 Reply Last reply Reply Quote 0
          • JKSH
            JKSH Moderators @Mr Pang last edited by JKSH

            @Mr-Pang said in I can't understand the new syntax of connect:

            static_cast<void(QSpinBox::*)(int)>(&QSpinBox::valueChanged)

            P.S. It is nicer to write

            • QOverload<int>::of(&QSpinBox::valueChanged) (C++11), OR
            • qOverload<int>(&QSpinBox::valueChanged) (C++14 and newer)

            P.P.S. You might find this article helpful: https://doc.qt.io/qt-5/signalsandslots-syntaxes.html

            M 1 Reply Last reply Reply Quote 2
            • M
              Mr Pang @Chris Kawa last edited by

              @Chris-Kawa
              OK. I just use qtcreator to jump and it takes me to wrong place.

              1 Reply Last reply Reply Quote 0
              • M
                Mr Pang @JKSH last edited by

                @JKSH
                Yes. I think doc.qt.io may update the doc about QSpinBox since my syntax is copied from there.

                JKSH 1 Reply Last reply Reply Quote 0
                • JKSH
                  JKSH Moderators @Mr Pang last edited by JKSH

                  @Mr-Pang said in I can't understand the new syntax of connect:

                  I think doc.qt.io may update the doc about QSpinBox since my syntax is copied from there.

                  The latest version of the documentation (Qt 5.13.1) uses QOverload: https://doc.qt.io/qt-5/qspinbox.html

                  Anyway, has your original question been answered? Do you understand the new connect syntax now?

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