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

Diego Iastrubni diegoiast at gmail.com
Tue Apr 24 14:10:37 CEST 2012


On Tue, Apr 24, 2012 at 2:58 PM, Sujan Dasmahapatra <sdh at lmwindpower.com>wrote:

>  please check the code snippet below:**
>
> **
>
> ** **
>
> [code]****
>
> QWidget *wid  = reinterpret_cast<QWidget *>(parent());****
>
> CSheet *sheet = reinterpret_cast<CSheet *>(wid->parent());****
>
> QScrollBar *scrollBar = reinterpret_cast<QScrollBar *>(sheet->parent());**
> **
>
> CTabWidget *tabWid = reinterpret_cast<CTabWidget *>(scrollBar->parent());*
> ***
>
> CDataPage  *dataPage = reinterpret_cast<CDataPage *>(tabWid->parent());***
> *
>
> MainWindow *mainwin = reinterpret_cast<MainWindow *>(dataPage->parent());*
> ***
>
> mainwin->statusBar()->showMessage(tr("A new plot being added!!"),5000);
> //[I][SIZE=7]Here it is crashing[/SIZE][/I]****
>
> [/code]****
>
> **
>

You meant

[code]
MainWindow *mainwin = dynamic_cast<MainWindow>(dataPage->parent());
if (mainwin!=NULL) {
   // do your thing
}
[/code]

Or use qobject_cast as recommended. This will be faster then dynamic_cast.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120424/6c286ff0/attachment.html>


More information about the Interest mailing list