首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态删除QLabel

动态删除QLabel
EN

Stack Overflow用户
提问于 2017-05-24 00:05:36
回答 1查看 1.6K关注 0票数 1

我正在用Qt和C++创建一个图形用户界面。

我已经动态地创建了几对伙伴(QLabel + QSpinBox),以便刷新我的程序在循环的最后1000轮中发生的错误数量。

我创建这个的代码如下:

代码语言:javascript
复制
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()调用来访问它,并尝试在删除它之前清除布局。

我到底想错了什么?谢谢你的回答。

EN

回答 1

Stack Overflow用户

发布于 2017-05-24 03:18:38

您可以创建一个QList并存储指向创建的QWidgets的指针。下面是一个最小的例子:

mainwindow.hpp:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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);
    });
}

诚挚的问候

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44139883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档