QFormLayout() set window size - which is the best solution?
Unsolved
Qt for Python
-
Hello,
here my code:
import sys from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, \ QPushButton, QFormLayout, QLineEdit class Window(QWidget): define __init__(self): super().__init__() self.UI() define UI(self): self.setWindowTitle("QFormLayout()") self.lname = QLabel("&Name:") self.nameLineEdit = QLineEdit() self.lmail = QLabel("&Email:") self.emailLineEdit = QLineEdit() self.lname.setBuddy(self.nameLineEdit) self.lmail.setBuddy(self.emailLineEdit) self.btn_programm_beenden = QPushButton("En&de", self) self.btn_terminate_program.setDefault(True) self.btn_programm_beenden.clicked.connect( self.programm_beeden) formLayout = QFormLayout() #Several control elements can be placed in a row #to be included formLayout.addRow(self.lname,self.nameLineEdit) formLayout.addRow(self.lmail,self.emailLineEdit) formLayout.addRow(self.btn_terminate_program) self.setLayout(formLayout) def program_finished(self): QApplication.instance().quit() app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec())
I know that you can set the window size with setGeometry. Disadvantage: The use of coordinates could cause problems in terms of platform independence.
Then today I found out that you can set the window size with setFixedHeight and setFixedWidth. The charm of this solution is that the window is centered. Disadvantage: The user cannot change the fixed size with the computer mouse.
I'm looking for a solution
which is platform independent
The window is centered
The user can adjust the window size.What can you recommend me?
-
Hi,
Do you mean you would like for your application window to be shown centered on whatever screen the user starts your application on ?
If so you should take a look at the QScreen class.