2014-01-14 72 views
1

进入一个奇怪的问题,动态添加小部件到QScrollArea中包含的QVBoxLayout。如果我按照预期添加小部件,但是在删除所有小部件后,屏幕上仍然存在一些工件。见截图波纹管:QScrollArea和QVBoxLayout问题与动态添加小工具

enter image description here

参见下文代码:

ui_mainwindow.h

/******************************************************************************** 
** Form generated from reading UI file 'mainwindow.ui' 
** 
** Created by: Qt User Interface Compiler version 5.2.0 
** 
** WARNING! All changes made in this file will be lost when recompiling UI file! 
********************************************************************************/ 

#ifndef UI_MAINWINDOW_H 
#define UI_MAINWINDOW_H 

#include <QtCore/QVariant> 
#include <QtWidgets/QAction> 
#include <QtWidgets/QApplication> 
#include <QtWidgets/QButtonGroup> 
#include <QtWidgets/QHBoxLayout> 
#include <QtWidgets/QHeaderView> 
#include <QtWidgets/QMainWindow> 
#include <QtWidgets/QMenuBar> 
#include <QtWidgets/QPushButton> 
#include <QtWidgets/QScrollArea> 
#include <QtWidgets/QStatusBar> 
#include <QtWidgets/QToolBar> 
#include <QtWidgets/QVBoxLayout> 
#include <QtWidgets/QWidget> 

QT_BEGIN_NAMESPACE 

class Ui_MainWindow 
{ 
public: 
    QWidget *centralWidget; 
    QVBoxLayout *verticalLayout_2; 
    QScrollArea *scrollArea; 
    QWidget *scrollAreaWidgetContents; 
    QHBoxLayout *horizontalLayout_2; 
    QVBoxLayout *verticalLayout; 
    QPushButton *pushButton; 
    QPushButton *pushButton_2; 
    QMenuBar *menuBar; 
    QToolBar *mainToolBar; 
    QStatusBar *statusBar; 

    void setupUi(QMainWindow *MainWindow) 
    { 
     if (MainWindow->objectName().isEmpty()) 
      MainWindow->setObjectName(QStringLiteral("MainWindow")); 
     MainWindow->resize(600, 396); 
     centralWidget = new QWidget(MainWindow); 
     centralWidget->setObjectName(QStringLiteral("centralWidget")); 
     verticalLayout_2 = new QVBoxLayout(centralWidget); 
     verticalLayout_2->setSpacing(6); 
     verticalLayout_2->setContentsMargins(11, 11, 11, 11); 
     verticalLayout_2->setObjectName(QStringLiteral("verticalLayout_2")); 
     scrollArea = new QScrollArea(centralWidget); 
     scrollArea->setObjectName(QStringLiteral("scrollArea")); 
     scrollArea->setWidgetResizable(true); 
     scrollAreaWidgetContents = new QWidget(); 
     scrollAreaWidgetContents->setObjectName(QStringLiteral("scrollAreaWidgetContents")); 
     scrollAreaWidgetContents->setGeometry(QRect(0, 0, 574, 246)); 
     horizontalLayout_2 = new QHBoxLayout(scrollAreaWidgetContents); 
     horizontalLayout_2->setSpacing(6); 
     horizontalLayout_2->setContentsMargins(11, 11, 11, 11); 
     horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2")); 
     verticalLayout = new QVBoxLayout(); 
     verticalLayout->setSpacing(6); 
     verticalLayout->setObjectName(QStringLiteral("verticalLayout")); 

     horizontalLayout_2->addLayout(verticalLayout); 

     scrollArea->setWidget(scrollAreaWidgetContents); 

     verticalLayout_2->addWidget(scrollArea); 

     pushButton = new QPushButton(centralWidget); 
     pushButton->setObjectName(QStringLiteral("pushButton")); 

     verticalLayout_2->addWidget(pushButton); 

     pushButton_2 = new QPushButton(centralWidget); 
     pushButton_2->setObjectName(QStringLiteral("pushButton_2")); 

     verticalLayout_2->addWidget(pushButton_2); 

     MainWindow->setCentralWidget(centralWidget); 
     menuBar = new QMenuBar(MainWindow); 
     menuBar->setObjectName(QStringLiteral("menuBar")); 
     menuBar->setGeometry(QRect(0, 0, 600, 22)); 
     MainWindow->setMenuBar(menuBar); 
     mainToolBar = new QToolBar(MainWindow); 
     mainToolBar->setObjectName(QStringLiteral("mainToolBar")); 
     MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar); 
     statusBar = new QStatusBar(MainWindow); 
     statusBar->setObjectName(QStringLiteral("statusBar")); 
     MainWindow->setStatusBar(statusBar); 

     retranslateUi(MainWindow); 

     QMetaObject::connectSlotsByName(MainWindow); 
    } // setupUi 

    void retranslateUi(QMainWindow *MainWindow) 
    { 
     MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0)); 
     pushButton->setText(QApplication::translate("MainWindow", "add one", 0)); 
     pushButton_2->setText(QApplication::translate("MainWindow", "remove one", 0)); 
    } // retranslateUi 

}; 

namespace Ui { 
    class MainWindow: public Ui_MainWindow {}; 
} // namespace Ui 

QT_END_NAMESPACE 

#endif // UI_MAINWINDOW_H 

mainwindow.h

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

public: 
    explicit MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 

private slots: 
    void on_pushButton_clicked(); 

    void on_pushButton_2_clicked(); 

private: 
    Ui::MainWindow *ui; 
}; 

#endif // MAINWINDOW_H 

mainwindow.cpp

#include "mainwindow.h" 
#include "ui_mainwindow.h" 

#include <QLabel> 
#include <QDebug> 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 

void MainWindow::on_pushButton_clicked() 
{ 
    ui->verticalLayout->addWidget(new QLabel("This is a label", this)); 
    qDebug() << "Labels count: " << ui->verticalLayout->count(); 
} 

void MainWindow::on_pushButton_2_clicked() 
{ 
    delete ui->verticalLayout->takeAt(0); 
    qDebug() << "Labels count: " << ui->verticalLayout->count(); 
} 

的main.cpp

#include "mainwindow.h" 
#include <QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    MainWindow w; 
    w.show(); 

    return a.exec(); 
} 
+0

“屏幕上还有一些左转。 - >究竟剩下什么? – lpapp

+0

我的意思基本上是延长这个问题。目前尚不清楚。 – lpapp

+0

我会解释screenhot和文本之间的关系。我会澄清剩余的实际含义......即没有相应图像/图标的标签......然后我甚至会在屏幕截图上制作一个红色圆圈,以便更直观地显示出来。 – lpapp

回答

0

好像我也不得不删除QLayoutItem和窗口小部件,只是删除小部件或QLayoutItem(哪个我确实尝试过)不起作用,都必须删除,如下所示:

QLayoutItem *child = ui->verticalLayout->takeAt(0); 
    if (child) 
    { 
     delete child->widget(); 
     delete child; 
    }