简体   繁体   English

动态删除QLabel

[英]Remove QLabel dynamically

I am working to create a GUI with Qt and C++. 我正在使用Qt和C ++创建GUI。

I have create dynamically some couples of buddies ( QLabel + QSpinBox ) in order to refresh the number of errors that my program makes in the last thousand round to my loop. 我已经动态创建了一些伙伴( QLabel + QSpinBox ),以便刷新我的程序在我的循环的最后一千轮中QSpinBox的错误数。

My code to create this: 我创建的代码:

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 is the numbers of ( QLabels + QLineEdit ) to show simultaneously. NUM_FAIL_COUNT是要同时显示的( QLabels + QLineEdit )数字。 If there is less than NUM_FAIL_COUNT , I create new layouts and add them to the view. 如果小于NUM_FAIL_COUNT ,我会创建新的布局并将其添加到视图中。

NUM_FAIL_COUNT个视图等于5

As you can see, this is what I am creating dynamically. 如您所见,这就是我动态创建的内容。 But, if there are more than NUM_FAIL_COUNT , I want to remove the entire first layout, with its children in order to add the new one at the bottom. 但是,如果有超过NUM_FAIL_COUNT ,我想删除整个第一个布局及其子项,以便在底部添加新的布局。 What happens is that the first layout is removed, but not its children, overlapping the QLabel + QLineEdit . 会发生的是第一个布局被移除,但不是它的子QLabel ,与QLabel + QLineEdit重叠。

在前几个好友中重叠

I have tried accessing to it by children() call, with pointers, deleteLater() , and trying to clearing layout previously to its deletion. 我试过通过children()调用,使用指针, deleteLater()以及尝试清除之前的布局来删除它。

What am I thinking wrong? 我在想什么? Thanks for your answer. 感谢您的回答。

You could create a QList and store pointers to the created QWidgets. 您可以创建一个QList并存储指向创建的QWidgets的指针。 Here is a minimal example: 这是一个最小的例子:

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

Best regards 最好的祝福

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

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