0
votes

I have created my own custom widget class, the code is below:

class myWidget : public QWidget
{
    Q_OBJECT
public:
    explicit myWidget(QWidget*parent=nullptr): QWidget(parent){}
    void enterEvent(QEvent *event) override
    {
        Q_UNUSED(event);
        qDebug()<<"Entered";
    }
};

In widget.cpp I have created it's instance, like below

myWidget *w;

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    w=new myWidget(this);
    w->setStyleSheet("background-color: rgb(138, 226, 52);");
    w->setGeometry(10,10,100,100);
    w->show();
}

As you can see my object is child of the this widget window but whenever I run the program it won't be visible to me in the window but when i go to top bottom i can see the console output of mine Entered, So it is present in my Widget window but I am not able to see it even I have set the background color as green, If I just use the Qt GUI to add one Widget into the form and set the background color and run the code then that Widget which I added from the GUI is visible but My Widget which i created by my own custom widget is not visible.

2
try painting something in your custom widget - ΦXocę 웃 Пepeúpa ツ

2 Answers

0
votes

It seems that using stylesheet in the classes directly inherited from QWidget doesn't work. I override the paintEvent method of my program to get the desired output. Here it the paintEvent code, I added on my custom widget class,

 void CustomWidget::paintEvent(QPaintEvent *)
 {
     QStyleOption opt;
     opt.init(this);
     QPainter p(this);
     style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
 }
0
votes

I think your class has to inherit from QFrame instead of QWidget in order to have the stylesheet working.