Qt 初学者:QPainter 小部件未渲染
我对 Qt 没有太多经验,并且在使用 QPainter
时遇到问题。 我正在尝试制作一个简单的图形小部件,它接受多个点并创建 QPoints
的 QVector
,然后使用该向量绘制多边形。但是,我的实施目前没有出现任何情况。我相当确定我已将小部件正确添加到窗口中,因为我可以看到它应该占据的空白空间。这让我相信问题出在图形小部件中。 如有任何帮助,我们将不胜感激。
标题:
//graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <QWidget>
#include <QPainter>
#include <QVector>
class Graph : public QWidget
{
Q_OBJECT
public:
Graph(QWidget *parent = 0);
QSize minimumSizeHint() const;
QSize maximumSizeHint() const;
QSize sizeHint() const;
void addPoint(int w, int h);
void clearPoints();
void drawGraph();
protected:
void paintEvent(QPaintEvent *event);
private:
QPen pen;
QBrush brush;
QPixmap pixmap;
QVector<QPoint> points;
};
#endif // GRAPH_H
来源:
//graph.cpp
#include "graph.h"
Graph::Graph(QWidget *parent)
: QWidget(parent)
{
points.resize(0);
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
void Graph::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(QPen(Qt::NoPen));
painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawPolygon(points);
}
QSize Graph::minimumSizeHint() const
{
return sizeHint();
}
QSize Graph::maximumSizeHint() const
{
return sizeHint();
}
QSize Graph::sizeHint() const
{
return QSize(500, 200);
}
void Graph::addPoint(int w, int h)
{
points.append(QPoint(w*2, h*2));
}
void Graph::clearPoints()
{
points.clear();
}
void Graph::drawGraph() {
points.prepend(QPoint(0,0)); //The base points of the graph
points.append(QPoint(500,0));
update();
points.clear();
}
I don't have much experience with Qt and I am having trouble using QPainter
.
I am trying to make a simple graphing widget which takes in a number of points and to create a QVector
of QPoints
, and then uses this vector to draw a polygon. However, nothing is appearing right now with my implementation. I am fairly certain that I have added the widget correctly to the window, as I can see the empty space it should occupy. This leads me to believe the problem to be in the graphing widget.
Any assistance is appreciated.
header:
//graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <QWidget>
#include <QPainter>
#include <QVector>
class Graph : public QWidget
{
Q_OBJECT
public:
Graph(QWidget *parent = 0);
QSize minimumSizeHint() const;
QSize maximumSizeHint() const;
QSize sizeHint() const;
void addPoint(int w, int h);
void clearPoints();
void drawGraph();
protected:
void paintEvent(QPaintEvent *event);
private:
QPen pen;
QBrush brush;
QPixmap pixmap;
QVector<QPoint> points;
};
#endif // GRAPH_H
source:
//graph.cpp
#include "graph.h"
Graph::Graph(QWidget *parent)
: QWidget(parent)
{
points.resize(0);
setBackgroundRole(QPalette::Base);
setAutoFillBackground(true);
}
void Graph::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(QPen(Qt::NoPen));
painter.setBrush(QBrush(Qt::green, Qt::SolidPattern));
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawPolygon(points);
}
QSize Graph::minimumSizeHint() const
{
return sizeHint();
}
QSize Graph::maximumSizeHint() const
{
return sizeHint();
}
QSize Graph::sizeHint() const
{
return QSize(500, 200);
}
void Graph::addPoint(int w, int h)
{
points.append(QPoint(w*2, h*2));
}
void Graph::clearPoints()
{
points.clear();
}
void Graph::drawGraph() {
points.prepend(QPoint(0,0)); //The base points of the graph
points.append(QPoint(500,0));
update();
points.clear();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
drawGraph()
中,对update()
的调用会发布一个事件,通知小部件自行绘制。然后清除这些点,drawGraph()
调用退出。之后,事件循环将处理更新事件并触发对 PaintEvent() 的调用,但到那时,点向量中已经没有要绘制的点了。不要将
paintEvent()
视为在小部件上永久绘制一些内容,并且该内容将永远显示,直到您清除它并绘制其他内容。无论何时需要重绘,paintEvent()
都需要能够从头开始绘制小部件。这通常是由于移动小部件、最小化和恢复等时系统发出的请求。这意味着您的点向量需要保留,直到您不再需要显示多边形或更改点为止。In
drawGraph()
, the call toupdate()
posts an event notifying the widget to paint itself. You then clear the points and thedrawGraph()
call exits. After that, the event loop will process the update event and trigger a call to thepaintEvent()
but by then, there are no points in the vector of points to paint.Don't think of the
paintEvent()
as painting something permanent onto the widget once that will be displayed forever until you clear it and paint something else. ThepaintEvent()
needs to be able to paint the widget from scratch whenever it needs to be redrawn. This is often due to a request from the system when the widget is moved, minimized and restored etc. This means your vector of points needs to remain until you no longer want a polygon to be displayed or the points are changed.看起来您可能只在点列表中添加了两个点。我认为不可能有一个只有两个点的多边形;尝试添加第三个点,看看是否得到一个三角形。
It looks like you might be adding only two points to your points-list. I don't think it is possible to have a polygon with only two points; try adding a third point and see if you get a triangle.