QSizePolicy doesnt work
Unsolved
Qt for Python
-
QSizePolicy doesn't work. button,text and label doesnt scale with the main window. I tried different way and followed online Tutorials. but it still doesn't work. i am not sure what i am doing wrong. Help me Wizards
from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtWidgets import QSizePolicy class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(472, 464) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Expanding) MainWindow.setSizePolicy(sizePolicy) MainWindow.setBaseSize(QtCore.QSize(1, 1)) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 451, 411)) self.verticalLayoutWidget.setObjectName("verticalLayoutWidget") self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget) self.label = QtWidgets.QLabel(self.verticalLayoutWidget) font = QtGui.QFont() font.setPointSize(24) self.label.setFont(font) self.label.setScaledContents(True) self.label.setObjectName("label") self.verticalLayout.addWidget(self.label) self.textEdit = QtWidgets.QTextEdit(self.verticalLayoutWidget) font = QtGui.QFont() font.setPointSize(16) self.textEdit.setFont(font) self.textEdit.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContentsOnFirstShow) self.textEdit.setObjectName("textEdit") self.verticalLayout.addWidget(self.textEdit) self.pushButton = QtWidgets.QPushButton(self.verticalLayoutWidget) sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Expanding) self.pushButton.setSizePolicy(sizePolicy) font = QtGui.QFont() font.setPointSize(24) self.pushButton.setFont(font) self.pushButton.setObjectName("pushButton") self.verticalLayout.addWidget(self.pushButton) self.verticalLayout.setStretch(0, 1) self.verticalLayout.setStretch(1, 7) self.verticalLayout.setStretch(2, 2) MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 472, 21)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "Whats ur name ?")) self.pushButton.setText(_translate("MainWindow", "Submit")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
-
@RamanSMann
I have not tried your code. "doesnt scale" in what way, what do you expect? Layouts don't do scaling. Andself.label.setScaledContents(True)
won't scale anything on your label either. -
it worked, i realized my QVlayout was just floating in the window, so i attached it to the window with grid layout.
https://stackoverflow.com/questions/3492739/auto-expanding-layout-with-qt-designer