繁体   English   中英

动态删除QLabel

[英]Remove QLabel dynamically

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

我已经动态创建了一些伙伴( QLabel + QSpinBox ),以便刷新我的程序在我的循环的最后一千轮中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 ,我会创建新的布局并将其添加到视图中。

NUM_FAIL_COUNT个视图等于5

如您所见,这就是我动态创建的内容。 但是,如果有超过NUM_FAIL_COUNT ,我想删除整个第一个布局及其子项,以便在底部添加新的布局。 会发生的是第一个布局被移除,但不是它的子QLabel ,与QLabel + QLineEdit重叠。

在前几个好友中重叠

我试过通过children()调用,使用指针, deleteLater()以及尝试清除之前的布局来删除它。

我在想什么? 感谢您的回答。

您可以创建一个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);
    });
}

最好的祝福

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM