如何使用Qt QImage和QBrush渲染自定义背景?

时间:2012-09-28 16:36:53

标签: c++ qt

我正在尝试使用QImage for QT渲染自定义图像,但到目前为止我还没有使用我的代码。我不想从文件加载图像,我想使用QImage类和QBrush类以及其他任何需要的东西。我在使用API​​进行渲染方面并不是很出色,但是对于让我前进的任何帮助都会受到赞赏。

这是我得到的。我唯一真正给出的是图像'm_pImage'对象......

m_pImage = new QImage(ImageWidth, ImageHeight, QImage::Format_Indexed8);
m_pImage->setColorCount(255);

另外,我试图添加一些像这样的东西,但这部分不起作用:

QBrush* br = new QBrush(Qt::gray, Qt::Dense3Pattern);
br->setTextureImage(*m_pImage);
QPainter* paint = new QPainter(m_pImage);
paint->setPen(Qt::NoPen);
paint->setBrush(*br);
paint->drawRect(0, 0, ImageWidth, ImageHeight);
for(int i = 0; i < m_ulImageWidth; i++)
{
    for(int j = 0; j < ImageHeight; j++)
    {
        m_pImage->setPixel(i, j, qRgb(255, 255, 255));
    }
}

设置背景图片会更好,但只是让这个图像渲染画笔样式Dense3Pattern在两种情况下都是最好的。

以下是我一直使用的文档的链接QT Reference

提前致谢!!

1 个答案:

答案 0 :(得分:2)

Duh,我现在看到了我的问题。

所以我需要将我想要使用的所有颜色添加到我的colorTable中。在我这样做之后,我能够使用QImage :: setPixel(...)开始绘图。

for(int i = 0; i < 255; i++)
{
    m_pImage->setColor(i, qRgb(i, i, i));
}

我想这就是它的全部内容!