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. [SOLVED]please help me understand stylesheet
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]please help me understand stylesheet

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.0k Views 1 Watching
  • 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.
  • B Offline
    B Offline
    beetroot
    wrote on last edited by
    #1

    Hi
    I am having trouble with stylesheets

    I have a main window and have added a TestWidget that is derived from QWidget.
    I now try to set the background in the constructor for TestWidget-
    setStyleSheet("background-color: red");
    When the application is run the background color is not set.
    I tried this with in QDesigner, the background color is displayed in QDesigner but again when the application is run the background color is not set.

    If I use a QWidget then the background is as expected.

    What am I doing wrong

    Thanks

    beetroot

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dbzhang800
      wrote on last edited by
      #2

      Hi, the answer can be found in the documentation: http://qt-project.org/doc/qt-5.1/qtwidgets/stylesheet-reference.html

      If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:

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

      The above code is a no-operation if there is no stylesheet set.

      Warning: Make sure you define the Q_OBJECT macro for your custom widget.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        beetroot
        wrote on last edited by
        #3

        Thanks!!

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on last edited by
          #4

          I recommend you use QTCreator to manage stylesheet.

          If you select your object in QtCreator ui designer page, you can edit the field "stylesheet", the code will be generated automatically.
          You can right click on your derived QWidget and select "promote to" then write the header class of your new widget. That way, you can use designer to manipulate the element

          #NameOfWidgetHere {
          background-color: red;
          }

          or

          QWidget#NameOfWidgetHere {
          background-color: red;
          }


          Free Indoor Cycling Software - https://maximumtrainer.com

          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