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?
Code:
void MyWidget
::systemButton(QGroupBox *groupBox
[]) {
systemcounter += 1;
btnGtr
->setText
(QString("Guitar: %1").
arg(systemcounter
));
vbox->addWidget(btnGtr);
groupBox->setLayout(vbox);
mainLayout->addWidget(groupBox);
QObject::connect(btnGtr,
SIGNAL (clicked
()),
this,
SLOT (handleButton
()));
}
void MyWidget::on_addbutton_clicked()
{
pagecounter += 1;
btnTest
->setText
(QString("Test: %1").
arg(pagecounter
));
top->addWidget(groupBox);
top->addWidget(btnTest);
top->maximumSize();
mainLayout->addLayout(top);
QObject::connect(btnTest,
SIGNAL (clicked
()),
this,
SLOT (systemButton
()));
}
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,
_
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')
Re: Dynamically add QPushButtons to dynamically added QGroupBoxes
Yes, I would like to add the button to a buttonGroup (inside the GroupBox)
Re: Dynamically add QPushButtons to dynamically added QGroupBoxes
Quote:
Originally Posted by
bchinfosieeuw
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,
_
Re: Dynamically add QPushButtons to dynamically added QGroupBoxes
Your first solution works for me. I might ask some additional questions though...