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 6970177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:40:36+00:00 2026-05-27T16:40:36+00:00

I have a QHBoxLayout which I add widgets to, one by one. I alternate

  • 0

I have a QHBoxLayout which I add widgets to, one by one.
I alternate by adding a Custom Widget, than a QLabel (repeating).
The QHBoxLayout is owned by a QGroupBox.

However, I notice that the spacing between the Custom Widget and the QLabel is ‘irregular’ when there are few widgets added to the Layout.

I intend for the QLabels to be centered in the gap between the custom widgets, but the QLabels only approach the centre as more widgets are added.

These screenshots show the two cases.
The desired case (only occuring with lots of widgets in the layout),
and the undesired case (occuring when little widgets are in the layout).

Irregular Spacing in the bottom case, fine in the top.

As you can see in the bottom case, the QLabels are not centered between the Custom Widgets. Instead, they are very far to the right!

What is causing this behaviour??

I believe the QGroupBox has a centered (horizontal) alignment and the QLabels have a fixed width of 10 (or 20 for the ‘->’ QLabel) pixels (to avoid ugly overlap).

Any help at all would be greatly appreciated!
Thanks!

Specs:
python 2.7.1
PyQt4
Windows 7

The instantiation of the QHBoxLayout is absolutely normal and similiar to all examples.
Here is the code for the filling of the layout.

for i in range (0,len(Reactants)):

    self.WidgetHouse.Reaction_Element_Layout.addWidget(eval('self.OverallContainer_Reactants.Reactant_'+str(i)))

    # self.WidgetHouse.Reaction_Element_Layout           is the QHBoxLayout
    # self.OverallContainer_Reactants.Reactant_'+str(i)       is a Custom Widget

    if i != (len(Reactants)-1):
        tmp = QtGui.QLabel('+')
        tmp.setFixedWidth(10)
        tmp.setAlignment(QtCore.Qt.AlignCenter)
        self.WidgetHouse.Reaction_Element_Layout.addWidget(tmp)

    else:
        tmp = QtGui.QLabel('->')
        tmp.setFixedWidth(20)
        tmp.setAlignment(QtCore.Qt.AlignCenter)
        self.WidgetHouse.Reaction_Element_Layout.addWidget(tmp)

EDIT:

Setting a fixed width of the QLabels (tmp.setFixedWidth(10)) is the source of the ‘right anchoring’.
However, not setting a fixed width results in the same space being dedicated to QLabels and Custom Widgets in the layout, leading to an overlap of QLabels and Custom Widgets.

Fixed Width Labels vs Layout modulated widths

Identical to the code above, discluding ‘tmp.setFixedWidt(10)’

What can I do to prevent this that’s not completely horrible?
Can I move the Labels ‘back’ from the front?
(Calling .raise_() on all the custom widgets after everything was added to the layout did not work)

(Or will I have to do something like manually calculate the appropriate width of the labels based off the amount of widgets in the layout? Yuck!)

ANOTHER EDIT:

Progress:

I do not change the maximum/minimum (or fixed) width of the QLabels, but I do set their alignment to center.
Instead, I set a minimum width of the custom widget.
This fixes the apparent ‘overlapping’ (which wasn’t really the case) and makes the Labels appear ‘more centered’.

However, as you can see, the QLabels still aren’t perfectly centered – too far right.
If I don’t set a center alignment on the QLabels they are too far left.

What could be the problem now??

Overlapping solved, anchoring still an issue

(I do not set a maximum width on the labels)

Thanks for all the help so far guys!

  • 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-05-27T16:40:36+00:00Added an answer on May 27, 2026 at 4:40 pm

    Solution to all problems:

    Give the custom widgets a fixed width.

    Results in perfectly centered, not ‘overlapping’ widgets.
    🙂

    Thanks for all the help guys!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two widgets, one arbitrary (usually a QLineEdit), and one QLabel which displays
I have this little test-case which is supposed to show two widgets, with one
I have the following structure: QScrollArea - QWidget (with QHBoxLayout) - QLabel - QLabel
I have a widget which I'm putting in a QVBoxLayout. This widget's layout is
I have a QHBoxLayout horizontal layout with a lot of list widgets added to
I have two QTreeView s in a QHBoxLayout . <layout class=QHBoxLayout name=lay_disk_content> <item> <widget
I have a widget which changes when an option is toggled. This invalidates all
Say I have a QHBoxLayout where there are 2 QTextEdit s and between them
I have a QVBoxLayout that I've added a few widgets to, via addWidget() .
I'm writing my own class, very similar to a toolbar, which will contain widgets

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.