[英]How do I paint with QPainter?
I have started learning Qt recently.最近开始学习Qt。
I did not get quite clear how can I paint using QPainter
class.我不太清楚如何使用QPainter
class 进行绘画。 Let`s say I want just to place a few points in the window:假设我只想在 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?你能告诉我我哪里错了吗?
void SimpleExampleWidget::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(Qt::blue);
painter.setFont(QFont("Arial", 30));
painter.drawText(rect(), Qt::AlignCenter, "Qt");
}
http://doc.qt.digia.com/4.4/qpainter.html http://doc.qt.digia.com/4.4/qpainter.html
I think the problem is your QPainter
initialization.我认为问题在于您的QPainter
初始化。
You could just create the QPainter
like in hydroes' answer, it would look like this then:您可以像在 Hydroes 的答案中那样创建QPainter
,然后看起来像这样:
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. QPainter::begin(this)
和QPainter::end()
调用在第二个示例中是必不可少的。 In the first example, you can think of QPainter::begin(this)
being called in the constructor and QPainter::end()
in the destructor在第一个示例中,您可以认为QPainter::begin(this)
在构造函数中被调用,而QPainter::end()
在析构函数中被调用
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.出于这个原因,我猜测:由于QPaintDevice
通常在 QT4 中进行双缓冲,因此QPainter::end()
可能是将图像传输到图形 memory 的位置。
You need to initialize the painter with the widget you want to paint on.您需要使用要在其上绘制的小部件初始化画家。
Usually this is done using the constructor which takes a QPaintDevice
but you can also do it by calling begin()
.通常这是使用带有QPaintDevice
的构造函数完成的,但您也可以通过调用begin()
来完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.