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. QWidget custom styling
Forum Updated to NodeBB v4.3 + New Features

QWidget custom styling

Scheduled Pinned Locked Moved Unsolved General and Desktop
qwidgetstylestylesheet
14 Posts 3 Posters 7.3k Views 2 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.
  • J Offline
    J Offline
    Joel Bodenmann
    wrote on 16 Apr 2016, 10:15 last edited by
    #1

    I'd like to show the user a little pop-up box that contains information about a widget when he's hovering with the mouse over it for a certain time. The infobox mainly contains information in form of text and small buttons. Therefore, I subclass QWidgetand add that to the scene using QGraphicsScene::addWidget() which works nicely.

    My problem is the styling of the widget: I'd like to customize the following:

    • Background color
    • Text color
    • Border width and color
    • Rounded corners

    Colors can be changed using the QPalette. Border size with QStyleSheet. How about the rounded corners?
    Is there a better approach in general?

    Industrial process automation software: https://simulton.com
    Embedded Graphics & GUI library: https://ugfx.io

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 16 Apr 2016, 10:50 last edited by mrjj
      #2

      Hi
      What about using a QFrame as base and just use stylesheet ?
      QFrame{background-color: red; border: 1px solid black; border-radius: 10px;}

      Not tried with QGraphicsScene though :)

      alternate text

      J 1 Reply Last reply 16 Apr 2016, 12:47
      0
      • M mrjj
        16 Apr 2016, 10:50

        Hi
        What about using a QFrame as base and just use stylesheet ?
        QFrame{background-color: red; border: 1px solid black; border-radius: 10px;}

        Not tried with QGraphicsScene though :)

        alternate text

        J Offline
        J Offline
        Joel Bodenmann
        wrote on 16 Apr 2016, 12:47 last edited by Joel Bodenmann
        #3

        @mrjj Thank you for your suggestion!
        Two problems with that:

        1. While I see the rounder corners of the border the red background still is rectangular.
        2. Now the items that are part of the layout have a black border around them too


        The screenshot shows a QFormLayout with a QLabel using the text "Hello World"

        Industrial process automation software: https://simulton.com
        Embedded Graphics & GUI library: https://ugfx.io

        M 1 Reply Last reply 16 Apr 2016, 12:59
        0
        • J Joel Bodenmann
          16 Apr 2016, 12:47

          @mrjj Thank you for your suggestion!
          Two problems with that:

          1. While I see the rounder corners of the border the red background still is rectangular.
          2. Now the items that are part of the layout have a black border around them too


          The screenshot shows a QFormLayout with a QLabel using the text "Hello World"

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 16 Apr 2016, 12:59 last edited by mrjj
          #4

          @Joel-Bodenmann
          Ok. hmm.
          And this is using a QFrame ?
          Im wondering if QFrame is using the flag to not get bg painted before paintEvent.

          J 1 Reply Last reply 16 Apr 2016, 13:24
          0
          • M mrjj
            16 Apr 2016, 12:59

            @Joel-Bodenmann
            Ok. hmm.
            And this is using a QFrame ?
            Im wondering if QFrame is using the flag to not get bg painted before paintEvent.

            J Offline
            J Offline
            Joel Bodenmann
            wrote on 16 Apr 2016, 13:24 last edited by
            #5

            @mrjj
            Yes, that is a QFrame.
            QGraphicsScene::addWidget() takes Qt::WindowFlags as a parameter but as far as I can tell it's not related to this issue.

            Any ideas?

            Industrial process automation software: https://simulton.com
            Embedded Graphics & GUI library: https://ugfx.io

            M 1 Reply Last reply 16 Apr 2016, 13:26
            0
            • J Joel Bodenmann
              16 Apr 2016, 13:24

              @mrjj
              Yes, that is a QFrame.
              QGraphicsScene::addWidget() takes Qt::WindowFlags as a parameter but as far as I can tell it's not related to this issue.

              Any ideas?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 16 Apr 2016, 13:26 last edited by
              #6

              @Joel-Bodenmann
              hmm not really then.
              I was thinking of
              setAttribute(Qt::WA_NoSystemBackground);.

              J 1 Reply Last reply 16 Apr 2016, 13:28
              0
              • M mrjj
                16 Apr 2016, 13:26

                @Joel-Bodenmann
                hmm not really then.
                I was thinking of
                setAttribute(Qt::WA_NoSystemBackground);.

                J Offline
                J Offline
                Joel Bodenmann
                wrote on 16 Apr 2016, 13:28 last edited by
                #7

                @mrjj
                Well if I do that then I see the two labels but the actual rectangular box with the rounder border of the QFrame is gone.

                Industrial process automation software: https://simulton.com
                Embedded Graphics & GUI library: https://ugfx.io

                M 1 Reply Last reply 16 Apr 2016, 13:42
                0
                • J Joel Bodenmann
                  16 Apr 2016, 13:28

                  @mrjj
                  Well if I do that then I see the two labels but the actual rectangular box with the rounder border of the QFrame is gone.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 16 Apr 2016, 13:42 last edited by
                  #8

                  @Joel-Bodenmann

                  is the QFormLayout inside anything ?

                  J 1 Reply Last reply 16 Apr 2016, 13:47
                  0
                  • M mrjj
                    16 Apr 2016, 13:42

                    @Joel-Bodenmann

                    is the QFormLayout inside anything ?

                    J Offline
                    J Offline
                    Joel Bodenmann
                    wrote on 16 Apr 2016, 13:47 last edited by Joel Bodenmann
                    #9

                    @mrjj
                    Yes. I create the layout (and the label that goes into the layout) in the constructor of my SchematicPopup class which inherits from QFrame. I use setLayout() in the constructor to apply the layout to the QFrame.

                    Industrial process automation software: https://simulton.com
                    Embedded Graphics & GUI library: https://ugfx.io

                    kshegunovK 1 Reply Last reply 16 Apr 2016, 15:58
                    0
                    • J Joel Bodenmann
                      16 Apr 2016, 13:47

                      @mrjj
                      Yes. I create the layout (and the label that goes into the layout) in the constructor of my SchematicPopup class which inherits from QFrame. I use setLayout() in the constructor to apply the layout to the QFrame.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on 16 Apr 2016, 15:58 last edited by
                      #10

                      @Joel-Bodenmann
                      Try using a custom graphics effect for your widget.

                      Read and abide by the Qt Code of Conduct

                      J 1 Reply Last reply 16 Apr 2016, 16:18
                      0
                      • kshegunovK kshegunov
                        16 Apr 2016, 15:58

                        @Joel-Bodenmann
                        Try using a custom graphics effect for your widget.

                        J Offline
                        J Offline
                        Joel Bodenmann
                        wrote on 16 Apr 2016, 16:18 last edited by
                        #11

                        @kshegunov
                        Thank you for your suggestion! Can you be a bit more specific? I'm having troubles figuring out a way to implement a QGraphicsEffect to make rounder corners.

                        Industrial process automation software: https://simulton.com
                        Embedded Graphics & GUI library: https://ugfx.io

                        kshegunovK 1 Reply Last reply 16 Apr 2016, 16:28
                        0
                        • J Joel Bodenmann
                          16 Apr 2016, 16:18

                          @kshegunov
                          Thank you for your suggestion! Can you be a bit more specific? I'm having troubles figuring out a way to implement a QGraphicsEffect to make rounder corners.

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on 16 Apr 2016, 16:28 last edited by kshegunov
                          #12

                          @Joel-Bodenmann
                          I haven't used the effects framework myself, but as far as I understand how they work you could try the following:

                          1. Set your widget attributes to include Qt::WA_TranslucentBackground
                          2. Subclass QGraphicsEffect and implement the pure virtual QGraphicsEffect::draw(QPainter *)
                            • in that method draw manually the window's border and background using the provided QPainter argument (you should be able to provide even more complex behavior than rounded corners if needed, but for your case QPainter::drawRoundedRect should suffice).
                            • the geometry of the widget itself you should be able to get from QGraphicsEffect::boundingRect
                          3. Install an instance of your effect on your widget with QWidget::setGraphicsEffect

                          Note that the effect is supposed to be inherited through child widgets, so I'm not completely sure it won't interfere with the labels/buttons you add to the widget. In any case you could patch that up by holding a reference to the relevant widget in the effect itself.

                          I hope that helps.

                          Read and abide by the Qt Code of Conduct

                          J 1 Reply Last reply 17 Apr 2016, 14:35
                          1
                          • kshegunovK kshegunov
                            16 Apr 2016, 16:28

                            @Joel-Bodenmann
                            I haven't used the effects framework myself, but as far as I understand how they work you could try the following:

                            1. Set your widget attributes to include Qt::WA_TranslucentBackground
                            2. Subclass QGraphicsEffect and implement the pure virtual QGraphicsEffect::draw(QPainter *)
                              • in that method draw manually the window's border and background using the provided QPainter argument (you should be able to provide even more complex behavior than rounded corners if needed, but for your case QPainter::drawRoundedRect should suffice).
                              • the geometry of the widget itself you should be able to get from QGraphicsEffect::boundingRect
                            3. Install an instance of your effect on your widget with QWidget::setGraphicsEffect

                            Note that the effect is supposed to be inherited through child widgets, so I'm not completely sure it won't interfere with the labels/buttons you add to the widget. In any case you could patch that up by holding a reference to the relevant widget in the effect itself.

                            I hope that helps.

                            J Offline
                            J Offline
                            Joel Bodenmann
                            wrote on 17 Apr 2016, 14:35 last edited by
                            #13

                            Thank you for the explanation!
                            As this seems to be a bit more complex than it looked initially I will postpone this and just life with rectangular boxes for now :)

                            Industrial process automation software: https://simulton.com
                            Embedded Graphics & GUI library: https://ugfx.io

                            kshegunovK 1 Reply Last reply 17 Apr 2016, 14:43
                            0
                            • J Joel Bodenmann
                              17 Apr 2016, 14:35

                              Thank you for the explanation!
                              As this seems to be a bit more complex than it looked initially I will postpone this and just life with rectangular boxes for now :)

                              kshegunovK Offline
                              kshegunovK Offline
                              kshegunov
                              Moderators
                              wrote on 17 Apr 2016, 14:43 last edited by kshegunov
                              #14

                              @Joel-Bodenmann said:

                              As this seems to be a bit more complex than it looked initially I will postpone this and just life with rectangular boxes for now :)

                              Sure, if this is an option. However, implementing a class with a single function shouldn't be very complex ;)

                              class MyRoundedCornerEffects
                              {
                              public:
                                  MyRoundedCornerEffects(QObject * parent = NULL)
                                      : QObject(parent)
                                  {
                                  }
                                  
                                  virtual draw(QPainter * painter)
                                  {
                                      painter->setPen(QPen(Qt::black, 1));
                                      painter->setBrush(Qt::red);
                                      painter->drawRoundedRect(boundingRect(), 10, 10);
                                  }
                              };
                              
                              class MyWidgetWithEffect : public QWidget
                              {
                              public:
                                  MyWidgetWithEffect(QWidget * parent)
                                      : QWidget(parent)
                                  {
                                      setAttribute(Qt::WA_TranslucentBackground);
                                      setGraphicsEffect(new MyRoundedCornerEffects(this));
                                  }
                              };
                              

                              Read and abide by the Qt Code of Conduct

                              1 Reply Last reply
                              0

                              1/14

                              16 Apr 2016, 10:15

                              • Login

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