[英]How can i toggle a widget visibility that is in a grid layout?
I want to do a list that changes it's fields numbers when the user changes a the value of a spinbox. 我想做一个列表,当用户更改一个spinbox的值时,它会改变它的字段数。 Something like this:
像这样的东西:
First 5 fields by default 默认情况下前5个字段
Then just 1 field for example 然后只有1个字段
And if the user wants to change it again, he can put 5 fields again. 如果用户想要再次更改它,他可以再次放置5个字段。
I made a GridLayout and a couple of QList, one for the Labels and the other one for LineEdits. 我制作了一个GridLayout和几个QList,一个用于标签,另一个用于LineEdits。 I did this:
我这样做了:
I create a basic case (with just 1 field) and i later add more on excecution time adding Widgets to the GridLayout by: 我创建了一个基本案例(只有1个字段),后来我添加了更多关于执行时间添加Widgets到GridLayout的执行时间:
gridLayout->addWidget(labels.at(x), 0, 1)
where labels is the QList. 其中label是QList。 it works fine to add widgets but i can't remove and add again.
它可以正常添加小部件,但我无法删除和添加。
i tried using 我试过用
gridLayout->removeWidget(lables.at(x), 0, 1)
labels.at(x)->hide()
label.at(x)->setVisible(false)
all works but i can't show it again with none of this: 所有的工作,但我不能再显示它没有这个:
gridLayout->addWidget(labels.at(x), 0, 1)
labels.at(x)->show()
label.at(x)->setVisible(true)
Layouts are handlers of the geometry of the widgets. 布局是小部件几何的处理程序。 If you use the
removeWidget()
function, you will only remove that element from the layout but it will still be visible. 如果使用
removeWidget()
函数,则只会从布局中删除该元素,但它仍然可见。 If you want it not to be visible you have to delete it with delete
. 如果你想让它不可见,你有删除它
delete
。
In the following example I show you how to add and remove the widgets using the valueChanged
signal of the QSpinBox . 在下面的示例中,我将向您展示如何使用QSpinBox的
valueChanged
信号添加和删除小部件。
void Dialog::on_spinBox_valueChanged(int arg1)
{
int nElements = labels.count();
//add
if(arg1 > nElements){
for(int i=nElements; i < arg1; i++){
QLabel *label = new QLabel(QString::number(i), this);
QLineEdit *line = new QLineEdit(QString::number(i), this);
labels.append(label);
lines.append(line);
ui->gridLayout->addWidget(label, i, 0, 1, 1);
ui->gridLayout->addWidget(line, i, 1, 1, 1);
}
}
//remove
else if(arg1 < nElements){
for(int i=arg1; i < nElements; i++){
QLabel *label = labels.at(i);
QLineEdit *line = lines.at(i);
ui->gridLayout->removeWidget(label);
ui->gridLayout->removeWidget(line);
labels.removeAt(i);
lines.removeAt(i);
delete label;
delete line;
}
}
}
Add: 加:
Remove: 去掉:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.