Results 1 to 9 of 9

Thread: Several GroupBoxes with Radiobutton function

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Several GroupBoxes with Radiobutton function

    Hi,

    I have several group boxes (dynamical generated). The group boxes have been modified in a custom class and are now cards. I used the group box because with the border and the checkbox I only needed to add some content (image, text) to have my card.
    That my program runs only one card is allowed to be active, like radio buttons. My plan was to create a signal that is sent to all boxes and tells them to turn off. The signal contains the sender index so that the sending box knows not to turn off as well. My problem is to connect the toggled(bool) signal from the QGroupBox with my custom signal. Can I somehow wire these two signals? Or asked differently how do I know where the toggled(bool) signal came from?
    Here is the card class:
    Qt Code:
    1. #ifndef SATIONCARD_H
    2. #define SATIONCARD_H
    3.  
    4. #include <QLabel>
    5. #include <QGroupBox>
    6. #include <QLayout>
    7.  
    8. class StationCard : public QGroupBox{
    9. Q_OBJECT
    10. public:
    11. StationCard();
    12. QLabel *propLabel;
    13. int index;
    14. bool on;
    15.  
    16. public slots:
    17. void turnOff(int actIndex);
    18.  
    19. signals:
    20. void toggled(int index, bool on);
    21.  
    22. private:
    23.  
    24. };
    25.  
    26. #endif // SATIONCARD_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "stationcard.h"
    2.  
    3. StationCard::StationCard(){
    4. QHBoxLayout *layout = new QHBoxLayout;
    5. propLabel = new QLabel();
    6. layout->addWidget(propLabel, 10, Qt::AlignRight);
    7. setLayout(layout);
    8.  
    9. on = isChecked();
    10. if(on){
    11. emit toggled(index, on);
    12. emit turnOff(index);
    13. }
    14. }
    15.  
    16. void StationCard::turnOff(int actIndex){
    17. if(actIndex != index){
    18. setChecked(false);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    And the connection in my main window:
    Qt Code:
    1. int k = 0;
    2. for(int i = 0; i < 2; ++i){
    3. for(int j = 0; j < 4; ++j){
    4. CardVector[k] = new SationCard;
    5. CardVector[k]->index = k;
    6. QObject::connect(CardVector[k], SIGNAL(toggled(int, bool)), this, SLOT(on_stationCard_toggled(int, bool)));
    7. ui->cardLayout->addWidget(enemyVector[k], i, j);
    8. k++;
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Several GroupBoxes with Radiobutton function

    You need to connect the groupbox's toggled(bool) signal to slot in your StationCard class. In that slot you emit your signal with it two arguments.

    Btw, there is no point in emitting signals from inside the constructor. At this time nobody can be connected to the object yet.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Several GroupBoxes with Radiobutton function

    Hi ,


    is that really so easy ? If I connect toggled(bool) with toggled(int, bool) all cards will sent their signal and I still don't know where the signal came from.

  4. #4
    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: Several GroupBoxes with Radiobutton function

    No.

    You connect to a slot and make it emit the other signal.
    This indirection is needed because your second signal has more arguments than the first.

    Cheers,
    _

  5. #5
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Several GroupBoxes with Radiobutton function

    Okay I will try that. But I am still suspicious that it will work. If I connect toggle(bool) with toggle(bool , int) all cards will answer. That is okay as long as no other card is selected but if another card is already selected I will not be able to know where the signal came from. Or do I see that wrong?

  6. #6
    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: Several GroupBoxes with Radiobutton function

    As I already said, you can't connect toggled(bool) to toggled(int, bool) since the latter has more arguments than the former.
    Hence the need to go through a slot.

    Cheers,
    _

  7. #7
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Several GroupBoxes with Radiobutton function

    Okay here is how I made it. The code works but I do not really understand why.
    The class:
    Qt Code:
    1. #ifndef STATIONCARD_H
    2. #define STATIONCARD_H
    3.  
    4. #include <QLabel>
    5. #include <QGroupBox>
    6. #include <QLayout>
    7. #include <QDebug>
    8.  
    9. class StationCard : public QGroupBox{
    10. Q_OBJECT
    11. public:
    12. StationCard();
    13. QLabel *propLabel;
    14. int index;
    15. bool on;
    16.  
    17. public slots:
    18. void turnOff(int actIndex);
    19. void sentToggle(bool on);
    20.  
    21. signals:
    22. void toggled(int index);
    23.  
    24. private:
    25.  
    26. };
    27.  
    28. #endif // STATIONCARD_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "stationcard.h"
    2.  
    3. StationCard::StationCard(){
    4. QHBoxLayout *layout = new QHBoxLayout;
    5. propLabel = new QLabel();
    6. layout->addWidget(propLabel, 10, Qt::AlignRight);
    7. setLayout(layout);
    8. }
    9.  
    10. void StationCard::turnOff(int actIndex){
    11. if(actIndex != index){
    12. setChecked(false);
    13. }
    14. }
    15.  
    16. void StationCard::sentToggle(bool on){
    17. qDebug() << "Signal comes.";
    18. if(on){
    19. emit toggled(index);
    20. emit turnOff(index);
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    And the wirering (I also added a signal to my main window: void turnOff(int index)
    Qt Code:
    1. QObject::connect(stationVector[k], SIGNAL(toggled(bool)), stationVector[k], SLOT(sentToggle(bool)));
    2. QObject::connect(stationVector[k], SIGNAL(toggled(int)), this, SLOT(on_stationCard_toggled(int)));
    3. QObject::connect(this, SIGNAL(turnOff(int)), stationVector[k], SLOT(turnOff(int)));
    4. [...]
    5. void MainWindow::on_stationCard_toggled(int index){
    6. activeIndex = index;
    7. emit turnOff(index);
    8. qDebug() << activeIndex;
    9. }
    To copy to clipboard, switch view to plain text mode 
    The first connection connects the basic toggle signal with my custom signal. I thought if I do this all cards would listen if a toggle signal is sent but since the code works I think only signal from card k is connected to the slot from card k. Why is this so? All cards are identical, how does the compiler know that only signal and slot from k should be used? The slot from k+1 is exactly the same!
    The second connection connects the card to my main window and sends active card index.
    The third connection connects my main window to the cards and tells them to un-select if they are not the active card.

  8. #8
    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: Several GroupBoxes with Radiobutton function

    Quote Originally Posted by KeineAhnung View Post
    Why is this so?
    Each "k" value addresses a different object.

    Quote Originally Posted by KeineAhnung View Post
    All cards are identical, how does the compiler know that only signal and slot from k should be used? The slot from k+1 is exactly the same!
    The method/slot is the same, but the object is not.
    The "this" context is different for every StationCard object.

    You can actually make that connection internal to StationCard, it is of no interest outside

    Qt Code:
    1. private slots:
    2. void sentToggle(bool on);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. StationCard::StationCard(){
    2. QHBoxLayout *layout = new QHBoxLayout;
    3. propLabel = new QLabel();
    4. layout->addWidget(propLabel, 10, Qt::AlignRight);
    5. setLayout(layout);
    6.  
    7. connect(this, SIGNAL(toggled(bool)), this, SLOT(sentToggle(bool)));
    8. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  9. #9
    Join Date
    Apr 2014
    Posts
    116
    Thanks
    8
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Several GroupBoxes with Radiobutton function

    Okay I see. Thanks for the explanation.

    I just found a flaw in the code. It is possible to de-select all cards but then the activeIndex would still point to the last active card. To set it back to -1 I did these changes:
    I added a private slot in my main window: void resetActiveIndex() and a corresponding signal in my card: void resetActiveIndex(). In the card .cpp I changed the toggled function to this:
    Qt Code:
    1. void StationCard::sentToggle(bool on){
    2. if(on){
    3. emit toggled(index);
    4. }else{
    5. emit resetActiveIndex();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    The slot functions in the main window I changed to this:
    Qt Code:
    1. void MainWindow::on_stationCard_toggled(int index){
    2. emit turnOff(index);
    3. activeIndex = index;
    4. qDebug() << activeIndex;
    5. }
    6. void MainWindow::resetActiveIndex(){
    7. activeIndex = -1;
    8. qDebug() << activeIndex;
    9. }
    To copy to clipboard, switch view to plain text mode 
    It is important to send the signal in on_stationCard_toggled before the activeIndex is set. Otherwise a change of active cards would always result in -1. And of course I wired the new functions:
    Qt Code:
    1. QObject::connect(stationVector[k], SIGNAL(resetActiveIndex()), this, SLOT(resetActiveIndex()));
    To copy to clipboard, switch view to plain text mode 

    I hope this example is still understandable. If not let me know and I try to write a nicer example. Or maybe someone of the pros likes to round it up in nicer syntax. ;-)

Similar Threads

  1. tab order and QButtonGroup and radioButton
    By somename in forum Qt Programming
    Replies: 14
    Last Post: 12th January 2016, 13:00
  2. radiobutton connect
    By saman_artorious in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2012, 13:37
  3. stylesheet of radiobutton
    By DEEPADEV in forum Qt Programming
    Replies: 2
    Last Post: 4th June 2012, 14:27
  4. radiobutton behavior on menubar
    By sp4rk3r in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2008, 05:37
  5. radiobutton output file
    By nitriles in forum Qt Programming
    Replies: 5
    Last Post: 20th September 2007, 10:04

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.