Qt Forum

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

    Unsolved How to see the area detected by QMouseEvent

    General and Desktop
    3
    9
    358
    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.
    • L
      luciole last edited by

      Hello,

      I have a QWidget A to which I assigned a QVBoxLayout layout, and this layout contains two widgets B and C (two QLabel). I implemented the mousePressEvent method on my parent widget A. The event seems to be triggered everywhere in a rectangle containing both B and C.

      However, when I do for instance A->setStyleSheet("background-color: red"), only the background color of widgets B and C gets set to red. How can I change the background color of the whole rectangle containing B and C? From what I saw, I can't change the style of the layout itself.

      Thanks!

      JonB 1 Reply Last reply Reply Quote 0
      • JonB
        JonB @luciole last edited by

        @luciole

        A->setStyleSheet("background-color: red"), only the background color of widgets B and C gets set to red

        This should indeed be setting A's background to red. (No, you can't apply a style to a layout.)

        1 Reply Last reply Reply Quote 0
        • L
          luciole last edited by

          But it doesn't seem to, I get this:

          7dc75dcd-8ba4-4d9b-a136-56702243316d-image.png

          when putting this code inside my QWidget A:

              QVBoxLayout *layout = new QVBoxLayout;
          
              QLabel *text1 = new QLabel;
              QLabel *text2 = new QLabel;
          
              text1->setText("short text");
              text2->setText("some longer text");
          
              layout->addWidget(text1);
              layout->addWidget(text2);
          
              layout->setAlignment(text1, Qt::AlignHCenter);
              layout->setAlignment(text2, Qt::AlignHCenter);
          
              setLayout(layout);
              setStyleSheet("background-color: red;");
          
          JonB 1 Reply Last reply Reply Quote 0
          • JonB
            JonB @luciole last edited by JonB

            @luciole
            It's supposed to work, as per e.g. https://wiki.qt.io/How_to_Change_the_Background_Color_of_QWidget, Using Style Sheet. You sure A is a QWidget, or is it actually your own class?

            1 Reply Last reply Reply Quote 0
            • B
              Bonnie last edited by

              @luciole @JonB
              From the OP's code, seems A is a custom widget subclassing QWidget.
              So it will need some extra code in paintEvent, just as in the end of that "HowTo" you provide.

              JonB 1 Reply Last reply Reply Quote 0
              • JonB
                JonB @Bonnie last edited by JonB

                @Bonnie

                From the OP's code, seems A is a custom widget subclassing QWidget.

                That is what I was wondering/trying to tease out of OP. I wish people would say "It's a custom widget, let me know if that's relevant".

                According to my reading, you can avoid the paintEvent subclass override, which is a bit irritating, if you want to. You need to ensure you have Q_OBJECT macro in the header of the derived class --- that's what I was going to ask the OP --- and then, I'm not certain whether needed, but I think you also need customWidget->setAttribute(Qt::WA_StyledBackground, true);. Solution at https://stackoverflow.com/a/52434565/489865 is from 2018. Read through https://stackoverflow.com/questions/7276330/qt-stylesheet-for-custom-widget, one guy claims to manage without the WA_StyledBackground, but I think the majority use it.

                This question/answer may be relevant to me, I need to look at some code tomorrow where I was having this sort of trouble with background color and stylesheet....

                B 1 Reply Last reply Reply Quote 0
                • B
                  Bonnie @JonB last edited by

                  @JonB
                  Oh, didn't know that before, glad to know there's such an attribute.
                  Since I also feel overriding paintEvent annoying, I've been avoiding using style sheets on custom widgets. :)

                  JonB 1 Reply Last reply Reply Quote 0
                  • JonB
                    JonB @Bonnie last edited by

                    @Bonnie said in How to see the area detected by QMouseEvent:

                    Since I also feel overriding paintEvent annoying, I've been avoiding using style sheets on custom widgets. :)

                    Yeah, that's a shame limitation. I'm going to look into this for my code tomorrow, I had not appreciated you should have to do anything at all to use stylesheet on derived.

                    1 Reply Last reply Reply Quote 0
                    • JonB
                      JonB last edited by JonB

                      @Bonnie, @luciole
                      Well blow me down! This is indeed the problem/solution in my own code!

                      Please see my recent report of this issue in https://forum.qt.io/topic/113109/widget-background-color-at-runtime. There I have a QWidget-derived class, whose background I wish to set, which is the child widget of a QMdiSubWindow. It worked in Designer but not at runtime.

                      At the time I accepted @J-Hilk's reply at https://forum.qt.io/topic/113109/widget-background-color-at-runtime/2

                      thats a bug in Qt, you have 3 options:

                      • upgrade to 5.12.6/7
                      • only set stylesheets in designer
                      • only set stylesheets in code

                      Edit:
                      found the related bug report:
                      https://bugreports.qt.io/browse/QTBUG-79545

                      Maybe there is a Qt 5.12.2 issue, but it does not seem to be my case.

                      Until now I had "solved" this by placing colored background not on my subclassed widget but instead on its parent, which is a QMdiSubWindow.

                      Instead, I now find that all I have to do is add

                      myDerivedWidget->setAttribute(Qt::WA_StyledBackground, true);
                      

                      and the background color works at run-time!

                      FWIW, I tested and I do not have to have the Q_OBJECT macro on the derived class to make this work, though it does no harm. I do have to set the attribute.

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