我从GraphicsScene类中添加了Line子类,以绘制一条线。在MainWindow中,我在该类中调用line函数,没有错误,但未绘制线。我知道这一定是我的C++技能不足。但是搜索并没有帮助我。我想要的是制作不同的类来绘制不同的形状,而不是用所有这些代码污染GraphicsScene,以使事情保持结构化。但是我在做什么错呢?我将代码发布在github github.com/JackBerkhout/QT_Draw001上


#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;
}

最佳答案

除了让我感到复杂之外,我认为您不了解继承的目的,您只需要在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对象时,您正在创建一个新场景,该场景将在该场景中绘制,因此您将看不到它。

07-28 04:44