如何使用QPainter绘制? [英] How do I paint with QPainter?

查看:121
本文介绍了如何使用QPainter绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近才开始学习Qt。

我不太清楚如何使用 QPainter 类绘制。让我们说我只想在窗口中放几个点:

  
class PointDrawer:public QWidget {
Q_OBJECT
private:
QPainter p;
public:
PointDrawer(QWidget * obj = 0):QWidget(obj),p(this){}
virtual void paintEvent(QPaintEvent *){
p.setPen QPen(Qt :: black,3));
int n = 8;
while(...){
qreal fAngle = 2 * 3.14 * i / n;
qreal x = 50 + cos(fAngle)* 40;
qreal y = 50 + sin(fAngle)* 40;
p.drawPoint(QPointF(x,y));
i ++;
}
}
}

int main(int argc,char * argv []){
QApplication app(argc,argv);
PointDrawer抽屉;

drawer.resize(200,200);
drawer.show();

return app.exec();
}

然后,我什么都没有!
请你告诉我我想问题是你的 QPainter 初始化。



你可以创建 QPainter 就像在hydroes的回答,它看起来像这样: / p>

  class PointDrawer:public QWidget {
Q_OBJECT
public:
PointDrawer(QWidget * obj = 0):QWidget(obj){}
virtual void paintEvent(QPaintEvent *){
QPainter p(this)
p.setPen(QPen(Qt :: black,3));
int n = 8;
while(...){
qreal fAngle = 2 * 3.14 * i / n;
qreal x = 50 + cos(fAngle)* 40;
qreal y = 50 + sin(fAngle)* 40;
p.drawPoint(QPointF(x,y));
i ++;
}
}
}

这个,但我不真的推荐它(我只是喜欢其他解决方案):

  class PointDrawer:public QWidget {
Q_OBJECT
private:
QPainter p;
public:
PointDrawer(QWidget * obj = 0):QWidget(obj){}
virtual void paintEvent(QPaintEvent *){
p.begin(this);
p.setPen(QPen(Qt :: black,3));
int n = 8;
while(...){
qreal fAngle = 2 * 3.14 * i / n;
qreal x = 50 + cos(fAngle)* 40;
qreal y = 50 + sin(fAngle)* 40;
p.drawPoint(QPointF(x,y));
i ++;
}
p.end();
}
}

QPainter :: begin (this) QPainter :: end()调用在第二个例子中是必不可少的。在第一个例子中,你可以认为 QPainter :: begin(this)在构造函数和 QPainter :: end()在析构函数



原因,我猜猜:
As QPaintDevice 通常在QT4中双缓冲, QPainter :: end()可能是图像传输到图形内存的地方。


I have started learning Qt recently.
I did not get quite clear how can I paint using QPainter class. Let`s say I want just to place a few points in the window:


class PointDrawer: public QWidget {
    Q_OBJECT
private:
    QPainter p;
public:
    PointDrawer(QWidget* obj=0): QWidget(obj), p(this) {}
    virtual void paintEvent(QPaintEvent*) {
        p.setPen(QPen(Qt::black, 3));
        int n = 8;
        while(...) {
            qreal fAngle = 2 * 3.14 * i / n;
            qreal x = 50 + cos(fAngle) * 40;
            qreal y = 50 + sin(fAngle) * 40;
            p.drawPoint(QPointF(x, y));
                        i++;
        }
    }
}

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    PointDrawer drawer;

    drawer.resize(200, 200);
    drawer.show();

    return app.exec();
}

And after that, I got nothing!
Can you please tell me where I am wrong?

解决方案

I think the problem is your QPainter initialization.

You could just create the QPainter like in hydroes' answer, it would look like this then:

class PointDrawer: public QWidget {
    Q_OBJECT
public:
    PointDrawer(QWidget* obj=0): QWidget(obj) {}
    virtual void paintEvent(QPaintEvent*) {
        QPainter p(this)
        p.setPen(QPen(Qt::black, 3));
        int n = 8;
        while(...) {
                qreal fAngle = 2 * 3.14 * i / n;
                qreal x = 50 + cos(fAngle) * 40;
                qreal y = 50 + sin(fAngle) * 40;
                p.drawPoint(QPointF(x, y));
                        i++;
        }
    }
}

It could also use something like this, but I don't really recommend it (I just prefer the other solution):

class PointDrawer: public QWidget {
    Q_OBJECT
private:
    QPainter p;
public:
    PointDrawer(QWidget* obj=0): QWidget(obj) {}
    virtual void paintEvent(QPaintEvent*) {
        p.begin(this);
        p.setPen(QPen(Qt::black, 3));
        int n = 8;
        while(...) {
                qreal fAngle = 2 * 3.14 * i / n;
                qreal x = 50 + cos(fAngle) * 40;
                qreal y = 50 + sin(fAngle) * 40;
                p.drawPoint(QPointF(x, y));
                        i++;
        }
        p.end();
    }
}

The QPainter::begin(this) and QPainter::end() calls are essential in the second example. In the first example, you can think of QPainter::begin(this) being called in the constructor and QPainter::end() in the destructor

For the reason, I'm guessing: As QPaintDevices are usually double buffered in QT4, QPainter::end() might be where the image is transferred to the graphic memory.

这篇关于如何使用QPainter绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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