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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:33:44+00:00 2026-06-09T12:33:44+00:00

With the following code I tried to render a red push button using QStyle.drawControl()

  • 0

With the following code I tried to render a red push button using QStyle.drawControl():

#include <QtCore/QtCore>
#include <QtGui/QtGui>

class Widget : public QWidget
{
    virtual void paintEvent(QPaintEvent* event)
    {
        QStyleOptionButton opt;
        opt.palette = QPalette(Qt::red);
        opt.state = QStyle::State_Active | QStyle::State_Enabled;
        opt.rect = QRect(50, 25, 100, 50);
        QPainter painter(this);
        style()->drawControl(QStyle::CE_PushButton, &opt, &painter);
    }
};

int main(int argc, char** argv)
{
    QApplication app(argc, argv);
    Widget w;
    w.resize(200, 100);
    w.show();
    return app.exec();
}

However I get the following result:

enter image description here

How do I render a red push button using QStyle.drawControl()?

I’m using Qt 4.8.1 and Visal Studio 2010 on Windows XP.

  • 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-09T12:33:45+00:00Added an answer on June 9, 2026 at 12:33 pm

    The buttons are drawn by the native style engine, so the palette might not be used at all (see that question from the FAQ).

    You can use an actual button with a stylesheet that you pass as the last parameter to the own button’s style drawControl function.

    class Widget : public QWidget
    {
      // To allow the automatic deletion without parenting it
      QScopedPointer<QPushButton> button;
    public:
        Widget() : button(new QPushButton) {
          button->setStyleSheet("background-color: red");
        }
        virtual void paintEvent(QPaintEvent* event)
        {
            QStyleOptionButton opt;
            opt.state = QStyle::State_Active | QStyle::State_Enabled;
            opt.rect = QRect(50, 25, 100, 50);
            QPainter painter(this);
            button->style()->drawControl(QStyle::CE_PushButton, &opt, &painter, 
                                         button.data());
        }
    };
    

    But you will loose the native style, so you’ll have to fake it (bali182’s answer might be useful for that part).

    Or you can use the same button with colorize effect and call its render() function to paint it:

    Colorized Button

    class Widget : public QWidget {
        QScopedPointer<QPushButton> button;
    public:
        Widget() : button(new QPushButton) {
            QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect(button.data());
            effect->setColor(Qt::red);
            button->setGraphicsEffect(effect);
        }
        virtual void paintEvent(QPaintEvent* event) {
            button->setFixedSize(100, 50);
            button->render(this, QPoint(50, 25));
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I tried using the following code for a HTML page, but it doesn't work.
I tried using the following code in .java(main activity): final ImageView diskView1 = (ImageView)
I am using the following code to rendered the facebook login button <div id=facebook-login>
I want to get script's origin filename in global functions. I tried following code,
I just tried the following code, but the result seems a little strange. It
I have tried the following code but has no effect: Imports system.Runtime.InteropServices <DllImport(UxTheme.DLL, BestFitMapping:=False,
I have tried the following code: $('a.buildMenu').click(function (event) { // Prevent normal behaviour event.preventDefault();
I tried the following code tree. If I put the header file hello.h into
I tried the following code: import java.math.BigInteger; import org.apache.commons.codec.binary.Base32; import org.junit.Test; public class Sandbox
I tried the following code. Although I don't get any errors, it did not

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.