简体   繁体   English

如何切换网格布局中的窗口小部件可见性?

[英]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 . 在下面的示例中,我将向您展示如何使用QSpinBoxvalueChanged信号添加和删除小部件。

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.

相关问题 如何将动态小部件添加到 QT 中的小部件布局中 - How can i add dynamically widget into a widget's layout in QT 当我使用 Qt/C++ 将其他小部件添加到基本小部件时,如何增加基本小部件和所属垂直布局的高度? - How can I increase the height of a basic-widget and a belonging vertical layout when I add other widgets to the basic-widget using Qt/C++? 如何告诉 clang-format 缩进可见性修饰符? - How can I tell clang-format to indent visibility modifiers? 如何为小部件创建自定义布局 - How to create custom layout for widget 如何更改/交换小部件的布局? - How to change/swap the layout of a widget? 在使用网格布局并将重叠的小部件放置在距窗口边界特定距离的位置时,如何重叠qwidget? - How can I overlap qwidgets while using the grid layout and positioning overlapping widgets a particular distance from the window border? 如何根据同一布局中的另一个小部件将添加的小部件定位到布局中? - How to position an added widget to a layout based on another widget in the same layout? 如何通过单击图例来切换 QCustomPlot 图形的可见性 - How to toggle a QCustomPlot graph's visibility by clicking on the legend 使用属性 window 在 QT 网格布局中设置小部件的行和列 - Set the row and column of widget in QT grid layout using property window 如何更改 QTreeView 小部件中的图标 - How can i change icons in QTreeView widget
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM