【问题标题】:PyQt5 designer size policy not reflecting properly in previewPyQt5 设计器大小策略未在预览中正确反映
【发布时间】:2022-12-10 14:40:37
【问题描述】:

我在 pyqt5 设计器中为我的项目制作侧边栏原型,我注意到很难让侧边栏正确调整大小。理想情况下,侧边栏顶部的按钮应保持相同大小,并且无论您如何调整它的大小都应保持在顶部,而底部的设置按钮应保持在底部。然而,我得到的是按钮似乎有固定的位置,如果我试图把它变小,按钮就会从屏幕上消失,如果我把它变大,侧边栏不会随着屏幕一起放大。这是 .ui 格式的代码示例:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="enabled">
   <bool>true</bool>
  </property>
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1585</width>
    <height>1072</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QWidget" name="Sidebar" native="true">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>61</width>
      <height>1071</height>
     </rect>
    </property>
    <property name="sizePolicy">
     <sizepolicy hsizetype="Minimum" vsizetype="Expanding">
      <horstretch>0</horstretch>
      <verstretch>0</verstretch>
     </sizepolicy>
    </property>
    <property name="minimumSize">
     <size>
      <width>61</width>
      <height>1071</height>
     </size>
    </property>
    <property name="maximumSize">
     <size>
      <width>61</width>
      <height>16777215</height>
     </size>
    </property>
    <property name="contextMenuPolicy">
     <enum>Qt::NoContextMenu</enum>
    </property>
    <property name="autoFillBackground">
     <bool>false</bool>
    </property>
    <property name="styleSheet">
     <string notr="true">QWidget {
background-color: rgb(234, 234, 234);}</string>
    </property>
    <widget class="QPushButton" name="TabBtn1">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>0</y>
       <width>61</width>
       <height>61</height>
      </rect>
     </property>
     <property name="mouseTracking">
      <bool>false</bool>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton {
    background: rgb(234, 234, 234);
}

QPushButton:hover {
    background: rgb(195, 195, 195);
}</string>
     </property>
     <property name="text">
      <string/>
     </property>
     <property name="icon">
      <iconset>
       <normaloff>../Downloads/machine-learning.png</normaloff>../Downloads/machine-learning.png</iconset>
     </property>
     <property name="iconSize">
      <size>
       <width>40</width>
       <height>40</height>
      </size>
     </property>
     <property name="checkable">
      <bool>false</bool>
     </property>
    </widget>
    <widget class="QPushButton" name="TabBtn2">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>60</y>
       <width>61</width>
       <height>61</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton {
    background: rgb(234, 234, 234);
}

QPushButton:hover {
    background: rgb(195, 195, 195);
}</string>
     </property>
     <property name="text">
      <string/>
     </property>
     <property name="icon">
      <iconset>
       <normaloff>../Downloads/contact-list.png</normaloff>../Downloads/contact-list.png</iconset>
     </property>
     <property name="iconSize">
      <size>
       <width>40</width>
       <height>40</height>
      </size>
     </property>
    </widget>
    <widget class="QPushButton" name="TabBtn4">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>180</y>
       <width>61</width>
       <height>61</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton {
    background: rgb(234, 234, 234);
}

QPushButton:hover {
    background: rgb(195, 195, 195);
}</string>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
    <widget class="QPushButton" name="TabBtn5">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>240</y>
       <width>61</width>
       <height>61</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton {
    background: rgb(234, 234, 234);
}

QPushButton:hover {
    background: rgb(195, 195, 195);
}</string>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
    <widget class="QPushButton" name="TabBtn5_2">
     <property name="enabled">
      <bool>true</bool>
     </property>
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>1010</y>
       <width>61</width>
       <height>61</height>
      </rect>
     </property>
     <property name="sizePolicy">
      <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
       <horstretch>0</horstretch>
       <verstretch>1</verstretch>
      </sizepolicy>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton {
    background: rgb(234, 234, 234);
}

QPushButton:hover {
    background: rgb(195, 195, 195);
}</string>
     </property>
     <property name="text">
      <string/>
     </property>
     <property name="icon">
      <iconset>
       <normaloff>../Downloads/settings (1).png</normaloff>../Downloads/settings (1).png</iconset>
     </property>
     <property name="iconSize">
      <size>
       <width>40</width>
       <height>40</height>
      </size>
     </property>
    </widget>
    <widget class="QPushButton" name="TabBtn3">
     <property name="geometry">
      <rect>
       <x>0</x>
       <y>120</y>
       <width>61</width>
       <height>61</height>
      </rect>
     </property>
     <property name="styleSheet">
      <string notr="true">QPushButton {
    background: rgb(234, 234, 234);
}

QPushButton:hover {
    background: rgb(195, 195, 195);
}</string>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
   </widget>
   <widget class="QStackedWidget" name="stackedWidget">
    <property name="geometry">
     <rect>
      <x>60</x>
      <y>0</y>
      <width>1521</width>
      <height>1071</height>
     </rect>
    </property>
    <property name="currentIndex">
     <number>0</number>
    </property>
    <widget class="QWidget" name="page"/>
    <widget class="QWidget" name="page_2"/>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

我试过调整按钮的大小策略以及它所在的小部件,不幸的是我没有看到任何修复它的方法。

【问题讨论】:

  • 您需要使用layout managers(查看如何use them in Designer),并注意大小策略仅对布局有效,如果它们不是布局的一部分,则设置它们是无用的。

标签: python css pyqt5


【解决方案1】:

导入操作系统

用户界面类(QMainWindow):

** 将此行添加到您的代码中

os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    相关资源
    最近更新 更多