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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:24:56+00:00 2026-05-17T21:24:56+00:00

I need to place several instances of a custom QPushButton subclass adjacent to one

  • 0

I need to place several instances of a custom QPushButton subclass adjacent to one another. For some reason, the buttons overlap one another when painted. A simplified example of the problem is below.

Here is the (incorrect) output:

overlapping

Here is the code:

#include <QtGui>

class MyButton : public QPushButton {
public:
  explicit MyButton(Qt::GlobalColor color, QWidget *parent = NULL)
    : QPushButton(parent), color_(color) {
    setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
  }
  QSize sizeHint() const {
    return QSize(50, 25);
  }
protected:
  void paintEvent(QPaintEvent *) {
    QPainter painter(this);
    painter.setOpacity(0.5);
    painter.fillRect(0, 0, width(), height(), color_);
  }
private:
  Qt::GlobalColor color_;
};

int main(int argc, char **argv) {
  QApplication app(argc, argv);
  QWidget widget;
  QHBoxLayout *layout = new QHBoxLayout;
  layout->setSpacing(0);
  MyButton *w1 = new MyButton(Qt::red);
  MyButton *w2 = new MyButton(Qt::green);
  MyButton *w3 = new MyButton(Qt::blue);
  layout->addWidget(w1);
  layout->addWidget(w2);
  layout->addWidget(w3);
  widget.setLayout(layout);
  widget.show();
  return app.exec();
}

What is causing this, and how do I fix it? BTW, I tried something similar with regular QWidget subclasses (instead of QPushButton subclasses), and there is no problem. It is something peculiar to QPushButton.

UPDATE: I’m really thinking now that this is a bug. I submitted it to the Qt Bug Tracker; we’ll see what the Trolls think. In any case, deriving from QAbstractButton fixes the drawing problem … I just had to re-implement some of the functionality I needed.

UPDATE 2: The Trolls at Qt provided a solution (workaround?); I posted their fix as an answer below. I’m leaving it up to their team to determine if this is a feature or bug. It apparently only behaves differently on the Mac.

  • 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-17T21:24:57+00:00Added an answer on May 17, 2026 at 9:24 pm

    The solution is to add the following to the subclass:

    setAttribute(Qt::WA_LayoutUsesWidgetRect);
    

    Apparently it is only necessary on the Mac platform; Windows and Linux display the layout as expected.

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

Sidebar

Related Questions

I'm working on some 2D games with Pygame. I need to place several objects
I need to place several DatePicker widgets into the row. If there is not
I have several classes that I need to format. I need to place the
I need to place some graphic elements in a div that is supposed to
I have several commits already and I need to undo the latest one but
I have several large (200+ lines) of HTML that I need to place in
I need to place two repeated background images on the left and right border
I need to place the label text to left and the radio button to
I need to place default text to all rows for particular column in jquery
We need to place text over objects in a web page, similar to this:

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.