PYQT5设计师尺寸策略在预览中无法正确反映
我在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>
我尝试使用按钮的尺寸策略以及它所处的小部件的尺寸策略,不幸的是,我看不到任何修复它的东西。
I was prototyping a sidebar for my project in pyqt5 designer, and I noticed that it was rather hard to get the sidebar to resize properly. Ideally the buttons at the top of the sidebar should remain the same size and remain at the top no matter how you try to resize it, and the settings button at the bottom should remain at the bottom. However, what I got instead is that the buttons seem to have fixed positions and if I try to make it smaller the buttons simply go out of the screen, and if I make it larger the sidebar does not enlarge with the screen. Here's the code sample in .ui format:
<?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>
I've tried playing around with the size policies of the buttons as well as the widget it is in, unfortunately I don't see anything that fixes it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
导入OS
类UI(QMainWindow):
**将此行添加到您的代码
import os
class UI(QMainWindow):
** add this line to your code