简体   繁体   English

QPainter 在 QT/C++ 中将 QBrush 设置为 drawText

[英]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).问题是 - 我试图通过将画笔颜色设置为初始显示时总颜色的计算和减少部分来设置淡出(#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.不过,因为我使用样式表,我看到的是文本出现了 10 秒(因为那是计划中的淡出周期),然后它会消失而不会淡出。 But without the stylesheet setting color: #bbbbbb;, the text will not show.但是如果没有样式表设置颜色:#bbbbbb;,文本将不会显示。 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(). getFrame() 返回一个 qint64,因此它初始化为 -1,然后当事情开始时,它被设置为 0。有一个 QTimer 每 250 毫秒触发一次,在该方法中它递增帧字段并调用 update()。 This is inside my paintEvent() protected method where this drawText is.这是在这个 drawText 所在的我的paintEvent() 保护方法中。 I'm using QT 5.15 and C++ 11 if any of that makes a difference.我正在使用 QT 5.15 和 C++ 11 如果其中任何一个有所不同。

Any thoughts?有什么想法吗?

qt use QPen for drawing text. qt 使用 QPen 绘制文本。 Set a pen instead of brush for QPainter:为 QPainter 设置笔而不是画笔:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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