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. Applying StyleSheet to Custom Window

Applying StyleSheet to Custom Window

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 9.4k 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.
  • T Offline
    T Offline
    Thibaut
    wrote on last edited by
    #1

    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
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      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
      0
      • T Offline
        T Offline
        Thibaut
        wrote on last edited by
        #3

        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
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          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
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            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
            0
            • D Offline
              D Offline
              deimos
              wrote on last edited by
              #6

              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
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [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
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  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
                  0
                  • T Offline
                    T Offline
                    Thibaut
                    wrote on last edited by
                    #9

                    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
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      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
                      0
                      • G Offline
                        G Offline
                        giesbert
                        wrote on last edited by
                        #11

                        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
                        0

                        • Login

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