Qt Forum

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

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Multiple QObject::connect with Qt::UniqueConnection not work

    General and Desktop
    7
    14
    4823
    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.
    • P
      pvt.peter last edited by

      Hi,

      I did some experiments about signal/slots and encountered an interesting problem.
      I would like to test Qt::UniqueConnection parameter in QObject::connect() method but it did not work properly.
      I have got a QComboBox, and i connected currentIndexChanged(int) signal with my ownMethod() slot.

      for (int i = 0; i < 5; ++i)
      {
              connect<void(QComboBox::*)(int)>
              (ui.comboBox, &QComboBox::currentIndexChanged, this, [this] {ownMethod(); }, Qt::UniqueConnection);
      }
      

      When i select an item in my comboBox, my ownMethod run five times.

      How can i connect currentIndexChanged(int) with my ownMethod() to run only once?

      The main part is the for cycle and Qt::UniqueConnection parameter, because i would like to invoke connect method for more times, but i want to run ownMethod() slot only once.

      jsulm raven-worx 2 Replies Last reply Reply Quote 0
      • hskoglund
        hskoglund last edited by

        Hi try a connect() without a lambda.

        1 Reply Last reply Reply Quote 0
        • jsulm
          jsulm Lifetime Qt Champion @pvt.peter last edited by

          @pvt.peter To my knowledge those 5 lambdas are not same although they look same. For the compiler those are different functions.

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

          1 Reply Last reply Reply Quote 3
          • Venkatesh V
            Venkatesh V last edited by

            Hi @pvt-peter
            can you once try by bellow connect statement it may works

            connect(ui.comboBox,SIGNAL(currentIndexChanged(int),this,SLOT(ownMethod(int)));

            Thanks.

            1 Reply Last reply Reply Quote 1
            • m.sue
              m.sue last edited by

              Hi,
              Maybe this way the compiler and the connect statement see it as one lambda function:

              auto slot=[this] {ownMethod(); };
              for (int i = 0; i < 5; ++i)
              {
                      connect<void(QComboBox::*)(int)>
                      (ui.comboBox, &QComboBox::currentIndexChanged, this, slot, Qt::UniqueConnection);
              }
              

              -Michael.

              1 Reply Last reply Reply Quote 0
              • P
                pvt.peter last edited by

                So this is an example code from my original project.
                My problem is: i have to invoke a method which invoke QObject::connect multiple times, but i would like to run my slot method only once.
                I would like to use Qt::UniqueConnection, because it is solve my problem (in theory).

                @hskoglund: thanks for your reply. It was only an idea. The main task is above: I have to connect multiple times, but run slot method only once.

                @jsulm: thanks for your reply. It was very useful.

                @Venkatesh-V: i wrote my original problem above. I have to connect multiple times, but run slot method only once.

                @m-sue: thanks for your modification. I tried it, but unfortunately it was produced the same result.

                1 Reply Last reply Reply Quote 0
                • Venkatesh V
                  Venkatesh V last edited by VRonin

                  Hi @pvt-peter

                  bellow code invoke connect() 5 times and calls ownMethod() only once for me

                  for (int i = 0; i < 5; ++i)
                  {
                  connect(ui.comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(ownMethod(int)),Qt::UniqueConnection);
                  }
                  

                  i hope this is the only thing you need right?.

                  1 Reply Last reply Reply Quote 0
                  • P
                    pvt.peter last edited by

                    Hi @Venkatesh-V,

                    Unfortunately, it does not work:

                    connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(ownMethod(int)), Qt::UniqueConnection);
                    

                    I could not compile it, i use Qt 5.7.0.

                    jsulm raven-worx 2 Replies Last reply Reply Quote 0
                    • raven-worx
                      raven-worx Moderators @pvt.peter last edited by raven-worx

                      @pvt.peter
                      whats the benefit of your approach anyway? Why do you want to create multiple connections for each item???
                      Even if you manage to get it working, it's useless overhead (multiple connections in the MetaObject, checking if the connection has been already made, ...)

                      Do your connection setup once and check the selected index in the slot and call your desired method.

                      --- 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 Reply Quote 2
                      • jsulm
                        jsulm Lifetime Qt Champion @pvt.peter last edited by

                        @pvt.peter What error do you get? This is plain old connect() syntax.

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

                        1 Reply Last reply Reply Quote 0
                        • raven-worx
                          raven-worx Moderators @pvt.peter last edited by

                          @pvt.peter said in Multiple QObject::connect with Qt::UniqueConnection not work:

                          connect(ui.comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(ownMethod(int)), Qt::UniqueConnection);

                          I could not compile it, i use Qt 5.7.0.

                          because of ui.comboBox?

                          --- 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 Reply Quote 3
                          • P
                            pvt.peter last edited by

                            Hi guys, sorry for my delay.

                            So, start at the beginning.
                            I have got a QDialog and a QDialogButtonBox on it with Save and Cancel QPushButtons.
                            This dialogbox contains some input text fields, which have to run validation after click Save. If everything is OK, DB transaction is started.
                            If not, QDialog will remain open and the problematic text fields with invalid contents are highlighted.
                            I do not want to activate the validation function until the user do not click the Save button, it is invoke QDialogButtonBox::accepted() method.
                            I connect the QDialogButtonBox::accepted() signal with my own slot, which does some QObject::connect() on QLineEdit widgets to validate their contents if text is changed.
                            But this own slot is invoked multiple times, when i click to Save button, but i would like to run the slot method only once.
                            I found a solution which is the Qt::UniqueConnection. I would like to use the new syntax of connect signals and slots because of sometimes occurs that i have to connect overloaded methods and i would like to know the success of the connection in compile time.

                            @raven-worx: i know very well, it has got some overhead under the hood :(

                            @jsulm: sorry, now i can not reproduce the error.

                            The solution for my problem is the next:
                            I set a flag in my own slot which is represent that the needed QObject::connect() methods are invoked already or not.
                            It provides that the required connects invoked only once.

                            1 Reply Last reply Reply Quote 0
                            • SGaist
                              SGaist Lifetime Qt Champion last edited by

                              Hi,

                              If you are using QLineEdit, why not use a Qt's validator classes on it ?

                              An alternative would be to create one slot where you connect all your editors and that validates whatever you need to validate and only enable the Save button if everything is fine.

                              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 Reply Quote 0
                              • P
                                pvt.peter last edited by

                                Hi @SGaist, thanks for your reply.
                                It is a good solution, a am thinking about it.

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