Home>

I want, when changing the size of the main window all QPushButton. . Qlineedit. . QLabel Also changed their position, and if possible, then the size to save the proportions of the interface.

Currently in pyqt5 I can only specify the position of the elements only in pixels. I hope that I have clearly explained.

Please show your code. And the interface would also be nice.

V-Mor2021-04-08 07:40:37
  • Answer # 1

    provide the minimum example -in your own interests, and our time will save.

    method resizeEvent caused every time the window size changes, Therefore, this is the right place to move and change.

    What you ask may look something like this:

    Import Sys
    From Pyqt5 Import Qtcore, Qtgui, Qtwidgets
    from pyqt5.QTCore Import *
    from pyqt5.Qtgui import *
    from pyqt5.Qtwidgets import *
    Class Window (qtwidgets.qmainwindow):
        Def __init __ (Self, Parent= None):
            Super (Window, Self) .__ INIT __ (PARENT)
            Self.w= 400.
            Self.h= 400.
            self.Resize (Self.w, Self.h)
            Self.SetminimumSize (Self.W /2, Self.h /2)
            Self.widthfactor= 1.
            Self.Heightfactor= 1.
            self.Labelpix= qtwidgets.QLabel (Self, Alignment= Qtcore.qt.alignCenter)
            self.Labelpix.setpixmap (qtgui.qpixmap ("im.png"). Scaled (Self.w * Self.widthfactor,
                                                                   self.h * slf.heightfactor))
            self.Labelpix.move (0, 0)
            self.Labelpix.adjustSize ()
            self.label= qtwidgets.qlabel ("Label", self, alignment= qtcore.qt.aligncenter)
            self.Label.SetstyleSheet ("font: {} pt arial; background: lightblue" .format (20 * Self.HeightFactor))
            self.Label.Resize (self.w * self.widthfactor, Self.h * 0.25 * Self.HeightFactor)
            self.Label.adjustsize ()
            self.lineedit= qtwidgets.QLineEdit ("Hello World!", Self)
            self.lineedit.resize (self.w * 0.75 * self.widthfactor, Self.h * 0.1 * Self.HeightFactor)
            Self.Button= qtwidgets.qpushbutton ('Button', Self)
            self.button.clicalked.connect (Self.ClickButton)
            self.button.resize (self.w * 0.25 * self.widthfactor, Self.h * 0.2 * Self.HeightFactor)
        Def ClickButton (Self):
            self.Label.setText (self.lineedit.text ())
            self.Label.adjustsize ()
            self.Label.move (Self.Rect (). Center () -self.Label.Rect (). Center () -Qpoint (0, Self.h * 0.25 * Self.HeightFactor))
        DEF SETLABELPIX (SELF):
            self.Labelpix.setpixmap (qtgui.qpixmap ("im.png"). Scaled (Self.w * Self.widthfactor,
                                                                   self.h * slf.heightfactor))
            self.Labelpix.move (0, 0)
            self.Labelpix.adjustSize ()
            self.Label.SetstyleSheet ("font: {} pt arial; background: lightblue" .format (20 * Self.HeightFactor))
            self.Label.adjustsize ()
        DEF ResizeEvent (Self, Event):
            self.widthfactor= Self.Rect (). width () /400
            self.heightfactor= Self.Rect (). Height () /400
            self.Labelpix.move (Self.Rect (). Center () -self.Labelpix.Rect (). Center ()) # -Qpoint (0, 150))
            self.Label.move (Self.Rect (). Center () -self.Label.Rect (). Center () -Qpoint (0, Self.h /4 * Self.HeightFactor))
            self.lineedit.resize (self.w * 0.75 * self.widthfactor, Self.h * 0.1 * Self.HeightFactor)
            self.lineedit.move (Self.Rect (). Center () -self.lineedit.Rect (). Center ())
            self.button.resize (self.w * 0.25 * self.widthfactor, Self.h * 0.2 * Self.HeightFactor)
            self.button.move (self.Rect (). Center () -self.button.Rect (). Center () -Qpoint (0, -self.h * 0.3 * Self.HeightFactor))
            self.setlabelpix ()
            Super (Window, Self) .ResizeEvent (EVENT)
    if __name__== '__main__':
        Application= Qtwidgets.Qapplication (SYS.Argv)
        Window= Window ()
        Window.setWindowTitle ('When changing the size of the main window, change all the position and sizes.')
        Window.Show ()
        sys.exit (application.exec_ ())