其他分享
首页 > 其他分享> > Qt---ToolBox自由伸展

Qt---ToolBox自由伸展

作者:互联网

参考:Qt实战12.可自由展开的ToolBox - Qt小罗 - 博客园 (cnblogs.com)  

 1 MainWindow::MainWindow(QWidget *parent)
 2     : QMainWindow(parent)
 3     , ui(new Ui::MainWindow)
 4 {
 5     ui->setupUi(this);
 6     setWindowTitle(QStringLiteral("自定义ToolBox演示  Qt小罗"));
 7     setMinimumSize(300, 400);
 8     resize(300, 450);
 9     QLabel *label[4];
10     for(int i = 0; i < 4; i++){
11         label[i] = new QLabel;
12         label[i]->setFixedSize(300, 200);
13     }
14     label[0]->setStyleSheet("QLabel{background-color:red;}");
15     label[1]->setStyleSheet("QLabel{background-color:green;}");
16     label[2]->setStyleSheet("QLabel{background-color:blue;}");
17     label[3]->setStyleSheet("QLabel{background-color:black;}");
18 
19     ToolBox *toolBox = new ToolBox(this);
20     toolBox->addWidget(QStringLiteral("Qt小罗Box1"), label[0]);
21     toolBox->addWidget(QStringLiteral("Qt小罗Box2"), label[1]);
22     toolBox->addWidget(QStringLiteral("Qt小罗Box3"), label[2]);
23     toolBox->addWidget(QStringLiteral("Qt小罗Box4"), label[3]);
24 
25     setCentralWidget(toolBox);
26 }
 1 #ifndef TOOLBOX_H
 2 #define TOOLBOX_H
 3 
 4 #include <QWidget>
 5 #include <QScrollArea>
 6 #include <QVBoxLayout>
 7 #include "toolpage.hpp"
 8 
 9 class QVBoxLayout;
10 class ToolBox : public QWidget
11 {
12     Q_OBJECT
13 
14 private:
15     QVBoxLayout *m_pContentVBoxLayout;
16     QScrollArea *scrollArea;
17 
18 public:
19     explicit ToolBox(QWidget *parent = nullptr)
20         : QWidget(parent)
21     {
22         QWidget *widget = new QWidget(this);
23 
24         scrollArea = new QScrollArea(this);
25         scrollArea->setWidgetResizable(true);
26 
27         QVBoxLayout *outerLayout = new QVBoxLayout(this);
28         outerLayout->setSpacing(0);
29         outerLayout->setContentsMargins(0, 0, 0, 0);
30         outerLayout->addWidget(scrollArea);
31         this->setLayout(outerLayout);
32         scrollArea->setWidget(widget);
33 
34         m_pContentVBoxLayout = new QVBoxLayout;
35         m_pContentVBoxLayout->setSpacing(2);
36         m_pContentVBoxLayout->setContentsMargins(0, 0, 0, 0);
37 
38         QVBoxLayout *innerLayout = new QVBoxLayout(widget);
39         innerLayout->setSpacing(2);
40         innerLayout->setContentsMargins(0, 0, 0, 0);
41         innerLayout->addLayout(m_pContentVBoxLayout);
42         innerLayout->addStretch(1);
43     }
44 
45     void addWidget(const QString &title, QWidget *widget)
46     {
47         m_pContentVBoxLayout->addWidget(new ToolPage(title, widget, this));
48     }
49 
50 };
51 
52 #endif // TOOLBOX_H
 1 #ifndef TOOLPAGE_H
 2 #define TOOLPAGE_H
 3 
 4 #include <QWidget>
 5 #include <QPushButton>
 6 #include <QVBoxLayout>
 7 
 8 namespace Ui {
 9 class ToolPage;
10 }
11 
12 class QFormLayout;
13 class QLabel;
14 
15 class ToolPage : public QWidget
16 {
17     Q_OBJECT
18 
19 private:
20     QVBoxLayout *widgetLayout;
21     QWidget *widgetContent;
22     QPushButton *pushButtonFold;
23 
24 public:
25     explicit ToolPage(const QString &title, QWidget *widget, QWidget *parent = nullptr)
26         : QWidget(parent)
27     {
28         widgetContent = new QWidget(this);
29         widgetContent->hide();
30         widgetLayout = new QVBoxLayout(widgetContent);
31         widgetLayout->setContentsMargins(0, 0, 0, 0);
32         widgetLayout->setSpacing(0);
33 
34         pushButtonFold = new QPushButton(this);
35         pushButtonFold->setMinimumHeight(26);
36 
37         QVBoxLayout *mainLayout = new QVBoxLayout(this);
38         mainLayout->addWidget(pushButtonFold);
39         mainLayout->addWidget(widgetContent);
40         mainLayout->setSpacing(0);
41         mainLayout->setContentsMargins(0, 0, 0, 0);
42         connect(pushButtonFold, &QPushButton::clicked, this, [=](){
43             widgetContent->setVisible(!widgetContent->isVisible());
44         });
45         pushButtonFold->setText(title);
46         widgetLayout->addWidget(widget);
47     }
48 
49 private:
50     void addWidget(const QString &title, QWidget *widget)
51     {
52         pushButtonFold->setText(title);
53         widgetLayout->addWidget(widget);
54     }
55 
56 
57 
58 };
59 
60 #endif // TOOLPAGE_H

 

标签:widget,Qt,QVBoxLayout,label,---,QWidget,addWidget,new,ToolBox
来源: https://www.cnblogs.com/endenvor/p/15036598.html