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. I can't understand the new syntax of connect

I can't understand the new syntax of connect

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 601 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.
  • M Offline
    M Offline
    Mr Pang
    wrote on last edited by
    #1

    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 *
    

    ?

    JKSHJ 2 Replies Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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
      3
      • M Mr Pang

        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 *
        

        ?

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • M Mr Pang

          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 *
          

          ?

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by JKSH
          #4

          @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
          2
          • Chris KawaC Chris Kawa

            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 Offline
            M Offline
            Mr Pang
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • JKSHJ 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 Offline
              M Offline
              Mr Pang
              wrote on last edited by
              #6

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

              JKSHJ 1 Reply Last reply
              0
              • M Mr Pang

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

                JKSHJ Offline
                JKSHJ Offline
                JKSH
                Moderators
                wrote on last edited by JKSH
                #7

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

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved