【问题标题】:Change the cursor if you pass to a QGraphicsRectItem Qt c++如果传递给 QGraphicsRectItem Qt c++,则更改光标
【发布时间】:2018-07-13 09:55:02
【问题描述】:

如果我越过像QGraphicsRectItem 这样的矩形,我想更改光标。

我有一个继承自QGraphicsView 的类,矩形显示在QGraphicScene 中。

我用eventFilter 实现了鼠标事件。

问题是当我点击矩形时光标会改变,而我希望它在我传递它时改变。 我已经用QAbstractButton 更改了光标,但是QGraphicsRectItem::enterEvent(event) 不起作用。

这是我的代码QAbstractButton

void ToggleButton::enterEvent(QEvent *event) {
    setCursor(Qt::PointingHandCursor);
    QAbstractButton::enterEvent(event);
}

在这种情况下它可以工作。

这是我的代码来检测我是否传递了一个矩形:

DetecRect::DetecRect(QWidget* parent) : 
    QGraphicsView(parent)
{
     scene = new QGraphicsScene(this);
     pixmapItem=new QGraphicsPixmapItem(pixmap);
     scene->addItem(pixmapItem);
     this->setScene(scene);
     this->setMouseTracking(true);
     scene->installEventFilter(this);
}

bool DetecRect::eventFilter(QObject *watched, QEvent *event)
{
    if(watched == scene){
        // press event
        QGraphicsSceneMouseEvent *mouseSceneEvent;
        if(event->type() == QEvent::GraphicsSceneMousePress){
            mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
            if(mouseSceneEvent->button() & Qt::LeftButton){

            }
        // move event
        } else if (event->type() == QEvent::GraphicsSceneMouseMove) {
            mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
             //selectedItem is a QGraphicsItem
            if(this->selectedItem && this->selectedItem->type() == QGraphicsRectItem::Type){
                selectedItem->setCursor(Qt::PointingHandCursor);
            }
        }
        // release event
        else if (event->type() == QEvent::GraphicsSceneMouseRelease) {
            mouseSceneEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
        }
    }
    return QGraphicsView::eventFilter(watched, event);
}

在这段代码中,如果我单击一次,光标会发生变化。但是如果我直接通过就不要更改。为什么?

【问题讨论】:

  • 可能重复,见this answer
  • @lubgr,我尝试了这个答案,但我有一个错误无法在没有对象 QGraphicsItem::hoverEnterEvent(event) 的情况下调用成员函数 'virtual void QGraphicsItem::hoverEnterEvent(QGraphicsSceneHoverEvent*)';

标签: c++ qt qgraphicsview qgraphicsitem qcursor


【解决方案1】:

您不必实现 hoverEnterEvent 方法或 hoverLeaveEvent,您只需为项目设置一个光标,如下所示:

#include <QApplication>
#include <QGraphicsRectItem>
#include <QGraphicsView>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QGraphicsView view;
    QGraphicsScene *scene = new QGraphicsScene(&view);
    view.setScene(scene);

    QGraphicsRectItem *rit = scene->addRect(QRectF(-50, -50, 100, 100), QPen(Qt::black), QBrush(Qt::gray));
    rit->setCursor(Qt::CrossCursor);

    QGraphicsRectItem *rit2 = new QGraphicsRectItem(QRectF(-50, -50, 100, 100));
    rit2->setPen(QPen(Qt::white));
    rit2->setBrush(QBrush(Qt::green));
    rit2->setCursor(Qt::PointingHandCursor);
    rit2->setPos(200, 100);
    scene->addItem(rit2);

    view.resize(640, 480);
    view.show();
    return a.exec();
}

【讨论】:

  • 好的,谢谢你的回答我以为我必须实现 hoverEnterEvent 方法,但只需将你想要的光标放在 QGraphicsRectItem 上。
猜你喜欢
  • 1970-01-01
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 2020-09-16
  • 1970-01-01
  • 1970-01-01
  • 2015-10-17
相关资源
最近更新 更多