inheritance from QWidget - StyleSheet problem



  • Hello~

    I made custom widget that inheritance from QWidget.
    But I set stylesheet option to my widget, that NOT working.
    This is my code. Could you check this and let me know what wrong with me.

    myWidget.h
    class myWidget : public QWidget {
    Q_OBJECT
    public :
    explicit myWidget(QWidget *parent=0);
    }

    myWidget.cc
    myWidget::myWidget(QWidget *parent) : QWidget(parent)
    {
    // do nothing...
    }

    main.cc
    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    QMainWindow win;
    //QWidget *w = new QWidget(&win); // CASE 1
    myWidget *w = new myWidget(&win); // CASE 2
    w->setStyleSheet("background-color:red;");
    win.show();
    return app.exec();
    }

    When I enable "CASE 1" (w == QWidget), that draw red color,
    but enable "CASE 2" (w == myWidget), that draw nothing.

    Thank you.


  • Qt Champions 2016

    AS far as I know, to use a styles heet for you won widget, you must override the paint event

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

    please see
    http://stackoverflow.com/questions/7276330/qt-stylesheet-for-custom-widget



  • @mrjj
    Your comment solved this problem. Thanks a lot!! :-)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.