2014-01-14 94 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; 
    }