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

  • Home
  • SEARCH
  • 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 8254787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:18:36+00:00 2026-06-08T01:18:36+00:00

I am struggling to understand something seemingly simple: I have a custom widget subclassing

  • 0

I am struggling to understand something seemingly simple:

I have a custom widget subclassing from QPushButton, multiple instances of which I am laying out in a QGridLayout(). The moment I add a paint() function and draw a background color to fill the button’s rect() the layout’s spacing does not seem to have an effect anymore.
Here is a screen shot to show what I mean:
enter image description here

This shows default QPushButtons that obey the layout’s spacing and my custom “buttons” that don’t.
I’m sure I just need to (re)implement something in my CustomButton but can’t find what it is. I tried setting contentMargins to no avail.

What am I missing? Maybe I need to not fill self.rect() but something else?
Here is the example code that produces above screen shot:

import sys
from PySide.QtGui import *
from PySide.QtCore import *
class CustomButton(QPushButton):
    def __init__(self, tool, icon=None, parent=None):
        super(CustomButton, self).__init__(parent)
        self.setSizePolicy(QSizePolicy.Expanding,  QSizePolicy.Fixed)
        self.setMinimumWidth(200)
        self.frameGeometry()

    def paintEvent(self, event):
        painter = QPainter(self)
        bgColor  = QColor(60, 60, 60)
        painter.fillRect(self.rect(), bgColor)

app = QApplication(sys.argv)
mainWindow = QWidget()
grid = QGridLayout()
grid.setSpacing(10)
mainWindow.setLayout(grid)
for i in xrange(4):
    btn1 = CustomButton('A')
    btn2 = QPushButton('B')
    grid.addWidget(btn1, 0, i)
    grid.addWidget(btn2, 1, i)

mainWindow.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-08T01:18:38+00:00Added an answer on June 8, 2026 at 1:18 am

    so a solution seems to be to manually adjust self.rect() to be a bit smaller, though I don’t understand why this is necessary as I thought that’s what the layout’s spacing is for:

    def paintEvent(self, event):
        rect = self.rect()
        rect.adjust(5,5,-5,-5)
        painter = QPainter(self)
        bgColor = QColor(60, 60, 60)
        painter.fillRect(rect, bgColor)
    

    This will give me the spacing I need. If anybody can shed light on whether this is a bug or a feature I’d be quite grateful.

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

Sidebar

Related Questions

there's something I'm struggling to understand with SQLAlchamy from it's documentation and tutorials. I
Bits and bitmask are something I have been struggling to understand for a while,
I'm struggling to understand how to be able to clone a git repository from
I'm unfamiliar with Javascript and have been struggling to understand how I'd go about
I am still struggling to understand the difference between *p, &p, and p. From
I am struggling to understand on java threads work so excuse this rather simple
I'm struggling to understand something about GIT. We've got a repository with a number
I have been struggling to understand the different behaviour when swapping pointers in C.
I am struggling with something that I think should be easily (ish). I have
I'm struggling to understand how to combine the TimePicker http://trentrichardson.com/examples/timepicker/ solution to my existing

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.