来自MainWindow的GraphicsScene中的qt访问功能

时间:2017-04-15 15:39:46

标签: qt function access

为了学习Qt和C ++,我玩了它并创建了一个非常基本的绘图应用程序。 我知道我需要使用GraphicsScene子类来处理鼠标事件,现在我可以画线。

但是如何从MainWindow(mainwindows.cpp)访问GraphicsScene(graphicsscene.cpp)中的函数? 为了尝试这个,我在GraphicsScene中创建了以下公共函数,以在MainWindow状态栏中显示鼠标坐标。

QPointF GraphicsScene::getMousePoint(void)
{
    return mousePoint;
}

如果需要,可以使用更多代码:

mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsView>
#include <QGraphicsScene>
#include "graphicsscene.h"
#include <QTimer>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    qApp->installEventFilter(this);

    Count = 0;
    // A way to close the app, clicking actionQuit, without using a seperate function
//    connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));

    int Width = 3200;
    int Height = 1600;
    this->resize(Width, Height);
    scene = new GraphicsScene();
    ui->graphicsView->setScene(scene);
    int sceneWidth = this->width()-20;
    int sceneHeight = this->height()-160;
    ui->graphicsView->resize(sceneWidth, sceneHeight);
    ui->graphicsView->setSceneRect(0, 0, sceneWidth, sceneHeight);
    ui->graphicsView->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
    ui->graphicsView->scale(1, -1); // Coordinate 0,0 at Bottom-Left

    statusBar()->showMessage(QString("Size: %1, %2 ").arg(Width).arg(Height));

    scene->setBackgroundBrush(Qt::black);
}

MainWindow::~MainWindow()
{
    delete ui;
}

graphicsscene.cpp

#include "graphicsscene.h"
#include <QDebug>

GraphicsScene::GraphicsScene(QObject *parent) :
    QGraphicsScene(parent)
{
    this->setBackgroundBrush(Qt::black);
}

void GraphicsScene::mouseDoubleClickEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
    qDebug() << Q_FUNC_INFO << mouseEvent->scenePos();
    QGraphicsScene::mouseDoubleClickEvent(mouseEvent);
}

void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
    mousePoint = mouseEvent->scenePos();
    QGraphicsScene::mouseMoveEvent(mouseEvent);
}

void GraphicsScene::mousePressEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
    mousePoint = mouseEvent->scenePos();

    mousePoints.append(mouseEvent->scenePos());

    if(mousePoints.size() == 2)
    {
        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();
    QGraphicsScene::mouseReleaseEvent(mouseEvent);
}

QPointF GraphicsScene::getMousePoint(void)
{
    return mousePoint;
}

graphicsscene.h

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

    virtual QPointF getMousePoint(void);

signals:

public slots:

private:
    QList <QPointF> mousePoints;

    QPointF mousePoint;

};

#endif // GRAPHICSSCENE_H

2 个答案:

答案 0 :(得分:0)

来自MainWindow课程,如果您对scene成员的场景有提及,那么您只需使用它:scene->getMousePoint()
当然,scene成员的类型必须是GraphicsScene*而不是QGraphicsScene*

答案 1 :(得分:0)

如果要使用此功能,则不应使用virtual修饰符。

你必须改变:

virtual QPointF getMousePoint(void);

QPointF getMousePoint(void);

如果您想在移动鼠标时跟踪对象,我建议您创建一个信号,例如:

<强> graphicsscene.h

signals:
    void changedMousePosition(QPointF point);

<强> graphicsscene.cpp

void GraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent * mouseEvent)
{
    QPointF point = mouseEvent->scenePos();
    emit changedMousePosition(point);
    QGraphicsScene::mouseMoveEvent(mouseEvent);
}

<强> mainwindow.h

public slots:
    void onChangedMousePosition(QPointF point);

<强> mainwindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ...
    statusBar()->showMessage(QString("Size: %1, %2 ").arg(Width).arg(Height));

    scene->setBackgroundBrush(Qt::black);
    connect(scene, &GraphicsScene::changedMousePosition, this, &MainWindow::onChangedMousePosition);
}


void MainWindow::onChangedMousePosition(QPointF point)
{
    statusBar()->showMessage(QString("x: %1 y: %2").arg(point.x()).arg(point.y()));
}