我正在用Qt和C++创建一个图形用户界面。
我已经动态地创建了几对伙伴(QLabel + QSpinBox),以便刷新我的程序在循环的最后1000轮中发生的错误数量。
我创建这个的代码如下:
if((num_fotos % 1000) == 0){
if(num_layouts < NUM_FAIL_COUNT){
for(int i = 0; i < (buffer->length()-1); i++){
sum_100_fallos += int(buffer->at(i));
}
QLabel * label_1000 = new QLabel();
label_1000->setText("Fallos 1000_" + QString::number(num_fotos/1000) + ": ");
label_1000->setMinimumHeight(24);
QSpinBox * spinBox_1000 = new QSpinBox();
//newElements->append(label_1000);
spinBox_1000->setReadOnly(true);
spinBox_1000->setRange(0, 1000);
spinBox_1000->setMinimumHeight(24);
new_layouts[num_layouts] = new QHBoxLayout;
//newElements->append(spinBox_1000);
new_layouts[num_layouts]->addWidget(label_1000);
new_layouts[num_layouts]->addWidget(spinBox_1000);
spinBox_1000->setValue(num_fallos_1000);
num_fallos_1000 = 0;
ui->verticalLayout_5->addLayout(new_layouts[num_layouts]);
num_layouts++;
}else{
aux = num_layouts % NUM_FAIL_COUNT;
delete new_layouts[aux];
for(int i = 0; i < (buffer->length()-1); i++){
sum_100_fallos += int(buffer->at(i));
}
QLabel * label_1000 = new QLabel();
label_1000->setText("Fallos 1000_" + QString::number(num_fotos/1000) + ": ");
label_1000->setMinimumHeight(24);
QSpinBox * spinBox_1000 = new QSpinBox();
//newElements->append(label_1000);
spinBox_1000->setReadOnly(true);
spinBox_1000->setRange(0, 1000);
spinBox_1000->setMinimumHeight(24);
new_layouts[aux] = new QHBoxLayout;
//newElements->append(spinBox_1000);
new_layouts[aux]->addWidget(label_1000);
new_layouts[aux]->addWidget(spinBox_1000);
spinBox_1000->setValue(num_fallos_1000);
num_fallos_1000 = 0;
ui->verticalLayout_5->addLayout(new_layouts[aux]);
num_layouts++;
}
}NUM_FAIL_COUNT是要同时显示的(QLabels + QLineEdit)的编号。如果少于NUM_FAIL_COUNT,我会创建新的布局并将它们添加到视图中。

正如您所看到的,这是我动态创建的。但是,如果不止NUM_FAIL_COUNT,我想删除整个第一个布局及其子布局,以便在底部添加新布局。发生的情况是,第一个布局被移除,但它的子布局没有被移除,与QLabel + QLineEdit重叠。

我尝试过使用指针、deleteLater()和children()调用来访问它,并尝试在删除它之前清除布局。
我到底想错了什么?谢谢你的回答。
发布于 2017-05-24 03:18:38
您可以创建一个QList并存储指向创建的QWidgets的指针。下面是一个最小的例子:
mainwindow.hpp:
#ifndef MAINWINDOW_HPP
#define MAINWINDOW_HPP
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
QList<QWidget*> widgets;
};
#endif // MAINWINDOW_HPPmainwindow.cpp:
#include "mainwindow.hpp"
#include <QLabel>
#include <QVBoxLayout>
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent)
{
QVBoxLayout *mainLayout = new QVBoxLayout;
QVBoxLayout *widgetLayout = new QVBoxLayout;
QHBoxLayout *buttonLayout = new QHBoxLayout;
QPushButton *add = new QPushButton("Add", this);
QPushButton *remove = new QPushButton("Remove", this);
buttonLayout->addWidget(add);
buttonLayout->addWidget(remove);
mainLayout->addLayout(widgetLayout);
mainLayout->addLayout(buttonLayout);
setCentralWidget(new QWidget(this));
centralWidget()->setLayout(mainLayout);
connect(remove, &QPushButton::clicked, [=](){
if (widgets.size() > 0) {
QWidget *widget = widgets.takeLast();
widgetLayout->removeWidget(widget);
widget->deleteLater();
}
});
connect(add, &QPushButton::clicked, [=](){
QWidget *widget = new QLabel("Test " + QString::number(widgets.size()), this);
widgetLayout->addWidget(widget);
widgets.append(widget);
});
}诚挚的问候
https://stackoverflow.com/questions/44139883
复制相似问题