我使用此代码添加QSlider
和QSpinBox
QSpinBox *spinner2 = new QSpinBox;
QSlider *slider2 = new QSlider(Qt::Vertical);
spinner2->setRange(2,100);
slider2->setRange(2,100);
QObject::connect(spinner2, SIGNAL(valueChanged(int)), slider2, SLOT(setValue(int)));
QObject::connect(slider2, SIGNAL(valueChanged(int)), spinner2, SLOT(setValue(int)));
spinner2->setValue(10);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(slider2);
layout->addWidget(spinner2);
我想添加30个,我怎么能循环呢?
答案 0 :(得分:3)
我会用以下方式做到这一点:
QWidget *widget = new QWidget;
// The main layout of the widget that will hold multiple spinner-slider pairs.
QVBoxLayout *mainLayout = new QVBoxLayout;
for (int i = 0; i < 30; i++) {
QSpinBox *spinner2 = new QSpinBox(widget);
QSlider *slider2 = new QSlider(Qt::Vertical, widget);
spinner2->setRange(2, 100);
slider2->setRange(2, 100);
QObject::connect(spinner2, SIGNAL(valueChanged(int)), slider2, SLOT(setValue(int)));
QObject::connect(slider2, SIGNAL(valueChanged(int)), spinner2, SLOT(setValue(int)));
spinner2->setValue(10);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(slider2);
layout->addWidget(spinner2);
mainLayout->addLayout(layout);
}
widget->setLayout(mainLayout);
答案 1 :(得分:0)