PDA

View Full Version : Widget that receives the mouseReleaseEvent



qtUser500
14th July 2009, 21:45
Excuse the trivial nature of the question :( , but I wanted to know how to get the widget which received the mouse click event.

- I have a tabWidget with a few tabs, and when moving from one tab to another, I want to save changes before going to the next tab.
- Example: I am in Tab1 where I an editing a textField, when the user clicks on another tab, before going to this tab, I want to run a function to save the contents.

I am implementing the click event as follows:




void MyClass::mouseReleaseEvent ( QMouseEvent * event )
{
if (event->buttons() == Qt::LeftButton)
{
// Here I want to get the widget on which the mouse has been clicked
}

}


Thanks in advance.

wysota
14th July 2009, 21:52
I would react to hideEvent() instead of this.

qtUser500
14th July 2009, 22:37
Thanks wysota for the suggestion.

Following is what I tried

My dialog consists of a TabWidget (myTabWid)
myTabWid has 5 tab's : Tab1, Tab2...Tab5

1. Added a connect only for Tab2, as I am concerned only with this tab


connect (Tab2, SIGNAL(hideEvent()), this, SLOT(verifyTab2());


2. The SLOT function was added in the header file
3. In verifyTab2, I execute my required code.

But I notice this 'verifyTab2()' is not called!! Am I missing something here?
Kindly advise. Thanks.

wysota
14th July 2009, 22:49
hideEvent is not a signal but an event, see QWidget::hideEvent(). Reimplement it for every widget you place into tab widget that you want monitored for being hidden.

qtUser500
14th July 2009, 23:15
Thanks once again Wyosta.

1. How do I implement the HideEvent (or any event for that matter) for an individual widget?
2. My class contains the dialog which inturn contains the tabs and all the individual widgets.
3. I tried reimplementing it for my class, but it was called only when I closed the dialog, not when I switched from one tab to another



void MyClass::hideEvent(QHideEvent * event )
{
if (Tab2 == myTabWid->currentWidget() )
verifyTab2();

}

wysota
14th July 2009, 23:30
As I said you need to implement it for every widget serving as content for a page of your tab widget. Alternatively you can install an event filter (see QObejct::installEventFilter()).

qtUser500
15th July 2009, 16:35
I tried associating the 'installEventFilter' to the concerned widget.



bool MyClass::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::Hide)
{
if (obj == Tab2)
verifyTab2();

}
}


But when the event is called, the state of the tabWidget is 'gray', i.e. it hasn't loaded the new tab as yet, and the current tab contents have been removed.
I want to trap the event when it is still in the current tab (Where I want to display a message 'Do you want to save before moving to next tab').

What do you suggest?

nish
15th July 2009, 16:40
if (event->type() == QEvent::Hide())
remove the brackets

qtUser500
15th July 2009, 16:54
Thanks Mr. Death, I made that correction, but notice the behavior mentioned in my previous comment.

wysota
15th July 2009, 21:11
This won't work then. You have to react on mouse release, use QTabBar::tabAt() to find which tab was clicked and if it is different than the current one use QTabWidget::currentWidget() to get the current tab and do your stuff.

qtUser500
16th July 2009, 16:33
Thanks Wyosta.