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. button action clicked() called several times per click?

button action clicked() called several times per click?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 2 Posters 879 Views 2 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 davecotter
    #1

    i have my button action connector like this:

    void	QtDialog::buttonConnect(DialogItemIndex itemS)
    {
    	QPushButton		*buttonP(i_dlgP->QtGetItem<QPushButton>(itemS));
    	
    	if (buttonP) {
    		QObject::connect(buttonP, &QPushButton::clicked, [this, itemS]() {
    			this->buttonClicked(itemS);
    		});
    	}
    }
    

    Problem is, my buttonClicked() method is called several times after just one click. what's going on?

    wanna see it in action?

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

      @SGaist said in button action clicked() called several times per click?:

      Each time you call buttonConnect with the same value of itemS you connect again the same button, hence your lambda will called as many times as you called buttonConnect for a given itemS.

      OMG

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Each time you call buttonConnect with the same value of itemS you connect again the same button, hence your lambda will called as many times as you called buttonConnect for a given itemS.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

          @SGaist said in button action clicked() called several times per click?:

          Each time you call buttonConnect with the same value of itemS you connect again the same button, hence your lambda will called as many times as you called buttonConnect for a given itemS.

          OMG

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

            well the next obvious question is: can i ask if a button already has a connection?

            i see this but that makes me nervous

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              The fast and easy way is to use Qt::UniqueConnection as connection type. BUT it won't work with lambdas. So you have to use a private slot.

              The longest is to rethink the design of buttonConnect. Do you really need the method ? What design is behind it ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                i'm porting a monolithic app that handles clicks in a single function that takes an "itemID", so it's prohibitive to retool THAT part for now.

                the dialog user may swtich between tabs a dozen times, and each time, the current panel is set up. i'd like the buttonConnect function to ignore subsequent setups. also i somehow just need to get the itemID to the receiver of the signal. How could i do that given the constraints?

                i suppose i could buttonP->setProperty("initted", true), then skip out if it's ALREADY set? <--- this worked

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  Why not make a loop that you run once that will connect the button of all the tabs ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                    well, it's a good idea and i've considered it, but that's a rearchitect of the monolithic app. a major goal is it must still compile and work under the old architecture during the transition, so i can't change that code.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      I'm not sure I'm following you with regard to that old architecture build.

                      In any case, if you really have to keep it like that, then as you already had the idea, use a dynamic property and check it before doing the connection.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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

                        i just mean that the old app must compile as is, and only the lowest level platform specific UI calls are being replaced with QT things. all the independent code must stay the same. to implement as you suggested, i'd have to change the independent code to accommodate QT, and i can't do that.

                        in any case, setProperty() does the trick.

                        thanks!

                        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