Qt中QGraphicsScene的setpixel [英] setpixel of QGraphicsScene in Qt

查看:446
本文介绍了Qt中QGraphicsScene的setpixel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅通过使用scene.addellipse()等来绘制线条或椭圆就很简单.

It's simple to draw line or ellipse just by using scene.addellipse(), etc.

QGraphicsScene scene(0,0,800,600);
QGraphicsView view(&scene);
scene.addText("Hello, world!");
QPen pen(Qt::green);
scene.addLine(0,0,200,200,pen);
scene.addEllipse(400,300,100,100,pen);
view.show();

现在我该怎么做才能设置一些像素颜色?我可以使用像qimage这样的小部件吗?顺便说一句,性能对我来说是个问题.谢谢

now what should i do to set some pixel color? may i use a widget like qimage? by the way performance is an issue for me.thanks

推荐答案

我认为在QImage上执行像素操作会大大降低应用程序的速度.一个不错的选择是将QGraphicsItem子类化为一个新类(例如QGraphicsPixelItem),并实现paint函数,如下所示:

I think that performing pixel manipulation on a QImage would slow down your application quite a lot. A good alternative is to subclasse QGraphicsItem in a new class, something like QGraphicsPixelItem, and implement the paint function like this:

// code untested

void QGraphicsPixelItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0)
{
    painter->save();

    foreach(const QPoint& p, pointList) {            
        // set your pen color etc.
        painter->drawPoint(p);
    }

    painter->restore();
}

其中pointList是用于存储要绘制的像素位置的某种容器.

where pointList is some kind of container that you use to store the position of the pixels you want to draw.

这篇关于Qt中QGraphicsScene的setpixel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆