Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting a centralwidget to a QMainWindow doesn't center the widget!
Qt 6.11 is out! See what's new in the release blog

Setting a centralwidget to a QMainWindow doesn't center the widget!

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shoto
    wrote on last edited by
    #1

    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 = 0_1560200331695_9b8cc286-31bd-4760-b79f-12b3d406f09e-image.png 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.

    1 Reply Last reply
    1
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      A QLabel aligns it's text left by default as described here: https://doc.qt.io/qt-5/qlabel.html#alignment-prop

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        A QLabel aligns it's text left by default as described here: https://doc.qt.io/qt-5/qlabel.html#alignment-prop

        S Offline
        S Offline
        Shoto
        wrote on last edited by
        #3

        @Christian-Ehrlicher

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Shoto
          wrote on last edited by
          #4

          But now that I think about it, do I even need a QMainWindow?

          Since I'm not gonna be using any menu bars or stuff that sort for my game, it's just going to be a simple 2D game.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Shoto
            wrote on last edited by
            #5

            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?

            0_1560203941537_396ba7b9-6941-45f4-9781-0196ee3c5769-image.png Screenshot 2019-06-11 at 00.58.49

            jsulmJ 1 Reply Last reply
            0
            • S Shoto

              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?

              0_1560203941537_396ba7b9-6941-45f4-9781-0196ee3c5769-image.png Screenshot 2019-06-11 at 00.58.49

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved