Search code examples
pythonpython-3.xuser-interfacepyqtpyqt5

Automatic readjust size of widgets when changing size of MainWindow


I designed a PyQt5 GUI (MainWindow) with QtDesigner. Therefore i "dragged and dropped" the Widgets into the MainWindow and put them where i wanted them.

Everything worked perfectly fine so far.

However, when i wanted to open the application in FullScreen, those Widgets are not getting Resized.

Some research-time later, it seems like i have done an oopsie, as i have not used any specific layout... Am i right?

But i still have some hope that somebody have had the same issue before and maybe fixed it. I also tried several things myself, like setting the SizePolicy of the Widgets to expanding. But nothing worked.

I also provided a minimalistic Code, which has the same Error.

Python Script:

from PyQt5 import QtWidgets, uic
import sys
class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__() # Call the inherited classes __init__ method
        uic.loadUi('Test.ui', self) # Load the .ui file
        self.show() # Show the GUI
        self.button1.clicked.connect(self.button1_clicked)

    def button1_clicked(self):
        self.label1.setText("Who is there?")
        
app = QtWidgets.QApplication(sys.argv)
window = Ui()
app.exec_()

Test.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>931</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QPushButton" name="button1">
    <property name="geometry">
     <rect>
      <x>300</x>
      <y>160</y>
      <width>261</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string>Knock Knock</string>
    </property>
   </widget>
   <widget class="QLabel" name="label1">
    <property name="geometry">
     <rect>
      <x>330</x>
      <y>250</y>
      <width>221</width>
      <height>61</height>
     </rect>
    </property>
    <property name="text">
     <string>-----------------------------------</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>931</width>
     <height>22</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

Can someone help me out or is there no other way than using a specific Layout?

Greetings :)


Solution

  • I'm not sure what YOU mean by "specific layout", but that phrase describes exactly what you have done here -- you have placed and sized each component with specific X and Y coordinates directly. There is no code here to do any resizing. The correct way to use Qt is to use a layout. Then the layout manager will manage all the resizing for you. It take a little bit to grasp the philosophy of layouts and sizers, but the result is well worth the effort. You'll never have to worry about X and Y again.

    Here is the layout guide:

    https://doc.qt.io/qt-6/layout.html