动态删除QLabel

时间:2017-05-23 16:05:36

标签: c++ qt

我正在使用Qt和C ++创建一个GUI。

我已经动态创建了一对好友(QLabel + QSpinBox),以便刷新我的程序在我的循环的最后一千轮中所犯的错误数。

创建此代码的代码:

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,我会创建新布局并将它们添加到视图中。

View with NUM_FAIL_COUNT equals five

正如您所看到的,这就是我动态创建的内容。 但是,如果有超过NUM_FAIL_COUNT的情况,我想删除整个第一个布局,并使用其子代,以便在底部添加新的布局。 会发生的情况是第一个布局被移除,但不会移除其子级,与QLabel + QLineEdit重叠。

Overlapping in first couple of buddies

我尝试通过children()调用访问它,使用指针deleteLater(),然后尝试清除以前删除的布局。

我在想什么? 谢谢你的回答。

1 个答案:

答案 0 :(得分:0)

您可以创建一个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_HPP

mainwindow.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);
    });
}

祝你好运