Results 1 to 5 of 5

Thread: Buttons inside QGroupBoxes onclick of QPushButtons.

  1. #1
    Join Date
    Aug 2016
    Posts
    36
    Qt products
    Qt4
    Platforms
    Windows

    Default Buttons inside QGroupBoxes onclick of QPushButtons.

    I have the following functions inside a childwindow. When onaddbutton_clicked() is triggered, a new QGroupBox and QPushbutton are added. Onclick of such a QPushButton, a button inside the QGroupBox must be added. But the QPushButtons must work independently from each other. Now systemcounter just keeps on increasing, causing buttons to be added inside all existing QGroupBoxes. I thought of making a systemcounter array, but then how to use the right systemcounter from the array? Also, is the QSignalMapper signalMapper working at all?
    So how to make the QPushbuttons work independently from each other?

    Qt Code:
    1. void MyWidget::systembutton_clicked(QGroupBox *groupBox, QVBoxLayout *vbox, int k)
    2. {
    3. systemcounter += 1;
    4.  
    5. QLayoutItem *child;
    6. while ((child = vbox->takeAt(0)) != 0){
    7. delete child;
    8. }
    9. for(int i = 0; i<systemcounter; i++){
    10. QPushButton *btnGtr = new QPushButton();
    11. btnGtr->setText(QString("Guitar: %1").arg(i+1));
    12. vbox->addWidget(btnGtr);
    13. QObject::connect(btnGtr, SIGNAL (clicked()), this, SLOT (handleButton()));
    14. }
    15.  
    16. mainLayout->addWidget(groupBox);
    17.  
    18. }
    19.  
    20. void MyWidget::onaddbutton_clicked()
    21. {
    22. pagecounter += 1;
    23.  
    24. QGroupBox *groupBox = new QGroupBox[pagecounter];
    25. QPushButton *btnTest = new QPushButton("G", this);
    26.  
    27. top->addWidget(groupBox);
    28. top->addWidget(btnTest);
    29. top->maximumSize();
    30.  
    31. mainLayout->addLayout(top);
    32.  
    33. QVBoxLayout *vbox = new QVBoxLayout;
    34. groupBox->setLayout(vbox);
    35. signalMapper = new QSignalMapper();
    36. for(int i = 0; i<pagecounter; i++){
    37. connect(btnTest, &QPushButton::clicked, [=] {
    38. emit systembutton_clicked(&groupBox[i], vbox, i);
    39. });
    40. signalMapper->setMapping(btnTest, i);
    41. }
    42. }
    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: Buttons inside QGroupBoxes onclick of QPushButtons.

    There are a couple of weird things here.

    Are you sure you want to create an array of QGroupBox in onaddbutton_clicked()?
    What exactly are your plans for the QSIgnalMapper?

    Not sure why you are wondering that btnTest adds things to all groupboxes, after all you connect it that way.

    Also no "emit" int that lambda, systembutton_clicked() is not a signal.

    Cheers,
    _

  3. #3
    Join Date
    Aug 2016
    Posts
    36
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Buttons inside QGroupBoxes onclick of QPushButtons.

    I am now trying the following: passing sender()->objectName().toInt() of a QPushButton in the systembutton_clicked(..., ..., ...) function. However, the program crashes on click of btnTest. So how can I let the program know which btnTest is clicked so a button can be added to the right groupBox? How to get it right so it not crashes?


    Qt Code:
    1. void MyWidget::systembutton_clicked(int j, QGroupBox *groupBox, QVBoxLayout *vbox)
    2. {
    3.  
    4. systemcounter[j] += 1;
    5. QLayoutItem *child;
    6. while ((child = vbox->takeAt(0)) != 0){
    7. delete child;
    8. }
    9. for(int i = 0; i<systemcounter[j]; i++){
    10. QPushButton *btnGtr = new QPushButton();
    11. btnGtr->setText(QString("Guitar: %1").arg(i+1));
    12. vbox->addWidget(btnGtr);
    13. QObject::connect(btnGtr, SIGNAL (clicked()), this, SLOT (handleButton()));
    14. }
    15.  
    16. mainLayout->addWidget(groupBox);
    17.  
    18. }
    19.  
    20. void MyWidget::onaddbutton_clicked()
    21. {
    22. pagecounter += 1;
    23. systemcounter = new int[pagecounter];
    24. systemcounter[pagecounter-1] = 0;
    25.  
    26. QGroupBox *groupBox = new QGroupBox(tr("&Page %1").arg(pagecounter));
    27. QPushButton *btnTest = new QPushButton("G", this);
    28. btnTest->setObjectName(QString::number(pagecounter-1));
    29.  
    30. top->addWidget(groupBox);
    31. top->addWidget(btnTest);
    32. top->maximumSize();
    33.  
    34. mainLayout->addLayout(top);
    35.  
    36. QVBoxLayout *vbox = new QVBoxLayout;
    37. groupBox->setLayout(vbox);
    38. connect(btnTest, &QPushButton::clicked, [=] {
    39. emit systembutton_clicked(sender()->objectName().toInt(), groupBox, vbox);
    40. });
    41. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2016
    Posts
    36
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Buttons inside QGroupBoxes onclick of QPushButtons.

    I now found out that it had to be:
    btnTest->objectName().toInt()
    Thank you.

  5. #5
    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: Buttons inside QGroupBoxes onclick of QPushButtons.

    You could also just set the number as a dynamic property.

    And are you sure you want to create a new array every time the first slot is called?

    Cheers,
    _

Similar Threads

  1. Replies: 5
    Last Post: 22nd December 2016, 23:04
  2. Replies: 1
    Last Post: 14th July 2012, 09:59
  3. getting to the buttons inside a QGridLayout
    By mr_kazoodle in forum Newbie
    Replies: 2
    Last Post: 19th February 2011, 17:48
  4. Replies: 0
    Last Post: 16th January 2011, 03:35
  5. resize buttons inside
    By eleanor in forum Qt Programming
    Replies: 8
    Last Post: 21st October 2007, 06:21

Tags for this Thread

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.