【问题标题】:QMouseEvents in Graphics Scene not working图形场景中的 QMouseEvents 不起作用
【发布时间】:2016-03-11 05:33:54
【问题描述】:

我是 Qt 的新手,我创建了一个简单的应用程序,它在 custom QGraphicsScene 中初始化了许多 custom QGraphicsItems。每个项目都使用随机起始位置和取决于项目位置的权重值进行初始化。在鼠标移动事件中,我希望根据鼠标光标的位置更新项目的权重值

我认为我的 mouseMoveEventgraphicsScene 中无法识别,它似乎在我在状态栏中实现标签以显示mouseMoveEvents 的数量和 mouseMoveEvent

的 XY 位置

代码如下:

自定义图形场景.h:

class ParticleScene : public QGraphicsScene
{
public:
    ParticleScene();

protected: 
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

private:
    qreal WTotal;
    Particle *particle;

} 

自定义图形场景.cpp:

ParticleScene::ParticleScene()
{

//this->setBackgroundBrush(Qt::gray);
this->setSceneRect(0,0,500,500);

WTotal=0;
int ParticleCount =5;
for (int i =0; i<ParticleCount; i++)
    {
    particle= new Particle();
    particle->StartX= rand()%500;
    particle->StartY= rand()%500;
    particle->W= qSqrt(qPow(particle->StartX,2) + qPow(particle->StartY,2));
    particle->setPos(particle->StartX,particle->StartY);
    this->addItem(particle);
    particle->setFocus();
    WTotal+=particle->W;
    }

}

void ParticleScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    update();
    QGraphicsScene::mouseMoveEvent(event);
}

Particle.h:

我添加了 Keypress 事件 函数,它只移动了添加到场景中的最后一个项目,我假设只有一个项目可以获得焦点。 另一方面,mouseMove 事件 没有做任何事情

class Particle :public QGraphicsItem
{
public:
    Particle();

    QRectF boundingRect() const;
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
    int StartX;
    int StartY;
    qreal W;

protected:
    //added keyPressEvent to test
    virtual void keyPressEvent(QKeyEvent *event);
    virtual void mouseMoveEvent(QGraphicsSceneMouseEvent *event);

};

粒子.cpp:

Particle::Particle()
{
//    setFlag(ItemIsMovable);
    setFlag(ItemIsFocusable);
}

QRectF Particle::boundingRect() const
{
     return QRect(0,0,120,30);
}

void Particle::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    QRectF rec= boundingRect();
    QBrush Brush(Qt::white);

    painter->fillRect(rec,Brush);
    painter->drawText(15,15,"Weight: "+QString::number(W));
    painter->drawRect(rec);

}

void Particle::keyPressEvent(QKeyEvent *event)
{

    switch(event->key()){
    case Qt::Key_Right:{
        moveBy(30,0);
        break;}
    case Qt::Key_Left:{
        moveBy(-30,0);
        break;}
    case Qt::Key_Up:{
        moveBy(0,-30);
        break;}
    case Qt::Key_Down:{
        moveBy(0,30);
        break;}
    }
    update();
}

void Particle::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    this->W= this->W / qSqrt(qPow(event->pos().x(),2) + qPow(event->pos().y(),2));
    moveBy(30,0);
    update();
}

MainWindow .h 和 cpp: 此处的状态栏标签正确显示鼠标坐标,即此处的 mouseMoveEvent 函数

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
   Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void mouseMoveEvent(QMouseEvent *event);
protected:

private:
    Ui::MainWindow *ui;
    ParticleScene *scene;
    QLabel *statlabel;

    int moves;
};

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

    statlabel=new QLabel(this);
    ui->statusBar->addWidget(statlabel);
    statlabel->setText ("Mouse Coordinates");

    setCentralWidget(ui->graphicsView);
    centralWidget()->setAttribute(Qt::WA_TransparentForMouseEvents);
    ui->graphicsView->setMouseTracking(true);

    scene= new ParticleScene();
    ui->graphicsView->setScene(scene);
    ui->graphicsView->setRenderHint(QPainter::Antialiasing);

    moves=0;

}

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

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    moves+=1;
    statlabel->setText("MouseMoves " +QString::number(moves)+ " X:"+QString::number(event->pos().x())+"-- Y:"+QString::number(event->pos().y()));
}

我在程序中缺少什么导致 mousemoveevent 无法运行,有没有办法将所有项目集中在一起?我是否需要将它们变成 QList

在程序的下一步中,我希望项目根据所有权重的总和更新其权重值,并根据使用新权重值确定新位置的算法移动。

【问题讨论】:

  • 移除函数ParticleScene::mouseMoveEvent。您无需在此处调用 update ,除非该函数中有其他代码,否则它不会为您做任何事情。当你在场景中移动一个项目时,只要它的边界矩形是正确的,它就会为你更新。您可以一次选择多个项目,但 QGraphicsItemGroup 可能就是您要查找的内容。

标签: c++ qt mousemove qgraphicsitem qgraphicsscene


【解决方案1】:

您缺少某种可以保存所有粒子的容器。在当前实现中,ParticleScene 将粒子指针作为私有变量。您正在循环中创建多个 Particles(它们是 QGraphicsItem 的),但只有最后一个指针存储在您的自定义场景中。正如您所说,您可以使用例如 QList 来保存您的粒子。

另外我假设你想在场景中移动你的物品。为此,您还可以自己实现 MousePress 事件(捕捉何时按下项目以及在什么坐标处),MouseMove(用于实际移动项目..you已经这样做了,并且 mouseRelease(例如,用于计算权重或发送将触发计算所有项目权重的信号)

与其将所有项目保存在自定义场景中,不如创建一个新类,例如,将存储所有项目的 ItemsManager 和指向场景的指针。在这个类中,您可以执行有关计算项目权重或其他所需操作的所有操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2012-04-13
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多