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



  • 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.



  • Hi try a connect() without a lambda.


  • Moderators

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



  • 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.



  • 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.



  • 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.



  • 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?.



  • 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.


  • Moderators

    @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.


  • Moderators

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


  • Moderators

    @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?



  • 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.


  • Lifetime Qt Champion

    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.



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.