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. QObject connection, why do I have to add sender twice?

QObject connection, why do I have to add sender twice?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 643 Views 3 Watching
  • 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
    kocka
    wrote on last edited by
    #1

    This function find QPushButton by their name.

    QPushButton *MainWindow::findByObjName(QWidget *widget, QString objName) {
        return widget->findChild<QPushButton *>(objName);
    }
    
    findByObjName(widget,"pushButton_xx")->connect(findByObjName(widget,"pushButton_xx"), &QPushButton::clicked, this, [=]{MainWindow::function(widget);});
    

    Why do I have to add findByObjName(widget,"pushButton_xx") twice?
    Is there a case where I need to use this method?

    raven-worxR 1 Reply Last reply
    0
    • K kocka

      @fcarney
      @raven-worx

      Okey, I but why there is a opportunity, to call connect as a reference of individual member of QPushButton class?

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #6

      @kocka said in QObject connection, why do I have to add sender twice?:

      @fcarney
      @raven-worx

      Okey, I but why there is a opportunity, to call connect as a reference of individual member of QPushButton class?

      C++ allows accessing a static class member via the scope operator, eg Class::member, or via an object that is an instance of the class, such as objectPtr->member or objectRef.member.
      https://en.cppreference.com/w/cpp/language/static

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      3
      • K kocka

        This function find QPushButton by their name.

        QPushButton *MainWindow::findByObjName(QWidget *widget, QString objName) {
            return widget->findChild<QPushButton *>(objName);
        }
        
        findByObjName(widget,"pushButton_xx")->connect(findByObjName(widget,"pushButton_xx"), &QPushButton::clicked, this, [=]{MainWindow::function(widget);});
        

        Why do I have to add findByObjName(widget,"pushButton_xx") twice?
        Is there a case where I need to use this method?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #2

        @kocka
        you dont have to.

        You can remove it when you are inside a QObject, or add QObject:: namespace to it

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        3
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by fcarney
          #3

          The connect just needs to be run on QObject. I believe connect is static.

          QObject::connect(findByObjName(widget,"pushButton_xx"), &QPushButton::clicked, this, [=]{MainWindow::function(widget);});
          
          // Also, you can store your object for reuse:
          auto foundObject = findByObjName(widget,"pushButton_xx");
          QObject::connect(foundObject, &QPushButton::clicked, this, [=]{MainWindow::function(widget);});
          

          This works because QPushButton base class is QObject.

          C++ is a perfectly valid school of magic.

          K 1 Reply Last reply
          1
          • fcarneyF fcarney

            The connect just needs to be run on QObject. I believe connect is static.

            QObject::connect(findByObjName(widget,"pushButton_xx"), &QPushButton::clicked, this, [=]{MainWindow::function(widget);});
            
            // Also, you can store your object for reuse:
            auto foundObject = findByObjName(widget,"pushButton_xx");
            QObject::connect(foundObject, &QPushButton::clicked, this, [=]{MainWindow::function(widget);});
            

            This works because QPushButton base class is QObject.

            K Offline
            K Offline
            kocka
            wrote on last edited by
            #4

            @fcarney
            @raven-worx

            Okey, I but why there is a opportunity, to call connect as a reference of individual member of QPushButton class?

            raven-worxR jeremy_kJ 2 Replies Last reply
            0
            • K kocka

              @fcarney
              @raven-worx

              Okey, I but why there is a opportunity, to call connect as a reference of individual member of QPushButton class?

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #5

              @kocka
              because the connect method is public in the QObject class and QWidget inherits QObject

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1
              • K kocka

                @fcarney
                @raven-worx

                Okey, I but why there is a opportunity, to call connect as a reference of individual member of QPushButton class?

                jeremy_kJ Offline
                jeremy_kJ Offline
                jeremy_k
                wrote on last edited by
                #6

                @kocka said in QObject connection, why do I have to add sender twice?:

                @fcarney
                @raven-worx

                Okey, I but why there is a opportunity, to call connect as a reference of individual member of QPushButton class?

                C++ allows accessing a static class member via the scope operator, eg Class::member, or via an object that is an instance of the class, such as objectPtr->member or objectRef.member.
                https://en.cppreference.com/w/cpp/language/static

                Asking a question about code? http://eel.is/iso-c++/testcase/

                1 Reply Last reply
                3

                • Login

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