[Qt-interest] mouse buttons

Zaid Amir z.alamir at genie-soft.com
Tue Mar 23 08:07:27 CET 2010


Hi

To monitor mouse events you could use the mousePressEvent, mouseReleaseEvent
and mouseMoveEvent which are virtual functions that, when implemented in
your class, immediately capture all the desired mouse events so that you can
do whatever you want with them. You can filter the event even more by
specifying different functionality for left, middle and right-click events.
You could do this by checking the type of the QMouseEvent as such:

mousePressEvent ( QMouseEvent * event )
{
	If(event->button() == Qt::MouseButton::LeftButton)
		//Do something
	Else if (event->button() == Qt::MouseButton::RightButton)
		//Do Something Different

}


And since QTreeWidget is actually a QObject you could install and event
filter for that item. Check the part for eventFilter in the documentation
for details and examples on how to use and install an event filter.

Regards

Zaid Amir

Date: Mon, 22 Mar 2010 18:30:30 +0100
From: "Erik Kruyt" <erik at forcheck.nl>
Subject: [Qt-interest] mouse buttons
To: <qt-interest at trolltech.com>
Message-ID: <009f01cac9e5$5edd5de0$1c9819a0$@nl>
Content-Type: text/plain; charset="iso-8859-1"

I want to react differently on a right, left and double mouse button click
in a QTreeWidget.

QTreeWidget only provides an itemClicked and an itemDoubleclicked signal.

How can I detect if the itemclicked signal was caused by the right or a left
mouse button?

 

The documentation of QTreeWidget does not give an answer.

The documentation of QAbstractItemView::pressed states that you can obtain
the state of the mouse buttons through QApplication::mouseButtons(). This is
a static function. I tried this but no buttons are reported to be pressed!

Example of the slot receiving the itemClicked signal:

 

void ProjectFrame::itemClicked(QTreeWidgetItem *item, int column)

{

    if (column == 0){

        itemSelected(item);

        Qt::MouseButtons buttons = QApplication::mouseButtons();

        if (buttons.testFlag(Qt::RightButton)){

            popUpMenu->exec(QCursor::pos());

        }

    }

}

 

As an alternative I tried to re?mplement mousePressEvent. Now I can
discriminate between right and left button. However, now I cannot
discriminate the double mouse click, because the double click starts the
mouseClickEvent instead of the mouseDoubleClickEvent! Moreover, now I have
to implement the item selection and deselections anew.

 

So the question is: how can I retrieve the mousebutton clicked in the
itemClicked signal?

 

Erik Kruyt






More information about the Qt-interest-old mailing list