【发布时间】:2021-03-30 01:17:31
【问题描述】:
所以我正在尝试创建一个宽度为 100%(屏幕的)和一个固定值高度的窗口(QDialog),例如。 60 像素。
我试图通过将 maximumSize 的 height 属性设置为 60px 来实现这一点,而将宽度保留为任意值。 (16777215) 我还将水平 sizePolicy 设置为最大,垂直设置为 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>
我错过了什么?
【问题讨论】:
-
编辑问题,添加python代码和ui文件。