0
votes

So I am trying to create a window (QDialog) with a width of a 100% (of the screen) and a height of a fixed value, eg. 60px.

I tried to achieve this by setting the maximumSize's height property to 60px, leaving the width to whatever it was. (16777215) I also set the horizontal sizePolicy to Maximum, and the vertical to Fixed, with stretch values of 1 and 0 respectively.

Here are the properties I tried to modify Here is the result

As you can see in the picture above, the dialog appears centered, but not stretched at all horizontally.

The code is pretty simple as seen below, its just a frameless 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_()

The ui file I created with QtDesigner is as follows:

<?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>

What am I missing?

1
Edited the question, added the python code and the ui file.Ádám Maul

1 Answers

1
votes

maximumSize only indicates the maximum size it can take, it does not indicate that the widget will be stretched. In that case it is better to use a layout:

<?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>

If you want to occupy the entire screen, then you have to calculate the size of the screen:

window.setFixedWidth(app.primaryScreen().availableSize().width())