如果传递到QGraphicsRectItem Qt c++,请更改光标
Change the cursor if you pass to a QGraphicsRectItem Qt c++
如果经过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);
}
在这段代码中,如果我点击一次,光标就会发生变化。但是,如果我直接传递,请不要更改。为什么?
您不必实现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();
}
相关文章:
- 如何在Qt C++中更改光标
- Win32 API 控制台光标在 WriteConsole 后不移动
- 在 macOS 中使用 C/C++ 设置鼠标光标图像
- 如何更改输出的位置或光标在输出处,以便在"Code Blocks"中C++
- 使用 C++将文本光标移动到屏幕中不可见的行
- 在 C# 中更改鼠标光标图标
- ASCII 旋转光标 (TUI) 动画出现问题
- 将鼠标悬停在图片框上时,如何显示带有 x-y 坐标的十字准线光标?
- 将光标单独移动到当前位置
- 如何获得鼠标光标连接时按下左键单击和在C++中释放左键单击时?
- 由于选项卡顺序的原因,鼠标光标总是得到错误的hwnd-MFC应用程序
- 如何在C++中模拟鼠标光标的移动
- 如何确定鼠标光标所在的控制台字符
- 如何在C++中使用光标定位
- 如何在QML中设置鼠标光标位置
- 如果传递到QGraphicsRectItem Qt c++,请更改光标
- 从 QML 更好地控制文本光标
- Windows 函数"GetCursorInfo"返回成功,但光标的句柄为 NULL
- 避免在光标移到子窗口上时WM_MOUSELEAVE
- 我试图将光标平滑地移动到随机点