Qt Forum

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

    Applying StyleSheet to Custom Window

    General and Desktop
    4
    11
    8669
    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.
    • T
      Thibaut last edited by

      Hi

      I've been looking for an answer to this question on the forum but could not find anything. I am trying to apply a Stylesheet to a promoted widget but it just won't apply.

      I used the Designer interface to promote a QWidget to a QWidget inherited class TitiWidget.
      Still in the designer interface I added

      @
      border: 2px solid green;
      border-radius: 20px;
      padding: 2px;
      background-color: rgb(85, 85, 255);
      @

      to the "StyleSheet" field of the now custom widget.

      The widget's style is changed in the Design window but nothing is changed when I run the application. Demoting the Custom widget to a regular QWidget solves the problem and the style is applied.

      I tried to add TitiWidget#widget or QWidget#widget before the stylesheet arguments. No improvements.

      What should I do ?

      1 Reply Last reply Reply Quote 0
      • A
        andre last edited by

        Styling a custom widget through style sheets is not supported. Sorry.

        Edit:
        Well, let me specify that a bit: custom drawn custom widgets can not be styled using qss. You can use qss to style custom widgets that are just compositions of existing Qt widgets.

        1 Reply Last reply Reply Quote 0
        • T
          Thibaut last edited by

          By custom drawn widget, you do mean widgets inherited from Qwidget, right ?

          Are you aware of any plan to implement this in a next version ?

          1 Reply Last reply Reply Quote 0
          • A
            andre last edited by

            Me? No, not me. I don't work for Nokia :-)

            What I mean by custom drawn, are widgets that are based directly on QWidget and where you implement paintEvent() do do your own painting yourself.

            1 Reply Last reply Reply Quote 0
            • G
              giesbert last edited by

              If you just derive from QWidget and need that one styled, you have to do some stuff :-)

              @
              void TitiWidget::paintEvent(QPaintEvent* /p_pEvent/)
              {
              QStyleOption opt;
              opt.init(this);
              QStylePainter p(this);
              p.drawPrimitive(QStyle::PE_Widget, opt);
              }
              @

              Nokia Certified Qt Specialist.
              Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

              1 Reply Last reply Reply Quote 0
              • D
                deimos last edited by

                why not in the constructor of TitiWidget put this ?

                @
                setStyleSheet(
                "border: 2px solid green;"
                "border-radius: 20px;"
                "padding: 2px;"
                "background-color: rgb(85, 85, 255);"
                );@

                1 Reply Last reply Reply Quote 0
                • A
                  andre last edited by

                  [quote author="marcoB" date="1305200019"]why not in the constructor of TitiWidget put this ?

                  @
                  setStyleSheet(
                  "border: 2px solid green;"
                  "border-radius: 20px;"
                  "padding: 2px;"
                  "background-color: rgb(85, 85, 255);"
                  );@
                  [/quote]

                  Because it all depends on how TitiWidget is implemented if this is going to work or not.

                  1 Reply Last reply Reply Quote 0
                  • G
                    giesbert last edited by

                    It only works if you overwrite the paint event.
                    Otherwise nothing happens.

                    Nokia Certified Qt Specialist.
                    Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                    1 Reply Last reply Reply Quote 0
                    • T
                      Thibaut last edited by

                      Thanks a lot for your answers.

                      The
                      @
                      void TitiWidget::paintEvent(QPaintEvent* /p_pEvent/)
                      {
                      QStyleOption opt;
                      opt.init(this);
                      QStylePainter p(this);
                      p.drawPrimitive(QStyle::PE_Widget, opt);
                      }
                      @

                      trick works just fine. And it's actually in Qt's documentation. Shame on me.

                      Edit: please use code tags (@) before and after your code section; Andre

                      1 Reply Last reply Reply Quote 0
                      • A
                        andre last edited by

                        Nice trick to get at least the basic style sheet stuff painted correctly, but I still don't see a way to get where you can draw, let alone a way to style what you draw yourself using that style sheet...

                        1 Reply Last reply Reply Quote 0
                        • G
                          giesbert last edited by

                          That does not work at all. If you have a custom drawn widget, it can't be styled. Only if you do some basic style sheet stuff and paint on it later...

                          Nokia Certified Qt Specialist.
                          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

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