【问题标题】:Make QGraphicsItem movable by right mouse button instead of left通过鼠标右键而不是左键使 QGraphicsItem 可移动
【发布时间】:2016-07-27 21:04:08
【问题描述】:

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

通过搜索,我唯一能找到的是一个线程 which suggests using QGraphicsItem::setAcceptedMouseButtons(),我尝试将其设置为只接受右键单击,但随后项目完全无法移动。

这是我试过的代码:

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);

【问题讨论】:

  • 不幸的是,QGraphicsItem::mouseMoveEvent 中的代码似乎硬连线只能在左侧按钮上移动——无论传递给setAcceptedMouseButtons 的参数如何。所以如果你想在右边的按钮上移动,我认为你必须覆盖 mouseMoveEvent 并自己做腿部工作。
  • 谢谢,这也是我怀疑的,虽然我不确定。我可能会尝试过滤事件并替换鼠标按钮以欺骗左键单击的对象,同时过滤掉真正的左键单击。

标签: c++ qt qgraphicsitem


【解决方案1】:

我最终以@G.M. 的身份查看了源代码。指出,QGraphicsItem::mouseMoveEvent() 的代码显式检查鼠标左键:

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

所以除了在派生类中重新实现移动之外什么都做不了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多