QApplication::mouseButtons 的线程安全和延迟安全如何?

发布于 2024-12-16 18:12:01 字数 411 浏览 4 评论 0原文

当您从模型获取鼠标信号到插槽中时,传递的参数是 QModelIndex。

QModelIndex 不会告诉您按下了什么按钮。因此,我们可以求助于 QApplication::mouseButtons。但 QApplication::mouseButtons 是当前按钮按下的时间,而不是模型经历点击时的时间。

我的思想实验表明,按下右键后,底层视图将信号发送到我的小部件,但就在我的小部件的插槽收到信号之前,发生了虚假的左键单击。因此,在收到 QModelIndex 时调用 QApplication::mouseButtons 会错误地将单击的行与鼠标左键而不是右键关联起来。这种情况怎么可能发生?

当您查看 Qt 甚至 QML 时,需要大量代码杂技才能在收到 QModelIndex 时获得正确的鼠标按钮信息。这是诺基亚极力宣扬鼠标按钮不可知论的政策吗?

When you get get a mouse signal from a model into your slot, the argument passed is a QModelIndex.

QModelIndex does not tell you what button is pressed. So, we can resort to QApplication::mouseButtons. But QApplication::mouseButtons is the current button press, not when model experienced the click.

My thought experiment is saying that, after the right-button is pressed, the underlying view sends the signal to my widget, but just before my widget's slot received the signal, a spurious left-click occurred. So calling QApplication::mouseButtons on receipt of QModelIndex would wrongly associate the row being click with the left mouse button rather than the right button. How possible is this scenario?

When you look at Qt and even QML, it takes a lot of code acrobatics to achieve proper mouse button info on receipt of a QModelIndex. Is it a policy that nokia is striving to promote mouse button agnosticism?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2024-12-23 18:12:01

continue

I don't think this is a very possible scenario but it may happen.

A "simple" way to be sure about which button was clicked is to subclass QTableView (or the view you are using and reimplement the mouseReleaseEvent.

void mouseReleaseEvent(QMouseEvent * event)
{
    // store the button that was clicked
    mButton = event->button();
    // Now call the parent's event
    QTableView::mouseReleaseEvent(event);
}

By default the mouseReleaseEvent emits the clicked signal if an item of the view is pressed

If a user presses the mouse inside your widget and then drags the
mouse to another location before releasing the mouse button, your
widget receives the release event. The function will emit the
clicked() signal if an item was being pressed.

The trick is to catch the clicked signal in your derived class and emit a new signal which except the model index will contain the button as well.

// Define your new signal in the header
signals:
    void clicked(QModelIndex, Qt::MouseButton);

// and a slot that will emit it
private slots:
    void clickedSlot(QModelIndex); 

// In the constructor of your derived class connect the default clicked with a slot 
connect(this, SIGNAL(clicked(QModelIndex), this, SLOT(clickedSlot(QModelIndex)));

// Now the slot just emits the new clicked signal with the button that was pressed
void clickedSlot(QModelIndex i)
{
    emit clicked(i, mButton);
}

You can do something similar with the mousePressEvent if you need the pressed signal as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文