Important: Please read the Qt Code of Conduct -

[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()) {
            QWidget *section = new QWidget(toolbox);
            QVBoxLayout *layout = new QVBoxLayout();
            //create Buttons for tools
            for (int j = 0 ; j < tools.length() ; j++)
                QToolButton *button = new QToolButton(section);
            connect(button_group, SIGNAL(buttonClicked(QAbstractButton*)),
            this, SLOT(buttonFilter(QAbstractButton*)));

    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.

  • Qt Champions 2017

    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.

    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