[Interest] type casting 5 levels down no error but not proper

BERAUD Alexandre aberaud at infflux.com
Tue Apr 24 15:42:07 CEST 2012


The reason is that the scrollarea is NOT the parent of the sheet. You 
must check every single pointer that you get from a cast. If you use a 
debugger you can dig a bit and discover whether your sheet's parent is 
null or some other class, but it is certainly not a QScrollArea. Anyway, 
don't use cascading casts like this, there are a lot of other methods, 
even ugly ones, but not as ugly (and dangerous) as this. Store your 
mainwin in a global variable, or pass it as an argument to the class 
contructor.

Regards.

Alex


Le 24/04/2012 15:27, Sujan Dasmahapatra a écrit :
> [code]
> QWidget *wid  = qobject_cast<QWidget *>(parent());
> CSheet *sheet = qobject _cast<CSheet *>(wid->parent());
> QScrollArea *scrollArea = qobject _cast<QScrollArea *>(sheet->parent());
> CTabWidget *tabWid = qobject _cast<CTabWidget *>(scrollArea->parent());//Here it is crashing, QSSERT(d)  failure
> CDataPage  *dataPage = qobject _cast<CDataPage *>(tabWid->parent());
> MainWindow *mainwin = qobject _cast<MainWindow *>(dataPage->parent());
> mainwin->statusBar()->showMessage(tr("A new plot being added!!"),5000);
> [/code]
>
> Dear Konstantin
>
> It's not returning anything, it's crashing. When casting to get CTabWidget. For the sheet's parent is QScrollBar so I am getting that by scrollArea and scrollArea's parent is CTabWidget.....which is getting failure. What could be the possible reason, please help.
>
> Kind regards,
>
>
> Sujan Dasmahapatra
> Project Leader, Aero Group
> CE - Aero Group
>
> Tel  +91 80 66470248
> Mob 
> sdh at lmwindpower.com
>
>
> LM Wind Power Blades
> lmwindpower.com
>
> Together we capture the wind to power a cleaner world
>
>
> This e-mail and any attachments are confidential. If you are not the named or intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Any unauthorized disclosure, use or storage is prohibited and might be unlawful.
> -----Original Message-----
> From: Konstantin Tokarev [mailto:annulen at yandex.ru]
> Sent: Tuesday, April 24, 2012 6:17 PM
> To: Sujan Dasmahapatra
> Cc: interest at qt-project.org
> Subject: Re: [Interest] type casting 5 levels down no error but not proper
>
>
> 24.04.2012, 16:42, "Sujan Dasmahapatra"<sdh at lmwindpower.com>:
>> Thanks Diego and Konstantin for your valuable inputs. I am now doing qobject_cast for all of them, please check the code snippet below, now it’s crashing at tabWid step. It’s giving QASSERT failure. The failure is happening in the following file
>>
>> //qscopedpointer.h at the following line QASSERT(d)
>>
>>    inline T *operator->() const
>>
>>      {
>>
>>          Q_ASSERT(d); //this line it’s crashing.
>>
>>          return d;
>>
>>      }
> It means that tabWid is QScopedPointer with 0 value. You need to check if qobject_cast doesn't return 0 before using pointer.
>


-- 
Alexandre BERAUD
Ingénieur Développement
INFFLUX
01.49.57.92.00




More information about the Interest mailing list