我正在尝试创建一个简单的应用程序,其中包含2个pushButtons和带有radiobuttons的QWidgetList。需要手动创建所有Ui元素。我试图使用这样的信号,但Qt Creator不会编译它。
// window.h
namespace Ui {
class Window;
}
class Window : public QWidget
{
Q_OBJECT
public:
explicit Window(QWidget *parent = 0);
QPushButton * addButton;
QPushButton * removeButton;
QLineEdit textEdit;
QList<QRadioButton*> radioButtonList;
QGridLayout * layout;
public slots:
void addButton_clicked();
private:
QGroupBox *createRadiobuttonGroup();
QGroupBox *createPushButtonGroup();
QGroupBox *createTextEdit();
QGroupBox *createListWidget();
Ui::Window *ui;
};
//Window.cpp
Window::Window(QWidget *parent) :
QWidget(parent),
ui(new Ui::Window)
{
QGridLayout *grid = new QGridLayout;
grid->addWidget(createRadiobuttonGroup(), 0, 1);
grid->addWidget(createPushButtonGroup(), 1, 1);
grid->addWidget(createTextEdit(), 1, 0);
grid->addWidget(createListWidget(), 0, 0);
setLayout(grid);
setWindowTitle(tr("Group Box"));
resize(640, 480);
}
QGroupBox *Window::createListWidget()
{
QGroupBox *groupBox = new QGroupBox(tr("RadioButton List"));
QListWidget *radioButtonList = new QListWidget();
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(radioButtonList);
vbox->addStretch(1);
groupBox->setLayout(vbox);
QListWidgetItem *item = new QListWidgetItem();
radioButtonList -> addItem(item);
QString *value = new QString("Radiobutton value");
radioButtonList -> setItemWidget(item, new QRadioButton(tr(value->toUtf8())));
return groupBox;
}
QGroupBox *Window::createRadiobuttonGroup()
{
QGroupBox *groupBox = new QGroupBox();
QRadioButton *radio1 = new QRadioButton(tr("&Vetical"));
QRadioButton *radio2 = new QRadioButton(tr("&Horizontal"));
radio1->setChecked(true);
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(radio1);
vbox->addWidget(radio2);
vbox->addStretch(1);
groupBox->setLayout(vbox);
return groupBox;
}
QGroupBox *Window::createPushButtonGroup()
{
QGroupBox *groupBox = new QGroupBox();
QPushButton *addButton = new QPushButton(tr("&Add"));
QPushButton *renameButton = new QPushButton(tr("&Remove"));
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget(addButton);
vbox->addWidget(renameButton);
vbox->addStretch(1);
groupBox->setLayout(vbox);
return groupBox;
}
QGroupBox *Window::createTextEdit()
{
QGroupBox *groupBox = new QGroupBox();
QLineEdit *textEdit = new QLineEdit();
QVBoxLayout *vbox = new QVBoxLayout;
vbox->addWidget (textEdit);
vbox->addStretch(1);
groupBox->setLayout(vbox);
connect(addButton, SIGNAL(clicked()), this, SLOT(addButton_clicked()));
return groupBox;
}
void Window::addButton_clicked()
{
QRadioButton radio_btn = new QRadioButton("name");
vbox << radio_btn; // append radio button to the list
vbox->addWidget(radio_btn);
}
答案 0 :(得分:0)
void Window::addButton_clicked()
{
QRadioButton radio_btn = new QRadioButton("name");
vbox << radio_btn; // append radio button to the list
vbox->addWidget(radio_btn);
}
该范围内不存在变量vbox
。即使它确实如此,QVBoxLayout
也没有<<
运算符,因此您无法使用它。你也出于某种原因把它称为列表。