qt graphicsscene line 子类
Posted
技术标签:
【中文标题】qt graphicsscene line 子类【英文标题】:qt graphicsscene line subclass 【发布时间】:2017-04-17 16:57:41 【问题描述】:我从 GraphicsScene 类中添加了一个 Line 子类来绘制一条线。从 MainWindow 我调用该类中的 line 函数,没有错误,但没有绘制线。我知道这一定是缺乏我的 C++ 技能。但是搜索并没有帮助我解决这个问题。我想要的是制作不同的类来绘制不同的形状,而不是用所有代码污染 GraphicsScene,以保持事物有点结构化。但是我做错了什么?我在 github github.com/JackBerkhout/QT_Draw001 上发布了我的代码
line.h
#ifndef LINE_H
#define LINE_H
#include <QMainWindow>
#include <QObject>
#include <QDebug>
#include "graphicsscene.h"
class Line: public GraphicsScene
public:
Line();
void drawLine(int x1, int y1, int x2, int y2);
;
#endif // LINE_H
line.cpp
#include "line.h"
Line::Line():
GraphicsScene()
void Line::drawLine(int x1, int y1, int x2, int y2)
// Just draw something by clicking a button
qDebug() << "line"; // This debug message is shown
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
this->addLine(x1, y1, x2, y2, pen); // Didn't draw the line on the scene
图形场景.cpp
#ifndef GRAPHICSSCENE_H
#define GRAPHICSSCENE_H
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QPointF>
#include <QList>
class GraphicsScene : public QGraphicsScene
Q_OBJECT
public:
explicit GraphicsScene(QObject *parent = 0);
virtual void mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent);
virtual void mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent);
QPointF getMousePoint(void);
int getMouseX(void);
int getMouseY(void);
int getNumber(void);
void setNumber(int num);
QPointF mousePoint;
int MouseX, MouseY;
int myNumber;
signals:
void changedMousePosition(QPointF mousePoint);
void changedNumber(int myNumber);
public slots:
private:
QList <QPointF> mousePoints;
// int Number;
;
#endif // GRAPHICSSCENE_H
图形场景.cpp
#include "mainwindow.h"
#include "graphicsscene.h"
#include <QDebug>
GraphicsScene::GraphicsScene(QObject *parent) :
QGraphicsScene(parent)
this->setBackgroundBrush(Qt::black);
myNumber = 0;
// this-> ->setMouseTracking(true);
void GraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent)
// mousePoint = mouseEvent->scenePos();
// MouseX = mousePoint.x();
// MouseY = mousePoint.y();
qDebug() << Q_FUNC_INFO << mouseEvent->scenePos();
QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
emit changedMousePosition(mousePoint);
QGraphicsScene::mouseMoveEvent(mouseEvent);
void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
mousePoints.append(mouseEvent->scenePos());
MainWindow *mainWindow = new MainWindow();
mainWindow->Count++;
if(mousePoints.size() == 2)
myNumber++;
emit changedNumber(myNumber);
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
this->addLine(mousePoints.at(0).x(), mousePoints.at(0).y(), mousePoints.at(1).x(), mousePoints.at(1).y(), pen);
mousePoints.clear();
QGraphicsScene::mousePressEvent(mouseEvent);
void GraphicsScene::mouseReleaseEvent(QGraphicsSceneMouseEvent * mouseEvent)
mousePoint = mouseEvent->scenePos();
MouseX = mouseEvent->scenePos().x();
MouseY = mouseEvent->scenePos().y();
QGraphicsScene::mouseReleaseEvent(mouseEvent);
QPointF GraphicsScene::getMousePoint(void)
return mousePoint;
int GraphicsScene::getMouseX(void)
MouseX = mousePoint.x();
return mousePoint.x();
int GraphicsScene::getMouseY(void)
MouseY = mousePoint.y();
return mousePoint.y();
void GraphicsScene::setNumber(int num)
myNumber = num;
int GraphicsScene::getNumber(void)
return myNumber;
【问题讨论】:
当我想到它时,对不同的形状进行子类化可能没有意义。它使事情变得复杂。 【参考方案1】:你太复杂了,除了我认为你不明白继承的目的是什么,你只需要在GraphicsScene
中创建一个名为drawLine
的函数,并在需要时使用它。
GraphicsScene.h
public:
void drawLine(int x1, int y1, int x2, int y2);
GraphicsScene.cpp
void GraphicsScene::drawLine(int x1, int y1, int x2, int y2)
QColor color;
color.setRgb(128, 0, 255);
QPen pen;
pen.setColor(color);
pen.setWidth(20);
pen.setCapStyle(Qt::RoundCap);
addLine(x1, y1, x2, y2, pen);
然后你在主窗口中调用:
void MainWindow::on_toolButtonDraw_clicked()
scene->drawLine(300, 100, 500, 300);
当您创建 Line 对象时,您正在创建一个新场景,它将在该场景中绘制,因此您将看不到它。
【讨论】:
如果我想画橡皮筋线,我想我需要使用XOR,我找不到方法,我认为GraphicsScene 没有使用QPainter。有没有办法做到这一点?或者我可以制作一个单独的透明场景图层来绘制和擦除,而不破坏显示的内容吗? 我尝试将 QPainter 与 GraphicsScene 一起使用,但无法编译。错误:没有匹配函数调用‘QPainter::QPainter(GraphicsScene*)’ QPainterpainter(this); ^ void GraphicsScene::myLine(int x1, int y1, int x2, int y2) QPoint point1, point2; point1.setX(x1); point1.setY(y1); point2.setX(x2); point2.setY(y2); QPainter 画家(本); Painter.setCompositionMode(QPainter::RasterOp_SourceXorDestination);画家.drawLine(point1,point2); 什么是橡皮筋线? 我更新了我的 github 链接,现在包含它。当我开始画一条线时,从原点到鼠标位置绘制一条细虚线,并在单击第二个点之前随鼠标移动。当我用黑色绘制它们时,它会擦除已经绘制的线条。一些层或 XOR 操作在这里会派上用场,但我还不知道该怎么做。 github.com/JackBerkhout/QT_Draw001 在 line.cpp, setLineP2(int x, int y) 我将为这个问题开始一个新问题,因为它在这里是题外话。到目前为止非常感谢!以上是关于qt graphicsscene line 子类的主要内容,如果未能解决你的问题,请参考以下文章
GraphicsScene 中的 Qt GLWidget 用于从图像数据(C++、Windows)中快速显示视频?
PyQt 将 graphicsScene 对象分组为一个对象