如何通知将鼠标坐标更改为父对象

如何通知将鼠标坐标更改为父对象

本文介绍了Qt:如何通知将鼠标坐标更改为父对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Qt类QGraphicsScene有点问题:为了检测当前的鼠标坐标,我创建了一个新类QGraphicsScenePlus,以QGraphicsScene作为基类.我已经重新定义了插槽功能mouseMoveEvent(QGraphicsSceneMouseEvent* event),并且收到的坐标似乎正确.现在,每当鼠标坐标更改时,我都想通知父类QMainWindow类,该类存储了QGraphicsScenePlus对象.做这个的最好方式是什么?我已经尝试定义信号和插槽,但是没有用.在执行程序期间找不到slot函数.

I have a little problem with the Qt class QGraphicsScene:To detect the current mouse coordinates I made a new class QGraphicsScenePlus with QGraphicsScene as the base class. I have already redefined the slot function mouseMoveEvent(QGraphicsSceneMouseEvent* event) and the received coordinates seem to be correct. Now I want to notify the parent QMainWindow class, where the QGraphicsScenePlus object is stored, whenever the mouse coordinates change. What is the best way to do this? I already tried to define signals and slots, but it didn't work. The slot function wasn't found during the execution of the program.

这是到目前为止的代码:

Here is the code so far:

qgraphicssceneplus.h

#ifndef QGRAPHICSSCENEPLUS_H
#define QGRAPHICSSCENEPLUS_H

#include <QObject>
#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>

class QGraphicsScenePlus : public QGraphicsScene {

public:
    QGraphicsScenePlus(QObject* parent = 0);

public slots:
    void mouseMoveEvent(QGraphicsSceneMouseEvent* event);

public:
    int mx = 0;
    int my = 0;
};

#endif // QGRAPHICSSCENEPLUS_H

qgraphicssceneplus.cpp

#include "qgraphicssceneplus.h"

QGraphicsScenePlus::QGraphicsScenePlus(QObject* parent) : QGraphicsScene(parent) {

}


void QGraphicsScenePlus::mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent) {

    mx = mouseEvent->scenePos().x();
    my = mouseEvent->scenePos().y();

    this->update();
}

推荐答案

评论

我不确定您是如何编译以上代码的.

Comment

I am not sure how you made the above code compiled.

1..即使您是QObject的子类,您仍然需要Q_OBJECT宏来使元对象编译器保持最新状态:

1. Even though you subclass from a QObject, you still need the Q_OBJECT macro to keep meta-object compiler informed:

class QGraphicsScenePlus : public QGraphicsScene {

    Q_OBJECT    // <--- You miss this

public:
    QGraphicsScenePlus(QObject* parent = 0);

2..不允许在C ++类定义中分配原始值,而应在构造函数中进行赋值:

2. It's not allowed to assign primitive value in C++ class definition, do it in the constructor instead:

public:
    int mx /*= 0*/;
    int my /*= 0*/;
};


解决方案

关于您的问题:


Solution

As for your question:

最好的方法仍然是 Signal&插槽.

qgraphicssceneplus.h

class QGraphicsScenePlus : public QGraphicsScene {

    Q_OBJECT

public:
    QGraphicsScenePlus(QObject* parent = 0);

public slots:
    void mouseMoveEvent(QGraphicsSceneMouseEvent* event);

signals:
    void sendCoord(int,int);  // for sending the information of coordinates

public:
    int mx;
    int my;
};

qgraphicssceneplus.cpp

QGraphicsScenePlus::QGraphicsScenePlus(QObject* parent) : QGraphicsScene(parent) {

    mx = 0;
    my = 0;
}

void QGraphicsScenePlus::mouseMoveEvent(QGraphicsSceneMouseEvent* mouseEvent) {

    mx = mouseEvent->scenePos().x();
    my = mouseEvent->scenePos().y();

    emit sendCoord(mx, my);  // emit the signal

    this->update();
}

要捕获信号,请在QMainWindow中定义插槽.例如:

To catch the signal, define the slot in QMainWindow. For example:

public slots:
    void receiveCoord(int x, int y);

并将其连接到图形场景的信号.

and connect it to the signal of your graphic scene.

这篇关于Qt:如何通知将鼠标坐标更改为父对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 06:55