Building a custom paintEvent for a custom widget.
-
Hi. I have a custom widget and according to this document, custom widgets based on QWidget have trouble with stylesheets and will only take background as an argument. As a result, I have attempted to replicate the accompanying custom paintEvent code on my own widget. My confusions on this custom code, which I've placed onto my custom widget's cpp file, are listed in the comments of the file.
void TitleBar::paintEvent(QPaintEvent *) //So in order to call this, I would be required to call a QPaintEvent pointer about the widget I am trying to build, right? { QStyleOption opt; opt.initFrom(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); //Where does "style() come from? }
Thank you for reading through the series of questions, it seemed easier to understand as a whole rather than making
separate posts on a single piece of code. Also thanks in advance for those who let me know of any additional information required to solve this issue. -
Hi. I have a custom widget and according to this document, custom widgets based on QWidget have trouble with stylesheets and will only take background as an argument. As a result, I have attempted to replicate the accompanying custom paintEvent code on my own widget. My confusions on this custom code, which I've placed onto my custom widget's cpp file, are listed in the comments of the file.
void TitleBar::paintEvent(QPaintEvent *) //So in order to call this, I would be required to call a QPaintEvent pointer about the widget I am trying to build, right? { QStyleOption opt; opt.initFrom(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); //Where does "style() come from? }
Thank you for reading through the series of questions, it seemed easier to understand as a whole rather than making
separate posts on a single piece of code. Also thanks in advance for those who let me know of any additional information required to solve this issue.@Dummie1138 The link you posted is for Qt 4.8! Do you really use Qt 4.8?
"//So in order to call this, I would be required to call a QPaintEvent pointer about the widget I am trying to build, right?" - wrong, paintEvent is called by Qt framework whenever widget should repaint.
-
@Dummie1138 The link you posted is for Qt 4.8! Do you really use Qt 4.8?
"//So in order to call this, I would be required to call a QPaintEvent pointer about the widget I am trying to build, right?" - wrong, paintEvent is called by Qt framework whenever widget should repaint.
@jsulm I am using Qt 5.12 but this document from 4.8 is the only available resource I found, both from googling my issue (Custom widget not changing text color) and from this stackoverflow post on the same issue. Is this issue not in Qt 5 or 6?
Also in regards to calling a QPaintEvent pointer, doing something like this in the main code shouldn't happen, right?
paintEvent(*some_pointer);
-
@jsulm I am using Qt 5.12 but this document from 4.8 is the only available resource I found, both from googling my issue (Custom widget not changing text color) and from this stackoverflow post on the same issue. Is this issue not in Qt 5 or 6?
Also in regards to calling a QPaintEvent pointer, doing something like this in the main code shouldn't happen, right?
paintEvent(*some_pointer);
@Dummie1138 said in Building a custom paintEvent for a custom widget.:
from 4.8 is the only available resource I
https://doc.qt.io/qt-5/stylesheet-reference.html#list-of-stylable-widgets
-
I remember that if adding
setAttribute(Qt::WA_StyledBackground, true)
in the constructor, then there's no need to reimplement paintEvent to let stylesheet background work for custom widget, if you are using Qt5 :)@Bonnie Is that the page constructor or the custom widget constructor?
-
@Bonnie Is that the page constructor or the custom widget constructor?
@Dummie1138 custom widget's constructor,
setAttribute(Qt::WidgetAttribute attribute, bool on)
is a member function ofQWidget
.