Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用鼠标右键而不是左键移动QGraphicsItem_C++_Qt_Qgraphicsitem - Fatal编程技术网

C++ 用鼠标右键而不是左键移动QGraphicsItem

C++ 用鼠标右键而不是左键移动QGraphicsItem,c++,qt,qgraphicsitem,C++,Qt,Qgraphicsitem,是否可以创建一个可移动的QGraphicsItem,用鼠标右键而不是左键移动 通过搜索,我只能找到一个线程,我尝试将其设置为只接受右键单击,但随后这些项目就完全停止了移动 这是我尝试的代码: QGraphicsScene *scene = new QGraphicsScene(this); ui->graphicsView->setScene(scene); QGraphicsRectItem *item = scene->addRect(0, 0, 100, 100); it

是否可以创建一个可移动的QGraphicsItem,用鼠标右键而不是左键移动

通过搜索,我只能找到一个线程,我尝试将其设置为只接受右键单击,但随后这些项目就完全停止了移动

这是我尝试的代码:

QGraphicsScene *scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QGraphicsRectItem *item = scene->addRect(0, 0, 100, 100);
item->setFlag(QGraphicsItem::ItemIsMovable);
item->setAcceptedMouseButtons(Qt::RightButton);

我最终查看了源代码,正如@G.M.所指出的,QGraphicsItem::mouseMoveEvent()的代码显式地检查鼠标左键:

void QGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    if ((event->buttons() & Qt::LeftButton) && (flags() & ItemIsMovable)) {

因此,除了在派生类中重新实现移动之外,什么也做不了。

不幸的是,
QGraphicsItem::mouseMoveEvent
中的代码似乎是硬连接的,只在左侧按钮上移动——不管传递给
setacceptedmousebutons
的参数是什么。因此,如果你想按右边的按钮,我想你必须覆盖
mouseMoveEvent
,自己做腿部运动。谢谢,这也是我所怀疑的,尽管我不确定。我可能会尝试过滤事件并替换鼠标按钮来愚弄对象--这是一次左键单击,而过滤掉真正的左键单击。