I have a pyqt5 based GUI application. I just want all the widgets like Textbox or Buttons to have a fixed size. And i successfully did that! But
I want that if someone resizes the window, the widgets must have their same fixed size BUT it should increase/decrease the spaces between them, rather than changing the size of widgets. (I don't want my widgets to be resized, that's why i opt this method to increase/decrease their spaces between each other).
For example: (Original application state)
(Trying to resize the window to bigger size vertically, but you can see that no buttons widget's size changed. Just more space was added...)
(Trying to resize horizontally)
I found that QSizePolicy
does the same thing that i want, but didn't get any good examples anywhere.
This is my code:
from PySide2 import QtWidgets
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtGui import QPainter, QBrush, QPen, QColor, QPolygon
from PySide2.QtCore import Qt, QRect, QPoint, QRectF
import sys
class MyWindow(QMainWindow):
def __init__(self):
super(MyWindow,self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(200, 200, 300, 300)
self.setWindowTitle("test app")
self.label = QtWidgets.QLineEdit(self)
self.label.setText("my first label!")
self.label.move(110,10)
self.label.resize(180,25)
self.b1 = QtWidgets.QPushButton(self)
self.b1.setText("Fixed Button size!")
self.b1.move(180,250)
self.b1.resize(110, 25)
self.b1.setStyleSheet("""
background-color: #4CAF50;
border: none;
color: white;
text-align: center;
font-size: 12px;
font-family: Arial
""")
def paintEvent(self, e):
qp = QPainter()
qp.begin(self)
self.drawRectangles(qp)
qp.end()
def drawRectangles(self, qp):
col = QColor(0, 0, 0)
col.setNamedColor('#d4d4d4')
qp.setPen(col)
qp.setBrush(QColor(200, 50, 8))
qp.drawRect(0, 0, 100, 299)
qp.setBrush(QColor(250, 255, 250))
qp.drawRect(110, 50, 180, 180)
def window():
app = QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
window()
It hides the widgets when resized. I want to make the buttons and text widgets to add spaces between them, when resized. Now it's hiding those widgets because i didn't used hstack
or vstack
. I know.
Just don't know how to use QSizePolicy
for the spacing purpose.
Thanks in advance...
For what you want to obtain there are multiple solutions, my solution uses the properties of the layouts:
import sys
from PySide2 import QtCore, QtGui, QtWidgets
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.initUI()
def initUI(self):
self.setGeometry(200, 200, 300, 300)
self.setWindowTitle("test app")
label = QtWidgets.QLabel(self)
label.setFixedWidth(100)
label.setStyleSheet("background-color: rgb(200, 50, 8)")
lineedit = QtWidgets.QLineEdit()
lineedit.setFixedWidth(180)
button = QtWidgets.QPushButton("Fixed Button size!")
button.setFixedSize(110, 25)
button.setStyleSheet(
"""
background-color: #4CAF50;
border: none;
color: white;
text-align: center;
font-size: 12px;
font-family: Arial
"""
)
widget = QtWidgets.QLabel()
widget.setFixedSize(180, 180)
widget.setStyleSheet("background-color: rgb(250, 255, 250)")
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
right_container = QtWidgets.QWidget()
botton_widget = QtWidgets.QWidget()
botton_widget.setContentsMargins(0, 0, 0, 0)
botton_widget.setFixedWidth(180)
hlay2 = QtWidgets.QHBoxLayout(botton_widget)
hlay2.setContentsMargins(0, 0, 0, 0)
hlay2.addStretch()
hlay2.addWidget(button)
glay = QtWidgets.QGridLayout(right_container)
glay.addWidget(QtWidgets.QWidget(), 0, 0, 5, 1)
glay.addWidget(lineedit, 0, 1)
glay.addWidget(QtWidgets.QWidget(), 1, 1)
glay.addWidget(widget, 2, 1, alignment=QtCore.Qt.AlignCenter)
glay.addWidget(QtWidgets.QWidget(), 3, 1)
glay.addWidget(QtWidgets.QWidget(), 0, 2, 5, 1)
glay.addWidget(botton_widget, 4, 1)
hlay = QtWidgets.QHBoxLayout(central_widget)
hlay.setContentsMargins(0, 0, 0, 0)
hlay.addWidget(label)
hlay.addWidget(right_container)
def main():
app = QtWidgets.QApplication(sys.argv)
win = MyWindow()
win.show()
sys.exit(app.exec_())
if __name__ == "__main__":
main()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.