panelwidget.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #pragma execution_character_set("utf-8")
  2. #include "panelwidget.h"
  3. #include "qscrollarea.h"
  4. #include "qframe.h"
  5. #include "qboxlayout.h"
  6. #include "qdebug.h"
  7. PanelWidget::PanelWidget(QWidget *parent) : QWidget(parent)
  8. {
  9. scrollArea = new QScrollArea(this);
  10. scrollArea->setObjectName("scrollAreaMain");
  11. scrollArea->setWidgetResizable(true);
  12. scrollAreaWidgetContents = new QWidget();
  13. scrollAreaWidgetContents->setGeometry(QRect(0, 0, 100, 100));
  14. verticalLayout = new QVBoxLayout(scrollAreaWidgetContents);
  15. verticalLayout->setSpacing(0);
  16. verticalLayout->setContentsMargins(0, 0, 0, 0);
  17. frame = new QFrame(scrollAreaWidgetContents);
  18. frame->setObjectName("frameMain");
  19. gridLayout = new QGridLayout(frame);
  20. gridLayout->setSpacing(0);
  21. gridLayout->setContentsMargins(0, 0, 0, 0);
  22. verticalLayout->addWidget(frame);
  23. scrollArea->setWidget(scrollAreaWidgetContents);
  24. frame->setStyleSheet("QFrame#frameMain{border-width:0px;}");
  25. margin = 0;
  26. space = 0;
  27. autoWidth = false;
  28. autoHeight = false;
  29. }
  30. void PanelWidget::resizeEvent(QResizeEvent *)
  31. {
  32. scrollArea->resize(this->size());
  33. }
  34. QSize PanelWidget::sizeHint() const
  35. {
  36. return QSize(300, 200);
  37. }
  38. QSize PanelWidget::minimumSizeHint() const
  39. {
  40. return QSize(20, 20);
  41. }
  42. int PanelWidget::getMargin() const
  43. {
  44. return this->margin;
  45. }
  46. int PanelWidget::getSpace() const
  47. {
  48. return this->space;
  49. }
  50. bool PanelWidget::getAutoWidth() const
  51. {
  52. return this->autoWidth;
  53. }
  54. bool PanelWidget::getAutoHeight() const
  55. {
  56. return this->autoHeight;
  57. }
  58. QList<QWidget *> PanelWidget::getWidgets()
  59. {
  60. return this->widgets;
  61. }
  62. int PanelWidget::getColumnCount()
  63. {
  64. return this->columnCount;
  65. }
  66. void PanelWidget::clearWidgets()
  67. {
  68. foreach (QWidget* w, widgets) {
  69. delete w;
  70. }
  71. widgets.clear();
  72. }
  73. void PanelWidget::setWidget(QList<QWidget *> widgets, int columnCount)
  74. {
  75. this->widgets = widgets;
  76. this->columnCount = columnCount;
  77. int row = 0;
  78. int column = 0;
  79. int index = 0;
  80. //先把之前的所有移除并不可见
  81. foreach (QWidget *widget, widgets) {
  82. gridLayout->removeWidget(widget);
  83. widget->setVisible(false);
  84. }
  85. //重新添加到布局中并可见
  86. foreach (QWidget *widget, widgets) {
  87. gridLayout->addWidget(widget, row, column);
  88. widget->setVisible(true);
  89. column++;
  90. index++;
  91. if (index % columnCount == 0) {
  92. row++;
  93. column = 0;
  94. }
  95. }
  96. row++;
  97. //设置右边弹簧
  98. if (!autoWidth) {
  99. QSpacerItem *hSpacer = new QSpacerItem(1, 1, QSizePolicy::Expanding, QSizePolicy::Minimum);
  100. gridLayout->addItem(hSpacer, 0, gridLayout->columnCount());
  101. }
  102. //设置底边弹簧
  103. if (!autoHeight) {
  104. QSpacerItem *vSpacer = new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding);
  105. gridLayout->addItem(vSpacer, row, 0);
  106. }
  107. }
  108. void PanelWidget::setMargin(int left, int top, int right, int bottom)
  109. {
  110. gridLayout->setContentsMargins(left, top, right, bottom);
  111. }
  112. void PanelWidget::setMargin(int margin)
  113. {
  114. if (this->margin != margin) {
  115. setMargin(margin, margin, margin, margin);
  116. }
  117. }
  118. void PanelWidget::setSpace(int space)
  119. {
  120. if (this->space != space) {
  121. gridLayout->setSpacing(space);
  122. }
  123. }
  124. void PanelWidget::setAutoWidth(bool autoWidth)
  125. {
  126. if (this->autoWidth != autoWidth) {
  127. this->autoWidth = autoWidth;
  128. }
  129. }
  130. void PanelWidget::setAutoHeight(bool autoHeight)
  131. {
  132. if (this->autoHeight != autoHeight) {
  133. this->autoHeight = autoHeight;
  134. }
  135. }