如何使用QPainter绘制? [英] How do I paint with 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 QPaintDevice
s are usually double buffered in QT4, QPainter::end()
might be where the image is transferred to the graphic memory.
这篇关于如何使用QPainter绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!