How to see the area detected by QMouseEvent
-
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!
-
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;");
-
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 haveQ_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 needcustomWidget->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 theWA_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....
-
@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.
-
@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 aQMdiSubWindow
. 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-79545Maybe 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.