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. [Solved] One buttonClicked(QAbstractButton*) signal calls several times to a slot in QButtonGroup

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

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.7k Views 1 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.
  • H Offline
    H Offline
    Hareen Laks
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      ankursaxena
      wrote on last edited by
      #2

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

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

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0
        • H Offline
          H Offline
          Hareen Laks
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            ankursaxena
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • H Offline
              H Offline
              Hareen Laks
              wrote on last edited by
              #6

              ankursaxena,
              Sorry I forgot to change.

              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