【问题标题】:PyQt 100% width windowPyQt 100% 宽度窗口
【发布时间】: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文件。

标签: python qt pyqt


【解决方案1】:

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())

【讨论】:

  • 感谢您的回复!试过这个,由于某种原因得到了相同的结果。这对你有用吗?
  • @ÁdámMaul 嗯,也许我不明白你的问题。什么叫窗户?你是说屏幕吗?我知道您希望如果用户更改窗口(而不是屏幕)的宽度,那么 QLineEdit 也必须这样做,这就是我的代码所做的:i.imgur.com/Np9DfGi.png
  • 那我觉得我解释的东西很烂,对不起,我再试一次。您的代码有帮助,因为我确实希望 lineedit 是窗口的全宽,但我也希望窗口填满屏幕的全宽。我主要关心的是水平填充屏幕的窗口。就像一个顶部栏,或者像 Windows 上的开始菜单一样。
  • @ÁdámMaul 然后添加 window.setFixedWidth(app.primaryScreen().availableSize().width()) 并重写您的帖子。
猜你喜欢
  • 2013-01-02
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
  • 2015-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多