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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:06:11+00:00 2026-05-23T14:06:11+00:00

I am facing problem while Setting text in QPushbuuton. I set the text on

  • 0

I am facing problem while Setting text in QPushbuuton.
I set the text on a button , but it never appears.

I have written a Custom Class for mousepress,mouserelease event for QPushbutton.

I Promote my QPushbutton to this class.

My code looks like :

//Customclass.h

enum ButtonState
{
Normal,
MouseOver,
Pushed
};

class CustomButtonStates : public QPushButton
{
    Q_OBJECT


public:
    explicit CustomButtonStates(QWidget *parent = 0,const QString &normal = "", const QString &active = "",QString strText = "");
    virtual ~CustomButtonStates();
    void mousePressEvent(QMouseEvent  *event);
    void mouseReleaseEvent(QMouseEvent  *event);
    void paintEvent(QPaintEvent *e);



private:

    QString m_PixmapNormal;
    QString m_PixmapActive;
    QString m_strText;
    bool m_bPressed;
    ButtonState state;

public:
    QImage *NormalImage;
    QImage *MouseOverImage;
    QImage *PushedImage;




};

// .cpp

CustomButtonStates::CustomButtonStates(QWidget *parent,const QString &normal, const QString &active,QString strText) :
    QPushButton(parent)
{

    m_PixmapNormal = normal ;
    m_PixmapActive = active ;
    m_strText = strText ;
    state = Normal;



}


void CustomButtonStates::mousePressEvent(QMouseEvent *event)
{
    QPushButton::mousePressEvent(event);
    state = Pushed;    
    this->repaint();

}

void CustomButtonStates::mouseReleaseEvent(QMouseEvent *event)
{
    QPushButton::mouseReleaseEvent(event);
    state = Normal;
    emit clicked();
    this->repaint();

}

void CustomButtonStates::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QImage *pic = NULL;

    NormalImage = new QImage(m_PixmapNormal);
    PushedImage = new QImage(m_PixmapActive);
    switch (state)
    {
    case Normal:
        pic = NormalImage;
        break;
    case MouseOver:
        pic = MouseOverImage;
        break;
    case Pushed:
        pic = PushedImage;
        break;
    default:
        pic = NormalImage;
        break;
    }

    painter.drawImage(0, 0, *pic);
}

CustomButtonStates::~CustomButtonStates()
{
    delete NormalImage;
    delete MouseOverImage;
    delete PushedImage;
}

In my Page :

 CustomButtonStates *btnMain ;
btnMain = new CustomButtonStates(this,":/Sampl1.png",":/Sample2.png","Users");
    btnMain->setText("Users");
    btnMain->setGeometry(QRect(3,6,65,33));

    connect(btnMain,SIGNAL(clicked()),this,SLOT(on_btnUsers_clicked()));
  • 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-23T14:06:12+00:00Added an answer on May 23, 2026 at 2:06 pm

    If you don’t need any of the stuff painted in the QPushButton except the text and your data (images/pixmaps), I suggest you can go a little deeper an directly inherits of QAbstractButton instead.

    In your button you will add a text member (plus accesors setText(const QString&...) and text()) then re-implement the paint event to draw your image and at last text above this background with one of the drawText() method of the QPainter.

    paintEvent will look like this :

    void YourButton::paintEvent(QPaintEvent* event)
    {
        QPainter painter(this);
        //hereadd some logic to determine witch pixmap goes background
        QPixmap* selectedPixmap ...
        painter.drawPixmap( rect(), *selectedPixmap);
        //here add some logic to set painter pen and brush 
        //according to button status (enabled/checked/...)
        painter.drawText( rect(), Qt::AlignCenter, text());
    }
    

    If you need fancier stuff like word wrapping, or Qt ‘css’ style to be applied to you text I suggest to use directly a QLabel and forward text accessor to this QLabel. In this case do not forget to re-implement resizeEvent to set correctly the overlay label on the top of your button rectangle any time it is changing.

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

Sidebar

Related Questions

I am facing a strange problem while running my application. I have a class
I am facing problem while aligning two text, one in center and other text
I am facing problem while upgrading an Higher version over Lower version. I have
I am trying to hide an array of elements but facing problem while writing
I have 2 classes Test(Base)and Program(child). Now I am facing some problem while downcasting.
I am facing problem while trying to open a simple text file in C#.
I'm facing a problem while unit testing my forms. The problem is that data
I am facing a problem while creating a static cursor in DB2. This is
I am facing a problem with FPDF while generating the PDF file with images
I am facing a problem while making Excel's LinEST function. My program goes like

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.