Kanmani
Kanmani

Reputation: 479

widget position and window size

I am new to GUI development and I am trying to learn how to use pyqt5 in python. Below is the example code I am working on. I want a window with some checkboxes,combobox and radiobuttons on the right side of the window. The details of the QtWidget objects are in the code.

from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget, QPushButton, QComboBox, QRadioButton, QVBoxLayout, QCheckBox
from PyQt5.QtGui import QIcon

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.left = 0
        self.top = 500
        self.title = 'Chip2 Torque Data'
        self.width = 500
        self.height =500
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        xselect=QRadioButton("X",self)
        xselect.setChecked(True)

        xselect.move(340,400)
        zselect=QRadioButton("Z",self)
        zselect.move(380,400)
        sselect=QRadioButton("SP1",self)
        sselect.move(420,400)
        pass_list=QComboBox(self)
        pass_list.addItems(sheets_idealcut)
        pass_list.move(340,300)
        rawdata_check=QCheckBox("Raw Data",self)
        rawdata_check.setChecked(True)
        rawdata_check.move(340,200)
        mvgavg_check=QCheckBox("Moving average",self)
        mvgavg_check.setChecked(True)
        mvgavg_check.move(380,200)
        mvgstd_check=QCheckBox("Moving stdev",self)
        mvgstd_check.setChecked(True)
        mvgstd_check.move(420,200)
        self.show()
if __name__ == '__main__':
  sheets_idealcut=['pass2','pass3','pass4','pass5']

    app = QApplication.instance()
    if app is None:
         app = QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))
    ex = App()
    #ex.show()
    app.exec_()

Below is the output of the code. (Please ignore the overlapping checkbox names. I intend to correct it later.)

enter image description here

My problem is when I maximise this window, the QtWidget objects(QComboBox,QRadioButton,QCheckBox) don't readjust their position with the new window size.
enter image description here

So for the widgets to reposition themselves automatically, what method should be used ?

I googled it but I couldn't find anything useful.

Please help.

Upvotes: 0

Views: 5459

Answers (2)

Magnus Wang
Magnus Wang

Reputation: 180

You should be using layouts for dynamic sizing. There are alot of different layouts. The layouts used in this example is the most basic types. I also changed the parent of "App" to a QDialog. They are easier to handle.

Here is an example of your app, using layouts:

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QSizePolicy, QWidget, QPushButton, QComboBox, QRadioButton, \
QVBoxLayout, QCheckBox, QHBoxLayout, QGroupBox, QDialog
from PyQt5.QtGui import QIcon

class App(QDialog):

    def __init__(self):
        super().__init__()
        self.left = 0
        self.top = 500
        self.title = 'Chip2 Torque Data'
        self.width = 500
        self.height = 500
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.main_layout = QVBoxLayout()

        xselect=QRadioButton("X",self)
        xselect.setChecked(True)
        # xselect.move(340,400)
        self.main_layout.addWidget(xselect)

        zselect=QRadioButton("Z",self)
        # zselect.move(380,400)
        self.main_layout.addWidget(zselect)

        sselect=QRadioButton("SP1",self)
        # sselect.move(420,400)
        self.main_layout.addWidget(sselect)



        pass_list=QComboBox(self)
        pass_list.addItems(sheets_idealcut)
        self.main_layout.addWidget(pass_list)
        #pass_list.move(340,300)
        rawdata_check=QCheckBox("Raw Data",self)
        rawdata_check.setChecked(True)
        self.main_layout.addWidget(rawdata_check)
        #rawdata_check.move(340,200)
        mvgavg_check=QCheckBox("Moving average",self)
        mvgavg_check.setChecked(True)
        #mvgavg_check.move(380,200)
        mvgstd_check=QCheckBox("Moving stdev",self)
        mvgstd_check.setChecked(True)
        #mvgstd_check.move(420,200)

        self.check_group = QHBoxLayout()
        self.check_group.addWidget(mvgavg_check)
        self.check_group.addWidget(mvgstd_check)
        self.check_group.stretch(1)

        self.main_layout.addLayout(self.check_group)
        self.setLayout(self.main_layout)
        self.show()

if __name__ == '__main__':
    sheets_idealcut=['pass2','pass3','pass4','pass5']

    app = QApplication.instance()
    if app is None:
         app = QApplication(sys.argv)
    else:
        print('QApplication instance already exists: %s' % str(app))
    ex = App()
    #ex.show()
    app.exec_()

Upvotes: 1

Simon
Simon

Reputation: 113

You are using move(x,y) which sets the Widgets to a specific fixed position. So you could write a function which is called on maximizing the window and then reposition your widgets accordingly.

Another automated approach is to use box layouts. QtGui.QHBoxLayout and QtGui.QVBoxLayout are basic layout classes which lines up widgets horizontally or vertically. From your pictures I do not know what exact positions you want your widgets to end up, so I try to explain it like I think you want it.

Each row of elements can be assigned into a HBox like this:

hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(rawdata_check)
hbox.addWidget(mvgavg_check)
hbox.addWidget(mvgstd_check)

If you then resize the window the widgets should automaticly be repositioned to the right side of the window. The QtDesigner can help you arranging all the elements.

Upvotes: 2

Related Questions