[Solved] One buttonClicked(QAbstractButton*) signal calls several times to a slot in QButtonGroup



  • Hi all,

    I'm using following code to create a toolbox. It creates QToolButtons and add it to a QButtonGroup. And it's buttonClicked(QAbstractButton*) signal connects with slot called buttonFilter(QAbstractButton*).

    @void createTools()
    {
    //retrieve toolgroups
    QMapIterator<int, QString> i(toolGroups());

        while (i.hasNext()) {
            i.next();
            
            QWidget *section = new QWidget(toolbox);
            QVBoxLayout *layout = new QVBoxLayout();
            section->setLayout(layout);
            
            //create Buttons for tools
            for (int j = 0 ; j < tools.length() ; j++)
            {
                QToolButton *button = new QToolButton(section);
                button_group->addButton(button);
            }
            
            connect(button_group, SIGNAL(buttonClicked(QAbstractButton*)),
            this, SLOT(buttonFilter(QAbstractButton*)));
    
            toolbox->addItem(button_group);
        }
    }@
    

    In this code one button click calls several times to slot. After some moments I found out the no. of calling times equal to no. of ToolGroups.

    Then as a try I put connection out of the iterator. Then one button click calls one time for slot as intended. Code is working! :)

    My problem is why it was calling several times even it was same connection?

    Thanks for reading.



  • I think ,because of QMapIterator, Your slot is calling several times.
    Please go through this.

    http://qt-project.org/doc/qt-5/QMapIterator.html



  • You are calling connect every iteration. It creates the those many connects.
    Do the following. You will all set.

    @connect(button_group, SIGNAL(buttonClicked(QAbstractButton*)),
    this, SLOT(buttonFilter(QAbstractButton*)),Qt::UniqueConnection);
    @
    Also there is no reason to call connect in loop. You can move this connect out of loop.



  • ankursaxena,
    Thanks for the reply. Yes due to the iteration it creates many connections between same signal and slot.

    Dheerendra,
    Qt::UniqueConnection can solve the problem. But I preferred to put the connection out of the loop.

    Thank you for the answer.



  • If your problem is solved , then please prepend the title as <Solved>



  • ankursaxena,
    Sorry I forgot to change.


Log in to reply
 

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