• QT之可自由折叠和展开的布局


    介绍和功能分析

            主要是实现控件的折叠和展开,类似抽屉控件,目前Qt自带的控件QToolBox具有这个功能,但是一次只能展开一个,所以针对自己的需求可以自己写一个类似的功能,这里实现的方法比较多,其实原理也比较简单,就是点一次隐藏,再点一次显示的效果。

    实现方法

    目前实现的方法有两种,原理基本相同,方法一是使用QPushButton结合SetVisible()函数来实现点击后隐藏和显示的效果。其UI布局如下:

    方法一使用点击QPushButton按钮来实现隐藏和显示QWidget的效果,再在QPushButton前增加辅助图标就实现了展开和收起的实际效果,其效果如下图:

    方法二中主要通过ToolBox进行调用,将传入的QWidget传入到ToolPage中,ToolPage自动填充到内容区,再将ToolPage添加到垂直布局中,ToolPage分为标题栏(QPushButton)和内容区(QWidget),点击QPushButton后,循环展开/折叠内容区。方法二与方法一实现原理相同,只是方法二对ToolBox进行了再次封装,然后通过ToolBox直接调用。其UI布局如下:

    代码实现

    首先重新写一个抽屉的类来创建控件相关功能:

    LockerButton.h

     

    1. #ifndef LOCKER_BUTTON_H
    2. #define LOCKER_BUTTON_H
    3. #include
    4. #include
    5. class QLabel;
    6. class LockerButton : public QPushButton
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit LockerButton(QWidget* parent = nullptr);
    11. // 设置按钮图标
    12. void SetImageLabel(const QPixmap &pixmap);
    13. // 设置按钮文字
    14. void SetTextLabel(QString text);
    15. // 返回图像label句柄
    16. QLabel* GetImageHandle();
    17. // 返回文字label句柄
    18. QLabel* GetTextHandle();
    19. private:
    20. // 按钮图标
    21. QLabel* m_imageLabel;
    22. // 按钮文字
    23. QLabel* m_textLabel;
    24. };
    25. #endif // LOCKER_BUTTON_H

     

    LockerButton类继承于PushButton类,主要进行控件的图标和文字设置。

    LockerButton.cpp

     

    1. #include "LockerButton.h"
    2. #include <QLabel>
    3. #include <QVBoxLayout>
    4. #include <QLineEdit>
    5. #include <QDoubleValidator>
    6. LockerButton::LockerButton(QWidget* parent)
    7. : QPushButton(parent)
    8. {
    9. m_imageLabel = new QLabel;
    10. m_imageLabel->setFixedWidth(20);
    11. m_imageLabel->setScaledContents(true);
    12. m_imageLabel->setStyleSheet("QLabel{background-color:transparent;}");
    13. m_textLabel = new QLabel;
    14. m_textLabel->setStyleSheet("QLabel{background-color:transparent;}");
    15. QHBoxLayout* mainLayout = new QHBoxLayout;
    16. mainLayout->addWidget(m_imageLabel);
    17. mainLayout->addWidget(m_textLabel);
    18. mainLayout->setMargin(0);
    19. mainLayout->setSpacing(0);
    20. this->setLayout(mainLayout);
    21. }
    22. void LockerButton::SetImageLabel(const QPixmap &pixmap)
    23. {
    24. m_imageLabel->setPixmap(pixmap);
    25. }
    26. void LockerButton::SetTextLabel(QString text)
    27. {
    28. m_textLabel->setText(text);
    29. }
    30. QLabel* LockerButton::GetImageHandle()
    31. {
    32. return m_imageLabel;
    33. }
    34. QLabel* LockerButton::GetTextHandle()
    35. {
    36. return m_textLabel;
    37. }

    接下来是调用,参考网上大部分是通过代码去创建控件,这里我使用的是PushButton控件在ui上实现,在Form上拉一个PushButton控件,然后提升为LockerButton,如下图:

     

    再接下来就是Widget的实现了

    widget.h

     

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. namespace Ui {
    5. class Widget;
    6. }
    7. class Widget : public QWidget
    8. {
    9. Q_OBJECT
    10. public:
    11. explicit Widget(QWidget *parent = 0);
    12. ~Widget();
    13. private slots:
    14. void on_ckbPic_clicked(bool checked);
    15. void on_ckbVideo_clicked(bool checked);
    16. private:
    17. Ui::Widget *ui;
    18. void initUI();
    19. int m_PicList;
    20. int m_VideoList;
    21. };
    22. #endif // WIDGET_H

    widget.cpp

     

    1. #pragma execution_character_set("utf-8")
    2. #include "widget.h"
    3. #include "ui_widget.h"
    4. #include <QDebug>
    5. Widget::Widget(QWidget *parent) :
    6. QWidget(parent),
    7. ui(new Ui::Widget)
    8. {
    9. ui->setupUi(this);
    10. initUI();
    11. }
    12. Widget::~Widget()
    13. {
    14. delete ui;
    15. }
    16. void Widget::initUI()
    17. {
    18. this->resize(300, 600);
    19. m_PicList = 0;
    20. m_VideoList = 0;
    21. ui->btnPic->SetTextLabel("图像");
    22. ui->btnPic->SetImageLabel(QPixmap(":/image/Collapse.png"));
    23. ui->btnPic->setStyleSheet("#btnPic{background-color:transparent}"
    24. "#btnPic:hover{background-color:rgba(195,195,195,0.4)}"
    25. "#btnPic:pressed{background-color:rgba(127,127,127,0.4)}");
    26. ui->btnVideo->SetTextLabel("视频");
    27. ui->btnVideo->SetImageLabel(QPixmap(":/image/Collapse.png"));
    28. ui->btnVideo->setStyleSheet("#btnVideo{background-color:transparent}"
    29. "#btnVideo:hover{background-color:rgba(195,195,195,0.4)}"
    30. "#btnVideo:pressed{background-color:rgba(127,127,127,0.4)}");
    31. QLabel* PicLabel = ui->btnPic->GetTextHandle();
    32. PicLabel->setStyleSheet("QLabel{color:rgba(183,71,42,1)}");
    33. PicLabel->setFont(QFont("图像", 10, QFont::Black));
    34. QLabel* VideoLabel = ui->btnVideo->GetTextHandle();
    35. VideoLabel->setStyleSheet("QLabel{color:rgba(183,71,42,1)}");
    36. VideoLabel->setFont(QFont("视频", 10, QFont::Black));
    37. ui->widget_Pic->setVisible(false);
    38. ui->widget_Video->setVisible(false);
    39. ui->btnPic->setEnabled(false);
    40. ui->btnVideo->setEnabled(false);
    41. connect(ui->btnPic, &LockerButton::clicked, [this](bool) {
    42. if (m_PicList % 2)
    43. {
    44. ui->btnPic->SetImageLabel(QPixmap(":/image/Collapse.png"));
    45. //m_sizeList偶数屏蔽Size列表界面,奇数显示Size列表界面
    46. ui->widget_Pic->setVisible(false);
    47. }
    48. else
    49. {
    50. ui->btnPic->SetImageLabel(QPixmap(":/image/Expand.png"));
    51. ui->widget_Pic->setVisible(true);
    52. }
    53. m_PicList++; });
    54. connect(ui->btnVideo, &LockerButton::clicked, [this](bool) {
    55. if (m_VideoList % 2)
    56. {
    57. ui->btnVideo->SetImageLabel(QPixmap(":/image/Collapse.png"));
    58. ui->widget_Video->setVisible(false);
    59. }
    60. else
    61. {
    62. ui->btnVideo->SetImageLabel(QPixmap(":/image/Expand.png"));
    63. ui->widget_Video->setVisible(true);
    64. }
    65. m_VideoList++; });
    66. }
    67. void Widget::on_ckbPic_clicked(bool checked)
    68. {
    69. if(checked)
    70. {
    71. qDebug()<<"复选框被选中";
    72. ui->btnPic->setEnabled(true);
    73. m_PicList++;
    74. ui->widget_Pic->setVisible(true);
    75. ui->btnPic->SetImageLabel(QPixmap(":/image/Expand.png"));
    76. }
    77. else
    78. {
    79. qDebug()<<"复选框被取消";
    80. ui->btnPic->setEnabled(false);
    81. m_PicList++;
    82. ui->widget_Pic->setVisible(false);
    83. ui->btnPic->SetImageLabel(QPixmap(":/image/Collapse.png"));
    84. }
    85. }
    86. void Widget::on_ckbVideo_clicked(bool checked)
    87. {
    88. if(checked)
    89. {
    90. qDebug()<<"复选框被选中";
    91. ui->btnVideo->setEnabled(true);
    92. m_VideoList++;
    93. ui->widget_Video->setVisible(true);
    94. ui->btnVideo->SetImageLabel(QPixmap(":/image/Expand.png"));
    95. }
    96. else
    97. {
    98. qDebug()<<"复选框被取消";
    99. ui->btnVideo->setEnabled(false);
    100. m_VideoList++;
    101. ui->widget_Video->setVisible(false);
    102. ui->btnVideo->SetImageLabel(QPixmap(":/image/Collapse.png"));
    103. }
    104. }

  • 相关阅读:
    分布式文件系统对比与选型参考
    SQL中的约束
    IPv6环境下的网络安全观察报告总结
    SPI机制
    C# 颜色与坐标
    c++模板进阶
    USACO18OPEN Talent Show G
    制造业质量管理如何实现数字化?
    使用Spark SQL读取阿里云OSS的数据
    Flutter系列文章-Flutter进阶
  • 原文地址:https://blog.csdn.net/hulinhulin/article/details/133658970