Unsolved How to see the area detected by QMouseEvent
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.
JonB last edited by
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.)
But it doesn't seem to, I get this:
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;");
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
QWidget, or is it actually your own class?
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
paintEventsubclass override, which is a bit irritating, if you want to. You need to ensure you have
Q_OBJECTmacro 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....
Oh, didn't know that before, glad to know there's such an attribute.
Since I also feel overriding
paintEventannoying, I've been avoiding using style sheets on custom widgets. :)
JonB last edited by
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.
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
found the related bug report:
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
Instead, I now find that all I have to do is add
and the background color works at run-time!
FWIW, I tested and I do not have to have the
Q_OBJECTmacro on the derived class to make this work, though it does no harm. I do have to set the attribute.