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

André Somers andre at familiesomers.nl
Thu Apr 26 18:09:04 CEST 2012


Op 24-4-2012 17:01, Andre Haupt schreef:
> On Tue, Apr 24, 2012 at 01:58:00PM +0200, Sujan Dasmahapatra wrote:
>>
>>
>> I have my mainwindow which is QMainWindow the parent of all my other
>> classes. I am type casting upwards from 5 levels down using
>> reinterpret_cast, to access the mainwindow to display message using
>> statusBar(), but when type casting it's not giving error but when
>> accessing mainwindow->statusBar() it's crashing.
>>
>>
>> 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]
>>
>>
>>
>> please help me someone whats going wrong in this. Thanks a lot for any
>> help. Sujan
> If you just want to show a message on the status bar, do not do this
> whole bunch of casts at all.
>
> Just keep a pointer to your main window somewhere.
>
Better yet, just use a signal/slot connection. Give the widget a signal 
that you can connect to whatever, including a slot that will show a 
message on the status bar, and make it so the widget does not need to 
care about main windows at all.

André




More information about the Interest mailing list