如何在“单击”中使用 QApplication::mouseButtons() 来判断鼠标按钮投币口?
我有一个 QMainWindow,并且想要处理来自其中较小的小部件(例如 tableview)的“单击”信号。
最初我将信号连接到这个 QMainWindow 的插槽,这是最常见的方法。 现在我需要判断单击了哪个鼠标按钮,并对左右按钮执行不同的操作,我发现“单击”信号没有鼠标事件信息。
我尝试实现“mousePressEvent”功能,但仍然存在一些问题。如果鼠标操作作用在较小的小部件上,则 MainWindow 将不会进入其 mousePressEvent。
有文档说我们可以通过 QQApplication::mousebuttons()
http:// bugreports.qt-project.org/browse/QTBUG-1067
我还找到了一些示例代码。但是,这是针对“按下事件”的,但我想获取“单击事件”的鼠标按钮。 以下是示例代码:
connect(moduleTree,SIGNAL(itemPressed(QTreeWidgetItem *, int)),this,SLOT(SlotItemClicked(QTreeWidgetItem *, int)));
void CGuiMainwindow::SlotItemClicked(QTreeWidgetItem *item, int column)
{
if (qApp->mouseButtons() == Qt::LeftButton)
{ return; }
if (qApp->mouseButtons() == Qt::RightButton)
{
......
}
}
当我尝试这样做时,两个 if 语句都不会得到满足,我不知道为什么。 qApp->mouseButtons() 总是返回 0,如何通过 QApplication::mouseButtons 获取单击的鼠标按钮?
在我的代码中,插槽如下所示:
void clickItem( const QModelIndex & idx){.....}
I have a QMainWindow, and want to handle the "clicked" signal from a smaller widget (such as tableview) inside it.
Originally I connect the signal to a slot of this QMainWindow, this is the most common approach.
Now I need to tell which mouse button is clicked, and do different things for left and right button, I found that the "clicked" signal don't have the mouse event information.
I tried to implement the "mousePressEvent" function,but there are still some problem. if the mouse action is acted on the smaller widget, the MainWindow won't go into its mousePressEvent.
Some document says that we can tell the button by QQApplication::mousebuttons()
http://bugreports.qt-project.org/browse/QTBUG-1067
and I also found some sample code. However, this is for "press event", but I want to get the mouse button for "click event".
Follows is the sample code :
connect(moduleTree,SIGNAL(itemPressed(QTreeWidgetItem *, int)),this,SLOT(SlotItemClicked(QTreeWidgetItem *, int)));
void CGuiMainwindow::SlotItemClicked(QTreeWidgetItem *item, int column)
{
if (qApp->mouseButtons() == Qt::LeftButton)
{ return; }
if (qApp->mouseButtons() == Qt::RightButton)
{
......
}
}
When I try to do this, neither of the 2 if statements will be satisfied, I don't know why. the qApp->mouseButtons() always return 0, how can I get the clicked mouse button by QApplication::mouseButtons?
In my code, the slot looks like that :
void clickItem( const QModelIndex & idx){.....}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你得到 0 因为 clicked 是在鼠标释放后发出的,而不是在鼠标按下时发出的。你想达到什么目的?也许尝试将小部件 contextMenuPolicy 设置为自定义,然后连接到信号 contextMenuRequested (用于右键单击)并单击用于左键单击?
You get 0 becouse clicked is emited after mouse release, not at mouse press. What do you want to achieve ? Maybe try settings on you widget contextMenuPolicy to custom, and than connect to signal contextMenuRequested (for the right click) and clicked for the left click ?
对于“connect”,使用这个:
定义一个全局标志:
然后重新实现“mouseReleaseEvent”:
然后:
for "connect" use this:
define a global flag:
and then reimplement "mouseReleaseEvent":
and then:
Qt::MouseButtons
是QFlags
类型。您无法使用==
运算符对其进行测试。使用&
运算符进行测试:Qt::MouseButtons
is aQFlags
type. You can't test it with==
operator. Use&
operator for testing: