在Qt图形视图中跟踪折线
Tracking polyline in graphics view in Qt
如何在图形视图中使用QPainter类跟踪鼠标折线和样条?我粘贴了我的代码。请检查和编辑。我正在生成一个折线上点击一个按钮。polyline.h
#include <QGraphicsSceneMouseEvent>
#include <QMouseEvent>
#include <QDebug>
#include "ui_mainwindow.h"
#include "qmath.h"
class polyline: public QObject, public QGraphicsItem
{
Q_OBJECT
public:
polyline();
QRectF boundingRect() const;
virtual void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget);
protected:
void mousePressEvent(QGraphicsSceneMouseEvent *e);
void mouseMoveEvent(QGraphicsSceneMouseEvent *e);
void mouseReleaseEvent(QGraphicsSceneMouseEvent *e);
private:
int x1, y1, x2, y2, w, h;
bool mFirstClick;
bool mSecondClick;
bool mPaintFlag;
QVector<QPointF> stuff;
};
#endif // POLYLINE_H
polyline.cpp
#include "polyline.h"
#include "QDebug"
polyline::polyline()
{
mFirstClick = true;
mSecondClick = false;
mPaintFlag = false;
}
QRectF polyline::boundingRect() const
{
// outer most edges
return QRectF(0,0,800,800);
}
void polyline::mousePressEvent(QGraphicsSceneMouseEvent* e){
if(e->button()==Qt::LeftButton) {
if(mFirstClick){
x1 = e->pos().x();
y1 = e->pos().y();
mFirstClick = false;
mSecondClick = true;
}
else if(!mFirstClick && mSecondClick){
x2 = e->pos().x();
y2 = e->pos().y();
mPaintFlag = true;
mSecondClick = false;
update();
}
}
QGraphicsItem::mousePressEvent(e);
update();
}
void polyline:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
QRectF rect = boundingRect();
if(mPaintFlag){
QPen paintpen(Qt::red);
paintpen.setWidth(4);
QPen linepen(Qt::black);
linepen.setWidth(1);
QPoint p1;
p1.setX(x1);
p1.setY(y1);
painter->setPen(paintpen);
painter->drawPoint(p1);
QPoint p2;
p2.setX(x2);
p2.setY(y2);
painter->setPen(paintpen);
painter->drawPoint(p2);
painter->setPen(linepen);
painter->drawLine(p1, p2);
}
}
void polyline::mouseMoveEvent(QGraphicsSceneMouseEvent *e)
{
m_pCurrentLine->setx2(e->x());
m_pCurrentLine->sety2(e->y());
m_pCurrentLine->update();
}
QGraphicsItem::mouseMoveEvent(e);
}
void polyline::mouseReleaseEvent(QGraphicsSceneMouseEvent *e)
{
QGraphicsItem::mouseReleaseEvent(e);
update();
}
创建从QGraphicsView派生的自己的类并重新实现mouseMoveEvent:
void OwnGraphicsView::mouseMoveEvent(QMouseEvent *event)
{
m_pCurrentLine->setX2(event->x());
m_pCurrentLine->setY2(event->y());
m_pCurrentLine->update();
}
使用GraphicsView为小部件设置鼠标跟踪:
setMouseTracking(true);
创建一个从QGraphicsItem派生的Item,例如一个类"line",并重新实现paint Method:
void line:: paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
因此每次调用mouseMoveEvent时,更新您的线条坐标并重新绘制它。
希望对大家有所帮助。
相关文章:
- 光线跟踪器灯光反射错误
- 人脸跟踪arduino代码的优化
- 跟踪滚动条上的鼠标事件
- 如何使用新运算符跟踪在循环中创建的 QLabel
- C++:跟踪类对象
- 用于解析 win64 堆栈跟踪的命令行客户端(可以访问符号服务器)
- 跟踪日志中的T.11803()是什么意思?
- 我能做些什么来跟踪矢量元素?
- 两个进程可以通过跟踪附加到同一个 PID 吗?
- 提升堆栈跟踪不显示函数名称和行号
- cygwin_exception::open_stackdumpfile:将堆栈跟踪转储到 class4.exe.sta
- 如何按指针查看堆栈跟踪
- WinDbg 不显示某些小型转储文件的完整堆栈跟踪
- C++ MFC 跟踪事件调用
- 是否可以/希望创建不可复制的共享指针模拟(以启用weak_ptr跟踪/借用类型语义)?
- 使用 Dijkstra 算法跟踪两个节点之间的最短路径
- 发布代码的 gdb 堆栈跟踪可读性如何影响 x64?
- MATLAB:跟踪imufilter对象中的状态变化
- mingw32-make 使用"MinGW Makefiles"生成器跟踪 CMAKE 无法将可执行文件链接到对象库
- 在Qt图形视图中跟踪折线