Results 1 to 5 of 5

Thread: Buttons inside QGroupBoxes onclick of QPushButtons.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. #2
    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.

  3. #3
    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, 22:04
  2. Replies: 1
    Last Post: 14th July 2012, 08:59
  3. getting to the buttons inside a QGridLayout
    By mr_kazoodle in forum Newbie
    Replies: 2
    Last Post: 19th February 2011, 16:48
  4. Replies: 0
    Last Post: 16th January 2011, 02:35
  5. resize buttons inside
    By eleanor in forum Qt Programming
    Replies: 8
    Last Post: 21st October 2007, 05: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.