源文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
{
ui->setupUi(this);
layout = new QVBoxLayout;
containWidget = new QWidget;
for (int i = 0; i < 1; ++i)
{
QLabel *label = new QLabel();
QPixmap pixmap;
pixmap.load(":/timg.jpg");
label->setPixmap(pixmap);
layout->addWidget(label);
}
containWidget->setLayout(layout);
ui->scrollArea->setWidgetResizable(true);
ui->scrollArea->setWidget(containWidget);
//this->setCentralWidget(ui->scrollArea);
connect(ui->AddBottun,SIGNAL(clicked(bool)),SLOT(addWidget()));
connect(ui->DelButton,SIGNAL(clicked(bool)),SLOT(delWidget()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::addWidget()
{
for (int i = 0; i < 1; ++i)
{
QLabel *label = new QLabel();
QPixmap pixmap;
pixmap.load(":/timg.jpg");
label->setPixmap(pixmap);
layout->addWidget(label);
}
}
void MainWindow::delWidget()
{
/* //单次删除控件
QLabel *fm = ui->scrollArea->findChild<QLabel*>(); //找到指定名称的控件
layout->removeWidget(fm);
fm->deleteLater();
*/
//删除所有Label 子控件
QList<QLabel *> labelList= ui->scrollArea->findChildren<QLabel *>();
for(int i=0;i<labelList.length();++i)
{
QLabel *fm =labelList.at(i);
layout->removeWidget(fm);
fm->deleteLater();
}
}
头文件
#include <QMainWindow>
#include<qlayout.h>
#include <qlabel>
#include <QBuffer>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void addWidget();
void delWidget();
private:
Ui::MainWindow *ui;
QVBoxLayout *layout;
QWidget *containWidget;
};
本文福利,莬费领取Qt开发学习资料包、技术视频,内容包括(C++语言基础,C++设计模式,Qt编程入门,QT信号与槽机制,QT界面开发-图像绘制,QT网络,QT数据库编程,QT项目实战,QSS,OpenCV,Quick模块,面试题等等)↓↓↓↓↓↓见下面↓↓文章底部点击莬费领取↓↓