qt graphicsscene line subclass



我添加了一个从图形学范围类中添加了一行子类,以绘制一条线。从MainWindow中,我称之为该类的行函数,没有错误,但是该行未绘制。我知道这一定缺乏我的C 技能。但是搜索对我没有帮助。我想要的是在绘制不同形状的不同类别中,而不是用所有代码污染图形学,以保持构造一点。但是我在做什么错?我在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;
}

您太复杂了,除了我认为您不了解继承的目的是什么,您只需要在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);
}

创建线路对象时,您正在创建一个新场景,并且将在该场景中绘制,因此您不会看到它。

相关内容

  • 没有找到相关文章

最新更新