简体   繁体   中英

QPainter set QBrush to drawText in QT/C++

I posted before about text not displaying - I got that working but by a slightly different method - setting a stylesheet(around the constructor). The thing is - I am trying to set up a fadeout by setting the brush color to a calculated and decreasing fraction of the total color at initial display(#bbbbbb). What I see though because I use the stylesheet is the text appearing for 10 seconds (as that's the planned fadeout cycle) and then it disappears without fading out. But without the stylesheet setting color: #bbbbbb;, the text will not show. Here is my code for that text:

if((getFrame() >= 0) && (getFrame() < 10))
    {
    int iColorComponent = ((10 - getFrame()) / 10) * 0xbb;

    painter.setBrush(QBrush(QColor(iColorComponent, iColorComponent, iColorComponent), Qt::SolidPattern));
    painter.drawText(245, 330, 125, 15, Qt::AlignCenter, tr("Ready"));
    }

getFrame() returns a qint64 so it initializes as -1 and when things start then, it is set to 0. There's a QTimer firing off every 250 ms and in that method it increments the frame field and calls update(). This is inside my paintEvent() protected method where this drawText is. I'm using QT 5.15 and C++ 11 if any of that makes a difference.

Any thoughts?

qt use QPen for drawing text. Set a pen instead of brush for QPainter:

painter.setPen(QPen(QColor(iColorComponent, iColorComponent, iColorComponent), Qt::SolidPattern));
painter.drawText(245, 330, 125, 15, Qt::AlignCenter, tr("Ready"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM