[Qt-interest] Iaaue with mouseGrabber

Malyushytsky, Alex alex at wai.com
Tue Jul 26 01:56:44 CEST 2011


If you are new to programming language, it is especially worth to spend time on setting up debugger and learn to use it.
I can save you some time now, but believe me you it is a good idea to follow advice above anyway.

1. mouseGrabber() may and probably returns 0, which means wgt->objectName(); you are trying dereference invalid pointer (this why it crashes and my suggestion above is not )

2. Even if you are sure pointer can’t be 0 , it is worth to put some check (for example Q_CHECK_PTR( p ); ), otherwise make sure you handle the null case properly

3. mouseGrabber() is not what you think  it is. In this case you probably want to use sender()  or QSignalMapper. First is easier, second is safer.

4. >>Also I would like to cast “wgt” to QPushButton type.
Search documentation for qobject_cast, it seems the most appropriate in this case:
        QPushButton *button = qobject_cast< QPushButton *>(wgt); // remember about null pointers

Regards,
 Alex



From: qt-interest-bounces+alex=wai.com at qt.nokia.com [mailto:qt-interest-bounces+alex=wai.com at qt.nokia.com] On Behalf Of nandita-a.mehta at nokia.com
Sent: Monday, July 25, 2011 4:29 AM
To: qt-interest at qt.nokia.com
Subject: [Qt-interest] Iaaue with mouseGrabber

Hi
In my application i need to know which widget received the mouse click and for that I am using mouseGrabber().
However the application crashes.

To make it simple, in my Qt application I have a pushbutton; on its click a slot (hideWidget) is triggered.
In the slot, I am using mouseGrabber and then finding the widget name.
Can anyone explain why the app crashes?

My code is simple as follows:

// In ctor
connect (ui->pushButton, SIGNAL(clicked()), this, SLOT(hideWidget()));

// Slot
void MainWindow::hideWidget()
{
QWidget *wgt = QWidget::mouseGrabber();
QString str = wgt->objectName();
ui->label->setText(str);
}

Also I would like to cast “wgt” to QPushButton type.

Thanks.


Regards,
Nandita



---------------------------------------------------------------------------------------------------
Weidlinger Associates, Inc. made the following annotations.

“This message and any attachments are solely for the intended recipient and may contain confidential or privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you.”

“Please consider our environment before printing this email.”


More information about the Qt-interest-old mailing list