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. in Qt Designer, how to create signal/slot from QPushButton that passes buttonID?
Forum Updated to NodeBB v4.3 + New Features

in Qt Designer, how to create signal/slot from QPushButton that passes buttonID?

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.7k Views 1 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.
  • D Offline
    D Offline
    davecotter
    wrote on last edited by
    #1

    see below:
    0_1557334062602_Screen Shot 2019-05-08 at 9.42.57 AM.png
    Instead of having each button call a separate function, can i have them all call the same function but pass an ID into that function, eg:

    clicked_button(int buttonID)
    

    and can i do this in Designer, not in code?

    VRoninV 1 Reply Last reply
    0
    • D Offline
      D Offline
      davecotter
      wrote on last edited by
      #8

      i'm convinced. i was hoping to wire the signal / slots in Qt Designer, but you're right it's better to just do it programmatically. new code:

      void	SetUpButtons()
       {
      	QPushButton		*buttonP;
      	DialogItemIndex		itemS;
      	
      	loop (3) {
      		itemS	= _index + 1;
      		buttonP	= i_dlgP->QtGetItem<QPushButton>(itemS);
      		
      		connect(buttonP, &QPushButton::clicked, [this, itemS]() { this->buttonClicked(itemS); });
      	}
      }
      
      void	QQuick3::buttonClicked(DialogItemIndex itemS)
      {
      	done(itemS);
      }
      

      :D

      1 Reply Last reply
      0
      • D davecotter

        see below:
        0_1557334062602_Screen Shot 2019-05-08 at 9.42.57 AM.png
        Instead of having each button call a separate function, can i have them all call the same function but pass an ID into that function, eg:

        clicked_button(int buttonID)
        

        and can i do this in Designer, not in code?

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #2

        @davecotter said in in Qt Designer, how to create signal/slot from QPushButton that passes buttonID?:

        can i have them all call the same function but pass an ID into that function

        Yes.
        QSignalMapper or std::bind

        and can i do this in Designer, not in code?

        No

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        6
        • D Offline
          D Offline
          davecotter
          wrote on last edited by
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • D Offline
            D Offline
            davecotter
            wrote on last edited by davecotter
            #4

            i was able to create something that worked for my purposes: you CAN have every button go to one function, then ask for the SENDER of the signal, and get the button that sent it, then case on that button. eg:

            void	QQuick3::handleButton()
            {
            	QPushButton		*buttonP(qobject_cast<QPushButton *>(QObject::sender()));
            	DialogItemIndex		itemS(i_dlgP->QtNameToItem(buttonP->objectName()));
            
            	done(itemS);
            }
            
            jsulmJ 1 Reply Last reply
            1
            • D davecotter

              i was able to create something that worked for my purposes: you CAN have every button go to one function, then ask for the SENDER of the signal, and get the button that sent it, then case on that button. eg:

              void	QQuick3::handleButton()
              {
              	QPushButton		*buttonP(qobject_cast<QPushButton *>(QObject::sender()));
              	DialogItemIndex		itemS(i_dlgP->QtNameToItem(buttonP->objectName()));
              
              	done(itemS);
              }
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @davecotter You can do that but it isn't a clean solution (I guess that's why @VRonin didn't suggest this "solution"). You should avoid using sender().
              Since C++11 you can use lambdas:

              int id = i_dlgP->QtNameToItem(button1->objectName());
              connect(button1, &QPushButton::clicked, [this, id]() { ... });
              id = i_dlgP->QtNameToItem(button2->objectName());
              connect(button2, &QPushButton::clicked, [this, id]() { ... });
              ...
              

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • D Offline
                D Offline
                davecotter
                wrote on last edited by
                #6

                why shouldn't i use sender() ?

                jsulmJ 1 Reply Last reply
                0
                • D davecotter

                  why shouldn't i use sender() ?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #7

                  @davecotter Because you tightly couple sender and receiver and that is bad design. The receiver should not know anything about the sender, and sender about the receiver. With your sender() implementation your slot now have to know how to get the id. If that logic changes you will have to change the slot as well.
                  sender() is really just a work around.
                  See also https://doc.qt.io/qt-5/qobject.html#sender

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  3
                  • D Offline
                    D Offline
                    davecotter
                    wrote on last edited by
                    #8

                    i'm convinced. i was hoping to wire the signal / slots in Qt Designer, but you're right it's better to just do it programmatically. new code:

                    void	SetUpButtons()
                     {
                    	QPushButton		*buttonP;
                    	DialogItemIndex		itemS;
                    	
                    	loop (3) {
                    		itemS	= _index + 1;
                    		buttonP	= i_dlgP->QtGetItem<QPushButton>(itemS);
                    		
                    		connect(buttonP, &QPushButton::clicked, [this, itemS]() { this->buttonClicked(itemS); });
                    	}
                    }
                    
                    void	QQuick3::buttonClicked(DialogItemIndex itemS)
                    {
                    	done(itemS);
                    }
                    

                    :D

                    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