pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

<i id='qVvr7'><tr id='qVvr7'><dt id='qVvr7'><q id='qVvr7'><span id='qVvr7'><b id='qVvr7'><form id='qVvr7'><ins id='qVvr7'></ins><ul id='qVvr7'></ul><sub id='qVvr7'></sub></form><legend id='qVvr7'></legend><bdo id='qVvr7'><pre id='qVvr7'><center id='qVvr7'></center></pre></bdo></b><th id='qVvr7'></th></span></q></dt></tr></i><div class="1dx7jfb" id='qVvr7'><tfoot id='qVvr7'></tfoot><dl id='qVvr7'><fieldset id='qVvr7'></fieldset></dl></div>

    1. <small id='qVvr7'></small><noframes id='qVvr7'>

      <tfoot id='qVvr7'></tfoot>

      1. <legend id='qVvr7'><style id='qVvr7'><dir id='qVvr7'><q id='qVvr7'></q></dir></style></legend>
        • <bdo id='qVvr7'></bdo><ul id='qVvr7'></ul>
      2. 帶有 FlowLayout 小部件的 QScrollArea 未正確調整大小

        QScrollArea with FlowLayout widgets not resizing properly(帶有 FlowLayout 小部件的 QScrollArea 未正確調整大小)
            <bdo id='KLEL9'></bdo><ul id='KLEL9'></ul>
              <tfoot id='KLEL9'></tfoot>

              <small id='KLEL9'></small><noframes id='KLEL9'>

            • <legend id='KLEL9'><style id='KLEL9'><dir id='KLEL9'><q id='KLEL9'></q></dir></style></legend>
                <tbody id='KLEL9'></tbody>
            • <i id='KLEL9'><tr id='KLEL9'><dt id='KLEL9'><q id='KLEL9'><span id='KLEL9'><b id='KLEL9'><form id='KLEL9'><ins id='KLEL9'></ins><ul id='KLEL9'></ul><sub id='KLEL9'></sub></form><legend id='KLEL9'></legend><bdo id='KLEL9'><pre id='KLEL9'><center id='KLEL9'></center></pre></bdo></b><th id='KLEL9'></th></span></q></dt></tr></i><div class="p7jpfrd" id='KLEL9'><tfoot id='KLEL9'></tfoot><dl id='KLEL9'><fieldset id='KLEL9'></fieldset></dl></div>

                  本文介紹了帶有 FlowLayout 小部件的 QScrollArea 未正確調整大小的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我想創建一個類似于 KDE(或 Gnome 或 MacOS)系統設置的小部件(例如,像這張圖片)

                  I want to create a widget similar to the KDE (or Gnome or MacOS) system settings (e.g., like this picture)

                  我已經從 Qt 文檔示例.

                  如果我將一些 FlowLayout 小部件(包裝在帶有 QVBoxLayout 的容器小部件中)放入 QScrollArea 并調整 QSrollArea 的大小,那么一切都會按照應有的方式流動和重新布局.

                  If I put some FlowLayout widgets (wrapped in a container widget with a QVBoxLayout) into a QScrollArea and resize the QSrollArea, everything flows and re-layouts as it shoulds.

                  然而,如果我增加滾動區域的寬度以減少它需要的高度,滾動區域仍然認為它的小部件需要其 minimumWidth 的原始高度:

                  However, if I increase the scroll area’s width so that it needs less height, the scroll area’s still thinks that its widgets require the orginal height for their minimumWidth:

                  如何使用子級的實際高度更新滾動區域,以便在不再需要垂直滾動條時消失?

                  How can I can I update the scroll area with the actual height of its child so that the vertical scroll bar disappears when it’s no longer needed?

                  您將在下面找到 FlowLayout 的 (Python) 實現,并在 __main__ 塊中找到實際示例.

                  Below, you’ll find the (Python) implementation of the FlowLayout and in the __main__ block the actual example.

                  干杯,斯蒂芬

                  """
                  PyQt5 port of the `layouts/flowlayout
                  <https://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html>`_ example
                  from Qt5.
                  
                  Usage:
                  
                      python3 -m pip install pyqt5
                      python3 flow_layout.py
                  
                  """
                  from PyQt5.QtCore import pyqtSignal, QPoint, QRect, QSize, Qt
                  from PyQt5.QtWidgets import QLayout, QSizePolicy, QSpacerItem
                  
                  
                  class FlowLayout(QLayout):
                      """A ``QLayout`` that aranges its child widgets horizontally and
                      vertically.
                  
                      If enough horizontal space is available, it looks like an ``HBoxLayout``,
                      but if enough space is lacking, it automatically wraps its children into
                      multiple rows.
                  
                      """
                      heightChanged = pyqtSignal(int)
                  
                      def __init__(self, parent=None, margin=0, spacing=-1):
                          super().__init__(parent)
                          if parent is not None:
                              self.setContentsMargins(margin, margin, margin, margin)
                          self.setSpacing(spacing)
                  
                          self._item_list = []
                  
                      def __del__(self):
                          while self.count():
                              self.takeAt(0)
                  
                      def addItem(self, item):  # pylint: disable=invalid-name
                          self._item_list.append(item)
                  
                      def addSpacing(self, size):  # pylint: disable=invalid-name
                          self.addItem(QSpacerItem(size, 0, QSizePolicy.Fixed, QSizePolicy.Minimum))
                  
                      def count(self):
                          return len(self._item_list)
                  
                      def itemAt(self, index):  # pylint: disable=invalid-name
                          if 0 <= index < len(self._item_list):
                              return self._item_list[index]
                          return None
                  
                      def takeAt(self, index):  # pylint: disable=invalid-name
                          if 0 <= index < len(self._item_list):
                              return self._item_list.pop(index)
                          return None
                  
                      def expandingDirections(self):  # pylint: disable=invalid-name,no-self-use
                          return Qt.Orientations(Qt.Orientation(0))
                  
                      def hasHeightForWidth(self):  # pylint: disable=invalid-name,no-self-use
                          return True
                  
                      def heightForWidth(self, width):  # pylint: disable=invalid-name
                          height = self._do_layout(QRect(0, 0, width, 0), True)
                          return height
                  
                      def setGeometry(self, rect):  # pylint: disable=invalid-name
                          super().setGeometry(rect)
                          self._do_layout(rect, False)
                  
                      def sizeHint(self):  # pylint: disable=invalid-name
                          return self.minimumSize()
                  
                      def minimumSize(self):  # pylint: disable=invalid-name
                          size = QSize()
                  
                          for item in self._item_list:
                              minsize = item.minimumSize()
                              extent = item.geometry().bottomRight()
                              size = size.expandedTo(QSize(minsize.width(), extent.y()))
                  
                          margin = self.contentsMargins().left()
                          size += QSize(2 * margin, 2 * margin)
                          return size
                  
                      def _do_layout(self, rect, test_only=False):
                          m = self.contentsMargins()
                          effective_rect = rect.adjusted(+m.left(), +m.top(), -m.right(), -m.bottom())
                          x = effective_rect.x()
                          y = effective_rect.y()
                          line_height = 0
                  
                          for item in self._item_list:
                              wid = item.widget()
                  
                              space_x = self.spacing()
                              space_y = self.spacing()
                              if wid is not None:
                                  space_x += wid.style().layoutSpacing(
                                      QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Horizontal)
                                  space_y += wid.style().layoutSpacing(
                                      QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Vertical)
                  
                              next_x = x + item.sizeHint().width() + space_x
                              if next_x - space_x > effective_rect.right() and line_height > 0:
                                  x = effective_rect.x()
                                  y = y + line_height + space_y
                                  next_x = x + item.sizeHint().width() + space_x
                                  line_height = 0
                  
                              if not test_only:
                                  item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
                  
                              x = next_x
                              line_height = max(line_height, item.sizeHint().height())
                  
                          new_height = y + line_height - rect.y()
                          self.heightChanged.emit(new_height)
                          return new_height
                  
                  
                  if __name__ == '__main__':
                      import sys
                      from PyQt5.QtWidgets import QApplication, QPushButton, QScrollArea, QVBoxLayout, QWidget
                  
                  
                      class Container(QWidget):
                          def __init__(self):
                              super().__init__()
                              self.setLayout(QVBoxLayout())
                              self._widgets = []
                  
                          def sizeHint(self):
                              w = self.size().width()
                              h = 0
                              for widget in self._widgets:
                                  h += widget.layout().heightForWidth(w)
                  
                              sh = super().sizeHint()
                              print(sh)
                              print(w, h)
                              return sh
                  
                          def add_widget(self, widget):
                              self._widgets.append(widget)
                              self.layout().addWidget(widget)
                  
                          def add_stretch(self):
                              self.layout().addStretch()
                  
                  
                      app = QApplication(sys.argv)  # pylint: disable=invalid-name
                      container = Container()
                      for i in range(2):
                          w = QWidget()
                          w.setWindowTitle('Flow Layout')
                          l = FlowLayout(w, 10)
                          w.setLayout(l)
                          l.addWidget(QPushButton('Short'))
                          l.addWidget(QPushButton('Longer'))
                          l.addWidget(QPushButton('Different text'))
                          l.addWidget(QPushButton('More text'))
                          l.addWidget(QPushButton('Even longer button text'))
                          container.add_widget(w)
                      container.add_stretch()
                  
                      sa = QScrollArea()
                      sa.setWidgetResizable(True)
                      sa.setWidget(container)
                      sa.show()
                  
                      sys.exit(app.exec_())
                  

                  推薦答案

                  解決方案非常簡單:使用 FlowLayout 的 heightChanged 信號更新容器的最小高度(ScrollArea 的小部件).

                  The solution was (surprisingly) simple: Use the FlowLayout’s heightChanged signal to update the minimum height of the container (the ScrollArea’s widget).

                  這是一個工作示例:

                  """
                  PyQt5 port of the `layouts/flowlayout
                  <https://doc.qt.io/qt-5/qtwidgets-layouts-flowlayout-example.html>`_ example
                  from Qt5.
                  
                  """
                  from PyQt5.QtCore import pyqtSignal, QPoint, QRect, QSize, Qt
                  from PyQt5.QtWidgets import QLayout, QSizePolicy, QSpacerItem
                  
                  
                  class FlowLayout(QLayout):
                      """A ``QLayout`` that aranges its child widgets horizontally and
                      vertically.
                  
                      If enough horizontal space is available, it looks like an ``HBoxLayout``,
                      but if enough space is lacking, it automatically wraps its children into
                      multiple rows.
                  
                      """
                      heightChanged = pyqtSignal(int)
                  
                      def __init__(self, parent=None, margin=0, spacing=-1):
                          super().__init__(parent)
                          if parent is not None:
                              self.setContentsMargins(margin, margin, margin, margin)
                          self.setSpacing(spacing)
                  
                          self._item_list = []
                  
                      def __del__(self):
                          while self.count():
                              self.takeAt(0)
                  
                      def addItem(self, item):  # pylint: disable=invalid-name
                          self._item_list.append(item)
                  
                      def addSpacing(self, size):  # pylint: disable=invalid-name
                          self.addItem(QSpacerItem(size, 0, QSizePolicy.Fixed, QSizePolicy.Minimum))
                  
                      def count(self):
                          return len(self._item_list)
                  
                      def itemAt(self, index):  # pylint: disable=invalid-name
                          if 0 <= index < len(self._item_list):
                              return self._item_list[index]
                          return None
                  
                      def takeAt(self, index):  # pylint: disable=invalid-name
                          if 0 <= index < len(self._item_list):
                              return self._item_list.pop(index)
                          return None
                  
                      def expandingDirections(self):  # pylint: disable=invalid-name,no-self-use
                          return Qt.Orientations(Qt.Orientation(0))
                  
                      def hasHeightForWidth(self):  # pylint: disable=invalid-name,no-self-use
                          return True
                  
                      def heightForWidth(self, width):  # pylint: disable=invalid-name
                          height = self._do_layout(QRect(0, 0, width, 0), True)
                          return height
                  
                      def setGeometry(self, rect):  # pylint: disable=invalid-name
                          super().setGeometry(rect)
                          self._do_layout(rect, False)
                  
                      def sizeHint(self):  # pylint: disable=invalid-name
                          return self.minimumSize()
                  
                      def minimumSize(self):  # pylint: disable=invalid-name
                          size = QSize()
                  
                          for item in self._item_list:
                              minsize = item.minimumSize()
                              extent = item.geometry().bottomRight()
                              size = size.expandedTo(QSize(minsize.width(), extent.y()))
                  
                          margin = self.contentsMargins().left()
                          size += QSize(2 * margin, 2 * margin)
                          return size
                  
                      def _do_layout(self, rect, test_only=False):
                          m = self.contentsMargins()
                          effective_rect = rect.adjusted(+m.left(), +m.top(), -m.right(), -m.bottom())
                          x = effective_rect.x()
                          y = effective_rect.y()
                          line_height = 0
                  
                          for item in self._item_list:
                              wid = item.widget()
                  
                              space_x = self.spacing()
                              space_y = self.spacing()
                              if wid is not None:
                                  space_x += wid.style().layoutSpacing(
                                      QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Horizontal)
                                  space_y += wid.style().layoutSpacing(
                                      QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Vertical)
                  
                              next_x = x + item.sizeHint().width() + space_x
                              if next_x - space_x > effective_rect.right() and line_height > 0:
                                  x = effective_rect.x()
                                  y = y + line_height + space_y
                                  next_x = x + item.sizeHint().width() + space_x
                                  line_height = 0
                  
                              if not test_only:
                                  item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
                  
                              x = next_x
                              line_height = max(line_height, item.sizeHint().height())
                  
                          new_height = y + line_height - rect.y()
                          self.heightChanged.emit(new_height)
                          return new_height
                  
                  
                  if __name__ == '__main__':
                      import sys
                      from PyQt5.QtWidgets import QApplication, QPushButton, QScrollArea, QVBoxLayout, QWidget, QGroupBox
                  
                      app = QApplication(sys.argv)
                  
                      container = QWidget()
                      container_layout = QVBoxLayout()
                      for i in range(2):
                          g = QGroupBox(f'Group {i}')
                          l = FlowLayout(margin=10)
                          l.heightChanged.connect(container.setMinimumHeight)
                          g.setLayout(l)
                          l.addWidget(QPushButton('Short'))
                          l.addWidget(QPushButton('Longer'))
                          l.addWidget(QPushButton('Different text'))
                          l.addWidget(QPushButton('More text'))
                          l.addWidget(QPushButton('Even longer button text'))
                          container_layout.addWidget(g)
                      container_layout.addStretch()
                      container.setLayout(container_layout)
                  
                      w = QScrollArea()
                      w.setWindowTitle('Flow Layout')
                      w.setWidgetResizable(True)
                      w.setWidget(container)
                      w.show()
                  
                      sys.exit(app.exec_())
                  

                  這篇關于帶有 FlowLayout 小部件的 QScrollArea 未正確調整大小的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                  • <small id='rAYK9'></small><noframes id='rAYK9'>

                    <legend id='rAYK9'><style id='rAYK9'><dir id='rAYK9'><q id='rAYK9'></q></dir></style></legend>
                      <bdo id='rAYK9'></bdo><ul id='rAYK9'></ul>
                      <tfoot id='rAYK9'></tfoot>

                        <tbody id='rAYK9'></tbody>
                      <i id='rAYK9'><tr id='rAYK9'><dt id='rAYK9'><q id='rAYK9'><span id='rAYK9'><b id='rAYK9'><form id='rAYK9'><ins id='rAYK9'></ins><ul id='rAYK9'></ul><sub id='rAYK9'></sub></form><legend id='rAYK9'></legend><bdo id='rAYK9'><pre id='rAYK9'><center id='rAYK9'></center></pre></bdo></b><th id='rAYK9'></th></span></q></dt></tr></i><div class="b5zxt7h" id='rAYK9'><tfoot id='rAYK9'></tfoot><dl id='rAYK9'><fieldset id='rAYK9'></fieldset></dl></div>
                            主站蜘蛛池模板: 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 别墅图纸超市|别墅设计图纸|农村房屋设计图|农村自建房|别墅设计图纸及效果图大全 | 物流之家新闻网-最新物流新闻|物流资讯|物流政策|物流网-匡匡奈斯物流科技 | 创绿家招商加盟网-除甲醛加盟-甲醛治理加盟-室内除甲醛加盟-创绿家招商官网 | 西安微信朋友圈广告投放_微信朋友圈推广_西安度娘网络科技有限公司 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 双工位钻铣攻牙机-转换工作台钻攻中心-钻铣攻牙机一体机-浙江利硕自动化设备有限公司 | 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 大流量卧式砂磨机_强力分散机_双行星双动力混合机_同心双轴搅拌机-莱州市龙跃化工机械有限公司 | 防渗膜厂家|养殖防渗膜|水产养殖防渗膜-泰安佳路通工程材料有限公司 | 电缆隧道在线监测-智慧配电站房-升压站在线监测-江苏久创电气科技有限公司 | 上海公司注册-代理记账-招投标审计-上海昆仑扇财税咨询有限公司 上海冠顶工业设备有限公司-隧道炉,烘箱,UV固化机,涂装设备,高温炉,工业机器人生产厂家 | 齿轮减速机电机一体机_齿轮减速箱加电机一体化-德国BOSERL蜗轮蜗杆减速机电机生产厂家 | 节流截止放空阀-不锈钢阀门-气动|电动截止阀-鸿华阀门有限公司 | 猪I型/II型胶原-五克隆合剂-细胞冻存培养基-北京博蕾德科技发展有限公司 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | 防水试验机_防水测试设备_防水试验装置_淋雨试验箱-广州岳信试验设备有限公司 | 分类168信息网 - 分类信息网 免费发布与查询 | 压力变送器-上海武锐自动化设备有限公司| 硫化罐-胶管硫化罐-山东鑫泰鑫智能装备有限公司 | 厂房出租_厂房出售_产业园区招商_工业地产&nbsp;-&nbsp;中工招商网 | 气动隔膜泵厂家-温州永嘉定远泵阀有限公司 | 锯边机,自动锯边机,双面涂胶机-建业顺达机械有限公司 | 合肥地磅_合肥数控切割机_安徽地磅厂家_合肥世佳电工设备有限公司 | 超声波乳化机-超声波分散机|仪-超声波萃取仪-超声波均质机-精浩机械|首页 | 建大仁科-温湿度变送器|温湿度传感器|温湿度记录仪_厂家_价格-山东仁科 | 卫生型双针压力表-高温防腐差压表-安徽康泰电气有限公司 | 灌木树苗-绿化苗木-常绿乔木-价格/批发/基地 - 四川成都途美园林 | 体视显微镜_荧光生物显微镜_显微镜报价-微仪光电生命科学显微镜有限公司 | 胶原检测试剂盒,弹性蛋白检测试剂盒,类克ELISA试剂盒,阿达木单抗ELISA试剂盒-北京群晓科苑生物技术有限公司 | 真空吸污车_高压清洗车厂家-程力专用汽车股份有限公司官网 | 济南电缆桥架|山东桥架-济南航丰实业有限公司 | 箱式破碎机_移动方箱式破碎机/价格/厂家_【华盛铭重工】 | 航空铝型材,7系铝型材挤压,硬质阳*氧化-余润铝制品 | 南昌旅行社_南昌国际旅行社_南昌国旅在线 | 雨燕360体育免费直播_雨燕360免费NBA直播_NBA篮球高清直播无插件-雨燕360体育直播 | 铝合金风口-玻璃钢轴流风机-玻璃钢屋顶风机-德州东润空调设备有限公司 | 标准品网_标准品信息网_【中检计量】 | 炒货机-炒菜机-炒酱机-炒米机@霍氏机械 | 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 |