Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. inheritance from QWidget - StyleSheet problem

inheritance from QWidget - StyleSheet problem

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sung Sean
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      1
      • mrjjM mrjj

        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

        S Offline
        S Offline
        Sung Sean
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved