Results 1 to 6 of 6

Thread: Dynamically add QPushButtons to dynamically added QGroupBoxes

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

    Default Dynamically add QPushButtons to dynamically added QGroupBoxes

    How do I dynamically add QPushButtons to dynamically added QGroupBoxes? Here is my code (inside a childwindow) which dynamically adds Groupboxes along with their PushButtons. Onclick of one of these PushButtons there must be added PushButtons inside the belonging GroupBox. But how to place them inside the GroupBox?


    Qt Code:
    1. void MyWidget::systemButton(QGroupBox *groupBox[])
    2. {
    3. systemcounter += 1;
    4.  
    5. QPushButton *btnGtr = new QPushButton();
    6. btnGtr->setText(QString("Guitar: %1").arg(systemcounter));
    7. QVBoxLayout *vbox = new QVBoxLayout;
    8. vbox->addWidget(btnGtr);
    9. groupBox->setLayout(vbox);
    10.  
    11. mainLayout->addWidget(groupBox);
    12.  
    13. QObject::connect(btnGtr, SIGNAL (clicked()), this, SLOT (handleButton()));
    14. }
    15.  
    16. void MyWidget::on_addbutton_clicked()
    17. {
    18. pagecounter += 1;
    19. QGroupBox *groupBox = new QGroupBox(tr("&Page 1"));
    20. QPushButton *btnTest = new QPushButton();
    21. btnTest->setText(QString("Test: %1").arg(pagecounter));
    22.  
    23. top->addWidget(groupBox);
    24. top->addWidget(btnTest);
    25.  
    26. top->maximumSize();
    27.  
    28. mainLayout->addLayout(top);
    29.  
    30. QObject::connect(btnTest, SIGNAL (clicked()), this, SLOT (systemButton()));
    31. }
    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: Dynamically add QPushButtons to dynamically added QGroupBoxes

    I am not exactly sure what you are asking for, can you explain with pointing into your code?

    Do you mean that when "btnTest" is clicked, another button should be added to the same button group?

    Cheers,
    _

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

    Default Re: Dynamically add QPushButtons to dynamically added QGroupBoxes

    When on_addbutton_clicked() is triggered, groupBox and its button (btnTest) are added (working). When btnTest is clicked, systemButton() is triggered, which must add a button inside its groupBox. How to do this? I thought of passing QGroupBox as an argument of systemButton, like
    QObject::connect(btnTest, SIGNAL (clicked()), this, SLOT (systemButton(QGroupBox)));
    but then how to get it working?


    Added after 10 minutes:


    Doing it this way I get the following errormessage when on_addbutton_clicked() is triggered:
    QObject::connect: No such slot MyWidget::systembutton_clicked(QGroupBox) in ..\mywidget.cpp:75
    QObject::connect: (receiver name: 'MyWidget')
    Last edited by bchinfosieeuw; 22nd December 2016 at 14:20.

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

    Default Re: Dynamically add QPushButtons to dynamically added QGroupBoxes

    Yes, I would like to add the button to a buttonGroup (inside the GroupBox)

  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: Dynamically add QPushButtons to dynamically added QGroupBoxes

    Quote Originally Posted by bchinfosieeuw View Post
    QObject::connect(btnTest, SIGNAL (clicked()), this, SLOT (systemButton(QGroupBox)));
    That does not work, signal and slot need matching arguments.

    Since the clicked() signal has no argument, a slot connected to it can have on either.

    If you are in a C++11 capable environment you could connect to a lambda that captures thes groupbox.

    If you are not, then simply retrieve the button's parent widget() inside the slot.
    See QObject::sender() on how to get the button that triggered the slot.

    A third option it to have a special receiver class that knows the groupbox it is responsible for.

    Cheers,
    _

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

    Default Re: Dynamically add QPushButtons to dynamically added QGroupBoxes

    Your first solution works for me. I might ask some additional questions though...

Similar Threads

  1. Get name of QAction from Menu when dynamically added
    By yapatel in forum Qt Programming
    Replies: 1
    Last Post: 9th April 2014, 09:16
  2. Replies: 2
    Last Post: 3rd June 2013, 17:45
  3. Replies: 2
    Last Post: 30th January 2013, 16:20
  4. Replies: 6
    Last Post: 7th February 2012, 03:03
  5. Connecting dynamically added properties
    By stefkeB in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2008, 16:19

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.