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. Multiple QObject::connect with Qt::UniqueConnection not work
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 8 Posters 6.3k Views 4 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.
  • P pvt.peter
    21 Nov 2016, 22:43

    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.

    J Offline
    J Offline
    jsulm
    Lifetime Qt Champion
    wrote on 22 Nov 2016, 05:20 last edited by
    #3

    @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
    3
    • V Offline
      V Offline
      Venkatesh V
      wrote on 22 Nov 2016, 06:27 last edited by
      #4

      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
      1
      • M Offline
        M Offline
        m.sue
        wrote on 22 Nov 2016, 07:52 last edited by
        #5

        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
        0
        • P Offline
          P Offline
          pvt.peter
          wrote on 22 Nov 2016, 09:00 last edited by
          #6

          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
          0
          • V Offline
            V Offline
            Venkatesh V
            wrote on 22 Nov 2016, 10:39 last edited by VRonin
            #7

            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
            0
            • P Offline
              P Offline
              pvt.peter
              wrote on 22 Nov 2016, 11:18 last edited by
              #8

              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.

              J R 2 Replies Last reply 22 Nov 2016, 11:37
              0
              • P pvt.peter
                21 Nov 2016, 22:43

                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.

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 22 Nov 2016, 11:24 last edited by raven-worx
                #9

                @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
                2
                • P pvt.peter
                  22 Nov 2016, 11:18

                  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.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 22 Nov 2016, 11:37 last edited by
                  #10

                  @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
                  0
                  • P pvt.peter
                    22 Nov 2016, 11:18

                    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.

                    R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 22 Nov 2016, 11:38 last edited by
                    #11

                    @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
                    3
                    • P Offline
                      P Offline
                      pvt.peter
                      wrote on 24 Nov 2016, 09:32 last edited by
                      #12

                      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
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 24 Nov 2016, 22:58 last edited by
                        #13

                        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
                        0
                        • P Offline
                          P Offline
                          pvt.peter
                          wrote on 26 Nov 2016, 20:58 last edited by
                          #14

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

                          1 Reply Last reply
                          0
                          • D Offline
                            D Offline
                            DungeonLords
                            wrote on 12 Jan 2024, 02:57 last edited by
                            #15

                            This is example how to use Qt::UniqueConnection

                            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