qt graphicsscene line子类

时间:2017-04-17 16:57:41

标签: c++ qt qt5 qgraphicsview qgraphicsscene

我在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
}

graphicsscene.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

graphicsscene.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 个答案:

答案 0 :(得分: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);
}

然后你打电话给Mainwindow:

void MainWindow::on_toolButtonDraw_clicked()
{
    scene->drawLine(300, 100, 500, 300);
}

当您创建一个Line对象时,您正在创建一个新场景,它将在该场景中绘制,因此您将看不到它。