如何在“单击”中使用 QApplication::mouseButtons() 来判断鼠标按钮投币口?

发布于 2024-09-06 16:35:11 字数 1109 浏览 12 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(3

只想待在家 2024-09-13 16:35:11

你得到 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 ?

公布 2024-09-13 16:35:11

对于“connect”,使用这个:

connect(moduleTree,SIGNAL(itemClicked(QTreeWidgetItem *,int )),this
        ,SLOT(SlotItemClicked(QTreeWidgetItem *, int)));

定义一个全局标志:

public:
Qt::MouseButton mouseClickedBtnFlag;

然后重新实现“mouseReleaseEvent”:

CGuiMainwindow::mouseReleaseEvent ( QMouseEvent * event )
{
mouseClickedBtnFlag = event->button();
}

然后:

void CGuiMainwindow::SlotItemClicked(QTreeWidgetItem *item, int column)
{    
 if (mouseClickedBtnFlag == Qt::LeftButton)              
 { return; }              

 if (mouseClickedBtnFlag == Qt::RightButton)              
 {              
    ......              
 }
}

for "connect" use this:

connect(moduleTree,SIGNAL(itemClicked(QTreeWidgetItem *,int )),this
        ,SLOT(SlotItemClicked(QTreeWidgetItem *, int)));

define a global flag:

public:
Qt::MouseButton mouseClickedBtnFlag;

and then reimplement "mouseReleaseEvent":

CGuiMainwindow::mouseReleaseEvent ( QMouseEvent * event )
{
mouseClickedBtnFlag = event->button();
}

and then:

void CGuiMainwindow::SlotItemClicked(QTreeWidgetItem *item, int column)
{    
 if (mouseClickedBtnFlag == Qt::LeftButton)              
 { return; }              

 if (mouseClickedBtnFlag == Qt::RightButton)              
 {              
    ......              
 }
}
以往的大感动 2024-09-13 16:35:11

Qt::MouseButtonsQFlags 类型。您无法使用 == 运算符对其进行测试。使用 & 运算符进行测试:

if(QApplication::mouseButtons() & Qt:LeftButton) {
...
}

Qt::MouseButtons is a QFlags type. You can't test it with == operator. Use & operator for testing:

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