Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Redraw text in custom widget removes button functionality (QPushbutton inherited) [SOLVED]



  • When deriving from QPushbutton all my QSS is applied as my paintevent is working. However when I insert my paintevent, Text is no longer drawn instead. Below is my paint event I use to draw the Push button however I am not sure if I am missing something to use this command on a derived QPushbutton.

    @void GUIButton::paintEvent(QPaintEvent* aEvent)
    {
    QStyleOption Opt;
    Opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget,&Opt,&p,this);
    }@

    Do I need to change the QStyle::PE_Widget to another QStyle ?



  • Figure out my problem however have created another problem because of it instead of actually having a button the button has now completely disappeared below is a simple example to show the problem.

    custom_button.cpp

    @#include "custom_button.h"

    Custom_Button::Custom_Button(QWidget *parent) :
    QPushButton(parent)
    {
    }

    void Custom_Button::paintEvent(QPaintEvent* aEvent)
    {
    QStyleOption Opt;
    Opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_PanelButtonCommand,&Opt,&p,this);
    style()->drawItemText(&p,this->rect(),Qt::AlignCenter,(this->palette()),true,this->text());
    }
    @

    custom_button.h
    @#ifndef CUSTOM_BUTTON_H
    #define CUSTOM_BUTTON_H

    #include <QPushButton>
    #include <QStyleOption>
    #include <QPainter>

    class Custom_Button : public QPushButton
    {
    Q_OBJECT
    public:
    explicit Custom_Button(QWidget *parent = 0);

    void paintEvent(QPaintEvent* aEvent);
    

    signals:

    public slots:

    };

    #endif // CUSTOM_BUTTON_H
    @

    MainWindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    //#include "custom_button.h"

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    mainCustom_Button * Button = new Custom_Button();
    //QPushButton *Button = new QPushButton();
    QVBoxLayout * layout = new QVBoxLayout();
    layout->addWidget(Button);
    
    QWidget * CenterWidget = new QWidget();
    setCentralWidget(CenterWidget);
    centralWidget()->setLayout(layout);
    

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    Everything else is just a standard project, now what I am seeing is that the button isn't actually being drawn at all. If anyone has any idea why this is happening that would be great.


Log in to reply