gegeben sind zwei GroupBoxen in einem BoxLayout. In der ersten Box sind mehrere Radiobuttons, die den Inhalt der zweiten Box alternativ bestimmen sollen.
Bisher gelingt es mittels je eines Signales eine entsprechende Box zu platzieren. Allerdings wird die alte Box nicht gelöscht, so dass beim Hin- und Herklicken ein endloses Band der abhängigen Boxen entsteht.
Kann jemand sagen, wie das geht - oder gibt es sinnvollere Alternativen?
Code: Alles auswählen
//main.cc
#include<QApplication>
#include"window.hh"
int main(int argc, char **argv){
QApplication a(argc, argv);
Window win;
win.show();
return a.exec();
}
Code: Alles auswählen
//window.hh
#ifndef WINDOW_HH
#define WINDOW_HH
#include<QWidget>
class QGroupBox;
class QVBoxLayout;
class QRadioButton;
class Window: public QWidget
{
Q_OBJECT
public:
Window(QWidget *parent = 0);
private:
QGroupBox *auswahlBox();
QGroupBox *ergebnisBox1();
QGroupBox *ergebnisBox2();
QGroupBox *ergebGBox;
QVBoxLayout *vbox, *vbox1;
QRadioButton *radio1;
QRadioButton *radio2;
public slots:
void eBox(bool);
void fBox(bool);
};
#endif
Code: Alles auswählen
#include<QtGui>
#include"window.hh"
Window::Window(QWidget *parent): QWidget(parent)
{
vbox = new QVBoxLayout;
vbox->addWidget(auswahlBox());
setLayout(vbox);
};
QGroupBox *Window::auswahlBox(){
QGroupBox *auswGBox = new QGroupBox("Auswahl");
radio1 = new QRadioButton("erster");
radio2 = new QRadioButton("zweiter");
vbox1 = new QVBoxLayout;
vbox1->addWidget(radio1);
vbox1->addWidget(radio2);
auswGBox->setLayout(vbox1);
connect(radio1, SIGNAL(toggled(bool)), this, SLOT(eBox(bool)));
connect(radio2, SIGNAL(toggled(bool)), this, SLOT(fBox(bool)));
return auswGBox;
}
QGroupBox *Window::ergebnisBox1()
{
ergebGBox = new QGroupBox("Ergebnis des ersten Knopfes");
QLabel *label = new QLabel("Text 1");
QVBoxLayout *lay = new QVBoxLayout;
lay->addWidget(label);
ergebGBox->setLayout(lay);
return ergebGBox;
}
QGroupBox *Window::ergebnisBox2()
{
ergebGBox = new QGroupBox("der zweite Knopf");
QLabel *label = new QLabel("zweiter Text");
QVBoxLayout *lay = new QVBoxLayout;
lay->addWidget(label);
ergebGBox->setLayout(lay);
return ergebGBox;
}
void Window::eBox(bool a)
{
if(a){
vbox->removeWidget(ergebnisBox2());
vbox->insertWidget(1, ergebnisBox1());
}
}
void Window::fBox(bool a)
{
if (a){
vbox->removeWidget(ergebnisBox1());
vbox->insertWidget(1, ergebnisBox2());
}
}