Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    inheritance from QWidget - StyleSheet problem

    General and Desktop
    2
    3
    1381
    Loading More Posts
    • 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
      Sung Sean last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

        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 Reply Quote 1
        • S
          Sung Sean @mrjj last edited by

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

          1 Reply Last reply Reply Quote 0
          • First post
            Last post