Python PyQt 100%宽度窗口
因此,我试图创建一个窗口(QDialog),宽度为100%(屏幕),高度为固定值,例如60px 我试图通过将maximumSize的height属性设置为60px来实现这一点,将宽度保留为任意值。(16777215) 我还将水平sizePolicy设置为最大,将垂直sizePolicy设置为固定,拉伸值分别为1和0 如上图所示,对话框居中显示,但完全没有水平拉伸 代码非常简单,如下所示,它只是一个无框架QDialog:Python PyQt 100%宽度窗口,python,qt,pyqt,Python,Qt,Pyqt,因此,我试图创建一个窗口(QDialog),宽度为100%(屏幕),高度为固定值,例如60px 我试图通过将maximumSize的height属性设置为60px来实现这一点,将宽度保留为任意值。(16777215) 我还将水平sizePolicy设置为最大,将垂直sizePolicy设置为固定,拉伸值分别为1和0 如上图所示,对话框居中显示,但完全没有水平拉伸 代码非常简单,如下所示,它只是一个无框架QDialog: 从PyQt5导入QtWidgets、QtCore、uic 导入系统 导入
从PyQt5导入QtWidgets、QtCore、uic
导入系统
导入操作系统
类FMenu(qtwidts.QDialog):
定义初始化(自):
超级(FMenu,self)。\uuuu init\uuuu()
chdir(os.path.dirname(_文件__))
self.ui=uic.loadUi('./ui/ui_searchbar.ui',self)
self.ui.setWindowFlags(QtCore.Qt.FramelessWindowHint)
self.ui.show()
app=qtwidts.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>
搜索栏
0
0
800
60
1.
0
16777215
60
主窗口
背景#2b2b;
0
0
801
61
10
10
781
41
1.
0
16777215
41
新罗马时代
22
背景:#3b3b;
颜色:#fff;
写在这里。。。
我遗漏了什么?maximumSize仅表示它可以接受的最大大小,并不表示小部件将被拉伸。在这种情况下,最好使用布局:
搜索栏
0
0
870
60
1.
0
16777215
60
主窗口
背景#2b2b;
0
0
0
0
1.
0
16777215
41
新罗马时代
22
背景:#3b3b;
颜色:#fff;
写在这里。。。
如果要占据整个屏幕,则必须计算屏幕的大小:
window.setFixedWidth(app.primaryScreen().availableSize().width())
编辑了问题,添加了python代码和ui文件。感谢您的回复!尝试了这个,出于某种原因得到了相同的结果。这对你有用吗?@dámMaul嗯,也许我不明白你的问题。你们叫窗户什么?你是说屏幕?我知道您希望,如果用户更改窗口(而不是屏幕)的宽度,那么QLineEdit也必须这样做,这就是我的代码所做的:那么我认为我只是在解释问题上很差劲,对不起,我会再试一次。您的代码很有帮助,因为我确实希望lineedit是窗口的全宽,但我也希望窗口填充屏幕的全宽。我主要关心的是窗口水平地填满了屏幕。例如,像顶栏,或者像windows上的“开始”菜单一样。@dámMaul然后添加window.setFixedWidth(app.primaryScreen().availableSize().width())
,然后重写您的帖子。