c++ - qt qgraphicsscene线子类

标签 c++ qt qt5 qgraphicsview qgraphicsscene

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

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

行.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;
}

最佳答案

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

关于c++ - qt qgraphicsscene线子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43455845/

相关文章:

c++ - Qt 的 QHeaderView::saveState() 和 QHeaderView::restoreState() 是如何工作的?

python - PyQt5 - 如何将关闭键(红色 x)连接到函数

c++ - 一次从输入流中读取一个单词到 char 数组?

c++ - 调用析构函数时从 0xfefefefe 读取错误

c++ - 为什么在 sqlite - QT here - 中触发查询失败?

c++ - 参数化离散曲线的正切

c++ - 如何在 Qt 运行时调整和移动组件(Label,PictureBox)?

qt5 - Qt 5.3 Qt Quick 应用程序显示白屏

c++ - 从单元测试用例运行线程是一个好习惯吗?

C++:在包含的头文件中使用#define 常量 (Arduino)