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

后端 未结 3 1864
忘掉有多难
忘掉有多难 2021-01-26 01:21

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 QM

相关标签:
3条回答
  • 2021-01-26 01:51

    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 ?

    0 讨论(0)
  • 2021-01-26 01:53

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

    if(QApplication::mouseButtons() & Qt:LeftButton) {
    ...
    }
    
    0 讨论(0)
  • 2021-01-26 01:54

    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)              
     {              
        ......              
     }
    }
    
    0 讨论(0)
提交回复
热议问题