PyQt 100% 宽度 window
PyQt 100% width window
所以我正在尝试创建一个 window (QDialog),宽度为 100%(屏幕的),高度为固定值,例如。 60px.
我试图通过将 maximumSize 的高度 属性 设置为 60px 来实现这一点,而宽度保持不变。 (16777215)
我还将水平方向的 sizePolicy 设置为 Maximum,垂直方向设置为 Fixed,拉伸值分别为 1 和 0。
如您在上图中所见,对话框居中显示,但根本没有水平拉伸。
代码很简单,如下所示,就是一个无框的QDialog:
from PyQt5 import QtWidgets, QtCore, uic
import sys
import os
class FMenu(QtWidgets.QDialog):
def __init__(self):
super(FMenu, self).__init__()
os.chdir(os.path.dirname(__file__))
self.ui = uic.loadUi('./ui/ui_searchbar.ui', self)
self.ui.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.ui.show()
app = QtWidgets.QApplication(sys.argv)
window = FMenu()
app.exec_()
我用QtDesigner创建的ui文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SearchBar</class>
<widget class="QDialog" name="SearchBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>60</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>60</height>
</size>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true">background: #2b2b2b;</string>
</property>
<widget class="QWidget" name="centralwidget" native="true">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>801</width>
<height>61</height>
</rect>
</property>
<widget class="QLineEdit" name="lineEdit_searchBar">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>781</width>
<height>41</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>41</height>
</size>
</property>
<property name="font">
<font>
<family>Times New Roman</family>
<pointsize>22</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background: #3b3b3b;
color: #fff;</string>
</property>
<property name="placeholderText">
<string>Write here...</string>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
我错过了什么?
maximumSize 仅表示它可以占用的最大尺寸,并不表示widget 会被拉伸。在那种情况下,最好使用布局:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SearchBar</class>
<widget class="QDialog" name="SearchBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>870</width>
<height>60</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>60</height>
</size>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true">background: #2b2b2b;</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QWidget" name="centralwidget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<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="QLineEdit" name="lineEdit_searchBar">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>41</height>
</size>
</property>
<property name="font">
<font>
<family>Times New Roman</family>
<pointsize>22</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background: #3b3b3b;
color: #fff;</string>
</property>
<property name="placeholderText">
<string>Write here...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
如果要占满整个屏幕,那么就得计算屏幕的大小:
window.setFixedWidth(app.primaryScreen().availableSize().width())
所以我正在尝试创建一个 window (QDialog),宽度为 100%(屏幕的),高度为固定值,例如。 60px.
我试图通过将 maximumSize 的高度 属性 设置为 60px 来实现这一点,而宽度保持不变。 (16777215) 我还将水平方向的 sizePolicy 设置为 Maximum,垂直方向设置为 Fixed,拉伸值分别为 1 和 0。
如您在上图中所见,对话框居中显示,但根本没有水平拉伸。
代码很简单,如下所示,就是一个无框的QDialog:
from PyQt5 import QtWidgets, QtCore, uic
import sys
import os
class FMenu(QtWidgets.QDialog):
def __init__(self):
super(FMenu, self).__init__()
os.chdir(os.path.dirname(__file__))
self.ui = uic.loadUi('./ui/ui_searchbar.ui', self)
self.ui.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.ui.show()
app = QtWidgets.QApplication(sys.argv)
window = FMenu()
app.exec_()
我用QtDesigner创建的ui文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SearchBar</class>
<widget class="QDialog" name="SearchBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>60</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>60</height>
</size>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true">background: #2b2b2b;</string>
</property>
<widget class="QWidget" name="centralwidget" native="true">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>801</width>
<height>61</height>
</rect>
</property>
<widget class="QLineEdit" name="lineEdit_searchBar">
<property name="geometry">
<rect>
<x>10</x>
<y>10</y>
<width>781</width>
<height>41</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>41</height>
</size>
</property>
<property name="font">
<font>
<family>Times New Roman</family>
<pointsize>22</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background: #3b3b3b;
color: #fff;</string>
</property>
<property name="placeholderText">
<string>Write here...</string>
</property>
</widget>
</widget>
</widget>
<resources/>
<connections/>
</ui>
我错过了什么?
maximumSize 仅表示它可以占用的最大尺寸,并不表示widget 会被拉伸。在那种情况下,最好使用布局:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SearchBar</class>
<widget class="QDialog" name="SearchBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>870</width>
<height>60</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>60</height>
</size>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<property name="styleSheet">
<string notr="true">background: #2b2b2b;</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QWidget" name="centralwidget" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<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="QLineEdit" name="lineEdit_searchBar">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>41</height>
</size>
</property>
<property name="font">
<font>
<family>Times New Roman</family>
<pointsize>22</pointsize>
</font>
</property>
<property name="styleSheet">
<string notr="true">background: #3b3b3b;
color: #fff;</string>
</property>
<property name="placeholderText">
<string>Write here...</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
如果要占满整个屏幕,那么就得计算屏幕的大小:
window.setFixedWidth(app.primaryScreen().availableSize().width())