views:

77

answers:

3

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.nokia.com/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){.....}
A: 

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

if(QApplication::mouseButtons() & Qt:LeftButton) {
...
}
Stephen Chu
Claire Huang
Of course you can use == operator. It will limit to "buttons are" not "buttons contains".
Kamil Klimek
Could you please explain what wrong with my sample code? why it cannot tell the button correctly ?? No matter I click left or right button, the slot won't go into corresponding "if" statement.
Claire Huang
+3  A: 

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 ?

Kamil Klimek
Oh, Thanks a lot!! I understand why it's zero now. Could you please give me a very simple example of the usage of customize contextMenu that how to connect the mouse action to different signal ??There is a tricky way to connect "pressed" signal instead of "clicked" in my own code, but I'd like to learn a more formal way to handle this.
Claire Huang
just see contextMenuPolicy property of QWidget. set it to Qt::CustomContextMenu, then you will receive customContextMenuRequested signal. Simply connect to it
Kamil Klimek
Oh, and I'm not sure about itemClicked/itemPressed, but I gues this signal is emited only for Qt::LeftButton
Kamil Klimek
Thanks so much. I've set it to CustomContextMenu. I can catch the "contextMenuRequested" signal correctly. However, if I click the right button,the program emits both "clicked" and "contextMenuRequested", and I don't know where the "clicked" signal comes from. That's why I need to separate the left button action and right button action for "clicked" signal.
Claire Huang
Did you try to use itemPressed ? It should have proper mouseButtons() set
Kamil Klimek
Ya~ now I use the press signal :)
Claire Huang
+1  A: 

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)              
 {              
    ......              
 }
}
Razi
It's great!! Thanks so much !!!!!
Claire Huang