Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7984107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:09:45+00:00 2026-06-04T11:09:45+00:00

I am adding a widget to a parent widget without the use of a

  • 0

I am adding a widget to a parent widget without the use of a layout (because I’m using some animations that conflict with what the layout try to do).

I am trying to understand what I need to do to align the child widget to it’s parent manually (centre it horizontally and vertically even when the parent widget is resized).

I’ve tried calculating the position myself and using QWidget.move() and QWidget.setGeometry(), but neither worked properly as I seem to be unable to get the correct parent width and height.

Here is a simplified example of what I’m trying to achieve:

import sys
from PySide.QtGui import *
from PySide.QtCore import *

class Test( QWidget ):

    def __init__( self, parent=None ):
          super( Test, self ).__init__( parent )

    def sizeHint( self ):
        return QSize( 500, 500 )

    def addPage( self, widget ):
        widget.setParent( self )
        # THIS SEEMS UNPREDICTABLE:
        widget.move( self.sizeHint().width()/2, self.sizeHint().height()/2 ) 

if __name__ == '__main__':
    app = QApplication( sys.argv )

    mainW = Test()
    childW = QPushButton( 'centre me please' )
    mainW.addPage( childW )
    mainW.show()

    sys.exit( app.exec_() )
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:09:46+00:00Added an answer on June 4, 2026 at 11:09 am

    You can use QWidget.setGeometry to place your child widget at the centre. If you want it to stay that way no matter what happens to your main widget, then you need to catch events corresponding to the changes you expect.

    The following should work:

    #!/usr/bin/env python
    
    
    from PyQt4 import QtGui, QtCore
    import sys
    
    class Application(QtGui.QApplication):
    
        def __init__(self):
            QtGui.QApplication.__init__(self, sys.argv)
    
            self.main = MyWidget()
            self.main.show()
    
    
    class MyWidget(QtGui.QWidget):
        def __init__(self, parent=None):
            QtGui.QLabel.__init__(self, parent)
    
            self.setStyleSheet("QWidget { background-color : white}")
            self.setGeometry(0, 0, 200, 200)
    
            self.sub_widget = QtGui.QWidget(self)
            self.sub_widget.setStyleSheet("QWidget { background-color : black}")
            self.sub_widget.setGeometry((self.width()-100)/2, (self.height()-100)/2 , 100, 100)
    
        def resizeEvent(self, event):
    
            self.sub_widget.setGeometry((self.width()-100)/2, (self.height()-100)/2 , 100, 100)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like the default handling/layout that adding a dock widget to the main window
I was adding recent videos gadget on my blog. In that widget i was
I already created a wordpress widget for adding some featured from admin panel.I need
I'm adding some custom enhancements to a jQuery-UI tabbed widget. One of the things
I'm using jquerys autocomplete widget but I'm having a few issues trying to style
I'm designing a home screen widget. Layout is simple, I just began adding a
Some apps clog up the select widget activity by adding different configuration activities. like
I am trying to use the jQuery UI Modal Dialog widget on ASP.NET Web
I have a GtkMenu Widget and i am adding it to screen on button
Adding more than one child to a WPF StatusBar results in poor layout with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.