How to tell the mouse button using QApplication::mouseButtons() in a “click” slot?

心不动则不痛 提交于 2019-12-02 10:04:52

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 ?

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)              
 {              
    ......              
 }
}

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

if(QApplication::mouseButtons() & Qt:LeftButton) {
...
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!