Results 1 to 19 of 19

Thread: simple way to connect 120 pushbutton signals to one slot

  1. #1
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default simple way to connect 120 pushbutton signals to one slot

    hi friends,
    i am having 128 QPushButton which checkable() condition is set true(Toggle button) in my window and i want to set color of each button if it is toggled so i will use stylesheet to set color with QPushButton signal toggled(bool) to recv_slot(bool)..
    but the problem is how i can connect each 128 buttons to a single slot .. it makes my program lengthier .. how can i minimize in connection .. can i go for QSignalMapper or QList ..?

    please suggest me a good solution ..
    "Behind every great fortune lies a crime" - Balzac

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Put the buttons in a QList<QPushButton*>. Iterate this list and connect the signal and slot for each button
    for eg -
    foreach(button,buttonList)
    connect(button,SIGNAL(clicked()),this,SLOT(onButto nClicked()));

    in the slot u can cast the sender() to know which button was clicked.

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

    wagmare (21st January 2010)

  4. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Hi,

    Maybe QButtonGroup can help you. You can get the button list and do a connection for every button on the list.
    Òscar Llarch i Galán

  5. #4
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Hi,

    One more way could be derive your class from QPushButton and add color change mechanism within derived class only. if that is not feasible for your architecture then add overloaded constructor within derived class to accept that class pointer to which your QPushButton needs to be connected. so within this class only you will have connect call. Later just matter of string replace in your code from QPushButton to MyCustonDerivedPushButton class.

  6. The following user says thank you to vishwajeet.dusane for this useful post:

    wagmare (21st January 2010)

  7. #5
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Why not use QSignalMapper? http://qt.nokia.com/doc/4.6/qsignalmapper.html

    It's a simple way of combining multiple signals to a single slot, and can pass id to your slot to state which triggered the signal.

  8. #6
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    [Why not use QSignalMapper? http://qt.nokia.com/doc/4.6/qsignalmapper.html

    It's a simple way of combining multiple signals to a single slot, and can pass id to your slot to state which triggered the signal.
    but if i am using toggled(bool) signal of QPushButton how can i use QSignalMapper because i need the bool value .. i dont know the procedure ..can u explan it ..
    Last edited by wagmare; 21st January 2010 at 14:14.
    "Behind every great fortune lies a crime" - Balzac

  9. #7
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    thanks for reply to all
    Last edited by wagmare; 21st January 2010 at 14:24.
    "Behind every great fortune lies a crime" - Balzac

  10. #8
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Quote Originally Posted by aamer4yu View Post
    Put the buttons in a QList<QPushButton*>. Iterate this list and connect the signal and slot for each button
    for eg -
    foreach(button,buttonList)
    connect(button,SIGNAL(clicked()),this,SLOT(onButto nClicked()));

    in the slot u can cast the sender() to know which button was clicked.
    thanks aamer ji ...
    i dont know how to get sender() from the button variable ... is it a QPushButton variable ..?

    please can u tell me more about this method ...

    i coded like this
    Qt Code:
    1. button_list = this->findChildren<QPushButton *> ();
    2. for(int i =0; i <button_list.size(); i++)
    3. {
    4. connect(button_list[i], SIGNAL(toggled(bool)), this, SLOT(state_changed(bool)));
    5. }
    To copy to clipboard, switch view to plain text mode 
    in state_changed(bool) slot
    f
    Qt Code:
    1. or(i =0; i <button_list.size(); i++)
    2. {
    3. if(sender() == button_list[i])
    4. {
    5. printf("blah blah \n");
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  11. #9
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Quote Originally Posted by wagmare View Post
    thanks aamer ji ...
    i dont know how to get sender() from the button variable ... is it a QPushButton variable ..?

    please can u tell me more about this method ...

    i coded like this
    Qt Code:
    1. button_list = this->findChildren<QPushButton *> ();
    2. for(int i =0; i <button_list.size(); i++)
    3. {
    4. connect(button_list[i], SIGNAL(toggled(bool)), this, SLOT(state_changed(bool)));
    5. }
    To copy to clipboard, switch view to plain text mode 
    in state_changed(bool) slot
    f
    Qt Code:
    1. or(i =0; i <button_list.size(); i++)
    2. {
    3. if(sender() == button_list[i])
    4. {
    5. printf("blah blah \n");
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    that is generally how this works: sender() returns QObject * pointer pointing to the object which called this slot. You then have to cast it to QPushButton* (using qobject_cast is the best way to do so), so your code is right :]
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  12. #10
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Thanks
    57
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Why not ...
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5. ...
    6. //QPushButton *buttonx[9];
    7. buttonx[0] = ui->b1;
    8. buttonx[1] = ui->b2;
    9. buttonx[2] = ui->b3;
    10. buttonx[3] = ui->b4;
    11. buttonx[4] = ui->b5;
    12. buttonx[5] = ui->b6;
    13. buttonx[6] = ui->b7;
    14. buttonx[7] = ui->b8;
    15. buttonx[8] = ui->b9;
    16.  
    17. //http://doc.trolltech.com/qq/qq10-signalmapper.html
    18. //The Signal Mapper Approach
    19. QSignalMapper *signalMapper = new QSignalMapper(this);
    20. connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(myButtonPressed(int)));
    21. for(int i = 0; i < 9; i++){
    22. signalMapper->setMapping(buttonx[i], i + 1);
    23. connect(buttonx[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
    24. }
    25. ...
    26.  
    27. }
    28.  
    29. ...
    30.  
    31. void MainWindow::myButtonPressed(int m){
    32. //use one specific button
    33.  
    34. //iterate the array to use all
    35.  
    36. }
    37.  
    38. ...
    To copy to clipboard, switch view to plain text mode 

  13. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    I guess I paid more attention to your list question. On reading again I feel what Vishwajeet said is better solution( Subclassing the QPushButton).
    In fact I myself had used subclassing for same problem once. You only need to do the following n paintEvent of inherited class-
    Qt Code:
    1. MyButton::paintEvent(*event)
    2. { QPushButton::paintevent(event);
    3. QPainter painter(this);
    4. // draw some color over top of the button using painter.
    5. }
    To copy to clipboard, switch view to plain text mode 

  14. #12
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Quote Originally Posted by graciano View Post
    Why not ...
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent)
    2. : QMainWindow(parent), ui(new Ui::MainWindowClass)
    3. {
    4. ui->setupUi(this);
    5. ...
    6. //QPushButton *buttonx[9];
    7. buttonx[0] = ui->b1;
    8. buttonx[1] = ui->b2;
    9. buttonx[2] = ui->b3;
    10. buttonx[3] = ui->b4;
    11. buttonx[4] = ui->b5;
    12. buttonx[5] = ui->b6;
    13. buttonx[6] = ui->b7;
    14. buttonx[7] = ui->b8;
    15. buttonx[8] = ui->b9;
    16.  
    17. //http://doc.trolltech.com/qq/qq10-signalmapper.html
    18. //The Signal Mapper Approach
    19. QSignalMapper *signalMapper = new QSignalMapper(this);
    20. connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(myButtonPressed(int)));
    21. for(int i = 0; i < 9; i++){
    22. signalMapper->setMapping(buttonx[i], i + 1);
    23. connect(buttonx[i], SIGNAL(clicked()), signalMapper, SLOT(map()));
    24. }
    25. ...
    26.  
    27. }
    28.  
    29. ...
    30.  
    31. void MainWindow::myButtonPressed(int m){
    32. //use one specific button
    33.  
    34. //iterate the array to use all
    35.  
    36. }
    37.  
    38. ...
    To copy to clipboard, switch view to plain text mode 
    but if i want to use toggled(bool) signal of QPushButton how can i map it to signal mapper ... i need the bool value ... i dont know how to do it in signal mapper mapping ..
    "Behind every great fortune lies a crime" - Balzac

  15. #13
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Quote Originally Posted by aamer4yu View Post
    I guess I paid more attention to your list question. On reading again I feel what Vishwajeet said is better solution( Subclassing the QPushButton).
    In fact I myself had used subclassing for same problem once. You only need to do the following n paintEvent of inherited class-
    Qt Code:
    1. MyButton::paintEvent(*event)
    2. { QPushButton::paintevent(event);
    3. QPainter painter(this);
    4. // draw some color over top of the button using painter.
    5. }
    To copy to clipboard, switch view to plain text mode 
    right
    i agree but all the push buttons are designed by designer .. i am having 128 button arranged in specific position by designer .. if i am adding it maually to my widget means no problem i will inherit QPushButton as sub class and i will set pseudo state in QPaintEvent ..
    "Behind every great fortune lies a crime" - Balzac

  16. #14
    Join Date
    Sep 2009
    Posts
    72
    Thanked 10 Times in 10 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Hi

    Thats ok if u have added push buttons from designer. There is an option in designer to promote your class. You right click on your push button and then press "promote to ..." promote single QPushButton to yourCustonClass. save your .ui file. in any editor open you .ui file which is nothign but a xml file. do the string replace for rest of you 127 push buttons Now you have used custom class in designer for your push buttons.

    second step would be to write implementation for it. that you can manage rite.

  17. The following user says thank you to vishwajeet.dusane for this useful post:

    wagmare (22nd January 2010)

  18. #15
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Quote Originally Posted by wagmare View Post
    but if i want to use toggled(bool) signal of QPushButton how can i map it to signal mapper ... i need the bool value ... i dont know how to do it in signal mapper mapping ..
    Just call isChecked ().

  19. The following user says thank you to numbat for this useful post:

    wagmare (25th January 2010)

  20. #16
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: simple way to connect 120 pushbutton signals to one slot

    you can try to move the logic inside the stylesheet:

    QPushButton:checked {
    background-color: rgb(224, 0, 0);
    border-style: inset;
    }

    (See the style sheet docs: stylesheet-examples

  21. The following user says thank you to caduel for this useful post:

    wagmare (25th January 2010)

  22. #17
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Quote Originally Posted by caduel View Post
    you can try to move the logic inside the stylesheet:

    QPushButton:checked {
    background-color: rgb(224, 0, 0);
    border-style: inset;
    }

    (See the style sheet docs: stylesheet-examples
    this one works perfect ... in designer i had given this stylesheet option to all 128 push buttons ... but dissolves the border of the push button .. it look flat surface without the trace of the button
    Attached Images Attached Images
    Last edited by wagmare; 25th January 2010 at 10:35.
    "Behind every great fortune lies a crime" - Balzac

  23. #18
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    but dissolves the border of the push button .. it look flat surface without the trace of the button
    Thats why subclassing is the better way. You wont lose the native drawing.

  24. #19
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple way to connect 120 pushbutton signals to one slot

    Thats why subclassing is the better way. You wont lose the native drawing.
    right sub classing is better option ...
    "Behind every great fortune lies a crime" - Balzac

Similar Threads

  1. Simple QObject::connect question
    By Bryku in forum Newbie
    Replies: 2
    Last Post: 8th January 2010, 12:09
  2. how to connect events with signals in QGraphicsScene?
    By nataly in forum Qt Programming
    Replies: 0
    Last Post: 3rd November 2009, 15:20
  3. QT Animation Simple Pushbutton Help
    By Pembar in forum Qt Programming
    Replies: 4
    Last Post: 6th May 2009, 13:38
  4. simple signals and slots
    By kroenecker in forum Newbie
    Replies: 2
    Last Post: 24th March 2007, 05:15
  5. Qt Designer & Qt4, connect to my own slot.
    By geitosten in forum Newbie
    Replies: 2
    Last Post: 17th February 2007, 19:22

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.