Setting a centralwidget to a QMainWindow doesn't center the widget!
-
So I am in trouble, I am trying to create a simple game in PyQt5 but when setting a central widget for my main class then the widget is centered to the left.
class Game(QMainWindow): def __init__(self, parent=None): super(Game, self).__init__(parent) self.handler = QLabel("hello world") self.setCentralWidget(self.handler) if __name__ == '__main__': app = QApplication(sys.argv) main = Game() main.show() sys.exit(app.exec_())
and the window looks like this =
Screenshot 2019-06-10 at 23.58.17
But I need it to be centered since my game has to have a starting screen where the buttons are in the center. I think I'm going to need to use a QStackedWidget to show multiple screens with a button click.
-
A QLabel aligns it's text left by default as described here: https://doc.qt.io/qt-5/qlabel.html#alignment-prop
-
But when the self.handler variable is a custom class that inherits from QWidget and that custom class has its own layout set it's still on the left.
-
This is actually a problem made by me because the buttons only appear on the left side when I gave a maximum size for them.
Although this kind of still leaves me with not a solution.
self.button_layout.addWidget(self.start_button, Qt.AlignRight)
that button is smaller than others, with a fixed width of 50 pixels
the other buttons are as big as the window lets them be.
But after that, why is the smaller button still appearing on the left side if i told it to align right?
Screenshot 2019-06-11 at 00.58.49
-
@Shoto What is button_layout exactly?
You can use https://doc.qt.io/qt-5/qspaceritem.html to stick a widget to a dedicated position. So, you would put a spacer to the left of your small button, the spacer then would push the button to the right side.