Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can i give a widget initial size in pyqt5?

i want give a widget an initial size >>>>>> not fixed , not minimum , and not maximum size

because i want the user be able to control the size of the widget, when i am using splitter.

how ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

look at this image :

enter image description here

i want the right side (the red) width to be 150 when i open the program > but when i try to change the size of it i want it to respond and to be changeable even if i want it to be bigger or smaller.

when i set a fixed size i will not be able to change its size.

when i set a minimum size it will expand and make the left side (the green) small.

when i set a maximam size i will be able to make it smaller but not bigger.

this is my code:

from PyQt5 import QtWidgets
import sys


app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.resize(800,500)
window_layout = QtWidgets.QGridLayout()



first_widget = QtWidgets.QScrollArea()
first_widget.setStyleSheet("background-color:rgb(0,150,0)")



second_widget = QtWidgets.QGroupBox()
second_widget.setStyleSheet("background-color:rgb(150,0,0)")



splitter = QtWidgets.QSplitter()
splitter.addWidget(first_widget)
splitter.addWidget(second_widget)


window_layout.addWidget(splitter)
window.setLayout(window_layout)
window.show()
app.exec()

thanks.

>Solution :

Since you are using a QSplitter, you can simply use the setSizes method in order to adjust the sizes of each window controlled by the splitter.

For example:

splitter.setSizes([splitter.width() - 150,150])

From the Qt docs

QSplitter.setSizes

Sets the child widgets’ respective sizes to the values given in the list.

If the splitter is horizontal, the values set the width of each widget in pixels, from left to right. If the splitter is vertical, the height of each widget is set, from top to bottom.

Extra values in the list are ignored. If list contains too few values, the result is undefined, but the program will still be well-behaved.

The overall size of the splitter widget is not affected. Instead, any additional/missing space is distributed amongst the widgets according to the relative weight of the sizes.

If you specify a size of 0, the widget will be invisible. The size policies of the widgets are preserved. That is, a value smaller than the minimal size hint of the respective widget will be replaced by the value of the hint.

An alternative method to set the initial size of a widget which is a little more versatile is to subclass the widget and define a sizeHint method that returns the desired QSize

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading