drawPrimitive does not draw over all primitive elements
-
Hi,
I have been trying to draw some primitive elements for a custom styled scrollbar. A snippet of my code:
void CustomStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption* opt, QPainter* p, const QWidget* widget) const { if (pe == PE_IndicatorArrowUp || pe == PE_IndicatorArrowDown) { QImage img; if (pe == PE_IndicatorArrowUp) { img = QImage(":/resources/ui/upward_arrow.png"); } else if (pe == PE_IndicatorArrowDown) { img = QImage(":/resources/ui/downward_arrow.png"); } QRect topPortion = QRect(QPoint(-2, -2), QSize(1.7 * opt->rect.width(), 1.7 * opt->rect.height())); QPixmap pixmap = QPixmap::fromImage(img); QSize size = pixmap.size().scaled(topPortion.size(), Qt::KeepAspectRatio); p->drawPixmap(topPortion.x(), topPortion.y(), size.width(), size.height(), pixmap); } else { QProxyStyle::drawPrimitive(pe, opt, p, widget); } }
Although I wanted to draw to separate primitive elements, only the PE_IndicatorArrowUp is drawn. What is even more surprising, is that the image for the downward arrow is drawn in place of the PE_IndicatorArrowUp primitive element while the PE_IndicatorArrowDown is left blank.
I have attached a screenshot of how it looks like.
I want to know why this behavior takes place and how to resolve it.
-
Use a debugger and see what is drawn when on which place. Also make sure you added the correct images.
-
Hi,
From a quick look at your code, aren't you always painting the image at the same place ?
-
@SGaist said in drawPrimitive does not draw over all primitive elements:
Hi,
From a quick look at your code, aren't you always painting the image at the same place ?
You're correct - opt.rect is not honored...
-
@Christian-Ehrlicher Thank you, I figured it out, using opt->rect's x and y attributes draws it in the correct place.