简体   繁体   English

PyQt5 listWidget 拒绝在 QtDesigner 中变小

[英]PyQt5 listWidget refuse to get smaller in QtDesigner

I have a layout in designer that looks like this:我在设计器中有一个如下所示的布局:

在此处输入图像描述

except that I want the listWidget to be as small as possible, and only expand vertically based on the minimum height needed to hold all it's items.除了我希望 listWidget 尽可能小,并且仅根据容纳所有项目所需的最小高度垂直扩展。 However for some reason it refuses to cooperate, and just remains at it's default size.但是由于某种原因,它拒绝合作,只是保持默认大小。 I tried to do multiple things to change it:我试图做很多事情来改变它:

  1. setting the stretch factor of textLabel to listWidget to be 1,0将 textLabel 的拉伸因子设置为 listWidget 为 1,0
  2. setting the sizePolicy of the listWidget as well as the widget it is in to be minimum将 listWidget 的 sizePolicy 以及它所在的小部件设置为最小
  3. setting the resizeMode of the listWidget to be adjust设置要调整的 listWidget 的 resizeMode
  4. setting the sizePolicy of the button (I want it to fill up all the remaining space) to expanding将按钮的 sizePolicy(我希望它填满所有剩余空间)设置为展开
  5. setting the stretch factor of the widget to the button to 0,1将小部件到按钮的拉伸因子设置为 0,1

but none of these work.但这些都不起作用。 In addition, the window does not seem to be able to become smaller than this despite there being no minimum size set for anything, leading me to believe that there is an invisible minimum size for the listWidget that is not related to the minimum size shown in the properties.此外,尽管没有为任何东西设置最小尺寸,但 window 似乎无法变得比这更小,这让我相信 listWidget 有一个不可见的最小尺寸,它与显示的最小尺寸无关属性。 Anyone knows how to fix this?任何人都知道如何解决这个问题?

Edit: after taking a suggestion of using preferred size policy and an expanding button, it still does not want to work.编辑:在建议使用首选尺寸策略和扩展按钮后,它仍然不想工作。 Here's the ui file for reference:这是供参考的ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>318</width>
    <height>277</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0">
   <property name="spacing">
    <number>0</number>
   </property>
   <property name="leftMargin">
    <number>0</number>
   </property>
   <property name="topMargin">
    <number>0</number>
   </property>
   <property name="rightMargin">
    <number>0</number>
   </property>
   <property name="bottomMargin">
    <number>0</number>
   </property>
   <item>
    <widget class="QWidget" name="widget" native="true">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Minimum" vsizetype="Maximum">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
     <property name="baseSize">
      <size>
       <width>0</width>
       <height>0</height>
      </size>
     </property>
     <layout class="QVBoxLayout" name="verticalLayout" stretch="0,0">
      <property name="spacing">
       <number>0</number>
      </property>
      <property name="sizeConstraint">
       <enum>QLayout::SetDefaultConstraint</enum>
      </property>
      <property name="leftMargin">
       <number>0</number>
      </property>
      <property name="topMargin">
       <number>0</number>
      </property>
      <property name="rightMargin">
       <number>0</number>
      </property>
      <property name="bottomMargin">
       <number>0</number>
      </property>
      <item>
       <widget class="QLabel" name="label">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Maximum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="text">
         <string>TextLabel</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QListWidget" name="listWidget">
        <property name="sizePolicy">
         <sizepolicy hsizetype="Preferred" vsizetype="Maximum">
          <horstretch>0</horstretch>
          <verstretch>0</verstretch>
         </sizepolicy>
        </property>
        <property name="sizeAdjustPolicy">
         <enum>QAbstractScrollArea::AdjustToContents</enum>
        </property>
        <property name="resizeMode">
         <enum>QListView::Fixed</enum>
        </property>
       </widget>
      </item>
     </layout>
    </widget>
   </item>
   <item>
    <widget class="QPushButton" name="pushButton">
     <property name="sizePolicy">
      <sizepolicy hsizetype="Expanding" vsizetype="Minimum">
       <horstretch>0</horstretch>
       <verstretch>0</verstretch>
      </sizepolicy>
     </property>
     <property name="text">
      <string>PushButton</string>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

Set the stretch factors to default and then将拉伸因子设置为默认值,然后

  1. set QListWidget set vertical size policy to Preferred设置QListWidget将垂直大小策略设置为Preferred
  2. set the QPushButton vertical size policy to ExpandingQPushButton垂直尺寸策略设置为Expanding
  3. set the QLabel vertical size policy to MaximumQLabel垂直大小策略设置为Maximum
  4. Set the sizeAdjuctPolicy for QListWidget to AdjustToContentsQListWidget的 sizeAdjuctPolicy 设置为AdjustToContents

The size policies in qt are not what they seem upon first glance. qt 中的大小策略与乍一看不同。 Minimum actually means grow as much as you can. Minimum实际上意味着尽可能多地增长。 Maximum actually means shrink as much as you can. Maximum实际上意味着尽可能地缩小。 you can learn more about them in the Qt docs您可以在Qt 文档中了解有关它们的更多信息

The last part you can do is overwrite the QListWidget.sizeHint method to dynamically adjust to the number of items in the list.您可以做的最后一部分是覆盖 QListWidget.sizeHint 方法以动态调整列表中的项目数。

Here is a runnable example:这是一个可运行的示例:

from PySide6.QtCore import *
from PySide6.QtWidgets import *

class ListWidget(QListWidget):

    def sizeHint(self):
        size = super().sizeHint()
        frame = self.frameWidth() * 2
        items_height = self.count() * self.sizeHintForRow(0)
        height = min(super().maximumHeight(), items_height)
        size.setHeight(height + frame)
        return size

    def minimumSizeHint(self):
        return self.sizeHint()

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent=parent)
        self.resize(318, 277)
        self.centralwidget = QWidget(self)
        self.verticalLayout = QVBoxLayout(self.centralwidget)
        sizePolicy = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Preferred)
        sizePolicy1 = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Expanding)
        sizePolicy2 = QSizePolicy(QSizePolicy.Minimum, QSizePolicy.Maximum)
        self.setCentralWidget(self.centralwidget)
        self.label = QLabel('Label', self.centralwidget)
        self.listWidget = ListWidget(self.centralwidget)
        self.pushButton = QPushButton('Push Button', self.centralwidget)
        self.label.setSizePolicy(sizePolicy2)
        self.listWidget.setSizePolicy(sizePolicy)
        self.listWidget.setSizeAdjustPolicy(QAbstractScrollArea.AdjustToContents)
        self.pushButton.setSizePolicy(sizePolicy1)
        self.verticalLayout.addWidget(self.label)
        self.verticalLayout.addWidget(self.listWidget)
        self.verticalLayout.addWidget(self.pushButton)
        self.pushButton.clicked.connect(self.additem)

    def additem(self):
        i = self.listWidget.count()
        self.listWidget.addItem(QListWidgetItem("Item" + str(i), type=0))


if __name__ == "__main__":
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM