Results 1 to 7 of 7

Thread: Using QSIgnalMapper with multiple pushbuttons

  1. #1
    Join Date
    Jul 2013
    Posts
    27
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Using QSIgnalMapper with multiple pushbuttons

    Hello friends, I am using multiple QPushButtons in my project. All of them should go inside 1 slot. So i am using QSignalMapper. My ui object is UI. And the pushbuttons are already created in designer. Lets say they are named pushbutton1, pushbutton2, etc. Now i want to map all of them in a signal mapper. So one would ideally use a for loop
    Syntax :
    for(i=0;i<10;i++){
    connect(UI.pushbutton[i],SIGNAL(clicked()),signalmapper,SLOT(map()));
    signalMapper->setMapping(UI.pushButton[i],i);
    }

    signalMapper is my object. With this code i can send the values to the slot and depending on that value i can take decisions. But it fails, compiler says, it doesnt know pushButton. But when i put pushButton1 or 2, it works. How to get that i in a for loop part working? Kindly let me know. Thank you for your time.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using QSIgnalMapper with multiple pushbuttons

    signalMapper is my object. With this code i can send the values to the slot and depending on that value i can take decisions. But it fails, compiler says, it doesnt know pushButton. But when i put pushButton1 or 2, it works. How to get that i in a for loop part working? Kindly let me know. Thank you for your time.
    You cannot access pushButton in the loop using array operator.

    You have two options.
    1. use pushButton1, pushButton2.... etc instead of loop.
    2. If you want to use lopp then get the list pushButtons from ui an then connect them, somthing like below. But be warned that this will connect the slot to all the pushButtons on the widget.

    Qt Code:
    1. Ui::Form * ui;
    2. QSignalMapper * signalMapper;
    3. ...
    4.  
    5. ui->setupUi(this);
    6.  
    7. QObjectList childs = this->children();
    8.  
    9. for(int i = 0; i < childs.size(); i++)
    10. {
    11. QPushButton * pushButton = qobject_cast<QPushButton *>(childs[i]);
    12.  
    13. if(pushButton)
    14. {
    15. this->connect(pushButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    16. signalMapper->setMapping(pushButton, i);
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. The following user says thank you to Santosh Reddy for this useful post:

    akshaysulakhe (1st August 2013)

  4. #3
    Join Date
    Jul 2013
    Posts
    27
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QSIgnalMapper with multiple pushbuttons

    Hello, Thank you for the reply. For now, i wrote it manually, but it still wont work. Syntax:
    connect(ui.PushButton1, SIGNAL(clicked()),signalMapper,SLOT(map()))
    Did similar for 10 buttons. I cant find child for all buttons, as there are other buttons also in the parent.
    then i did
    connect(signalMapper,SIGNAL(mapped(const QString &)),this, SIGNAL(clicked(const QString &)));
    connect(signalMapper, SIGNAL(mapped(const QString &)),SLOT(changeImage(const QString &)));

    This compiles, but when i click the button, i should get the button name in ChangeImage, which i am not getting. Kindly let me know what to do.

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using QSIgnalMapper with multiple pushbuttons

    You are missing this
    Qt Code:
    1. signalMapper->setMapping(ui.pushButton1, "pushButton1");
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. The following user says thank you to Santosh Reddy for this useful post:

    akshaysulakhe (1st August 2013)

  7. #5
    Join Date
    Jul 2013
    Posts
    27
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QSIgnalMapper with multiple pushbuttons

    Good god man, Thanks, what a mistake, was struggling. Just one last query. As i have done the mapping without a for loop, it doesnt look very neat. And i cant use the code mentioned above, as there are other pushbuttons too. But all these 10 pushbuttons who have a single parent object name = QVerticalLayout and type QVBoxLayout...How can i modify the above code for that...Thanks again. :-)

  8. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using QSIgnalMapper with multiple pushbuttons

    If you have the layout handle, then this way..
    Qt Code:
    1. QSignalMapper * signalMapper;
    2. QVBoxLayout * layout;
    3.  
    4. for(int i = 0; i < layout->count(); i++)
    5. {
    6. QLayoutItem * item = layout->itemAt(i);
    7.  
    8. QWidget * widget = item->widget();
    9.  
    10. if(widget)
    11. {
    12. QPushButton * pushButton = dynamic_cast<QPushButton *>(widget);
    13.  
    14. if(pushButton)
    15. {
    16. signalMapper->connect(pushButton, SIGNAL(clicked()), SLOT(map()));
    17. signalMapper->setMapping(pushButton, pushButton->objectName());
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  9. The following user says thank you to Santosh Reddy for this useful post:

    akshaysulakhe (1st August 2013)

  10. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using QSIgnalMapper with multiple pushbuttons

    Alternatively you could try using QButtonGroup, which is a kind of specialized signal mapper for buttons.

    For your case it has the advantage of being available in designer (select multiple buttons -> right click -> assign to button group)

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    akshaysulakhe (22nd August 2013)

Similar Threads

  1. Qwt Zoom through PushButtons?
    By sonulohani in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2012, 10:56
  2. Replies: 2
    Last Post: 19th October 2011, 09:30
  3. Using ICONs as PushButtons
    By sudhakaran in forum Newbie
    Replies: 10
    Last Post: 4th February 2010, 18:34
  4. Three pushButtons in a shape of one
    By Kenji_Takahashi in forum Qt Programming
    Replies: 13
    Last Post: 7th September 2009, 19:25
  5. Problems connecting PushButtons
    By Randulf in forum Newbie
    Replies: 3
    Last Post: 23rd August 2006, 14:39

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.