Results 1 to 5 of 5

Thread: type casting 5 levels down no error but not proper

  1. #1
    Join Date
    Feb 2009
    Posts
    189
    Thanks
    2

    Default type casting 5 levels down no error but not proper

    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:

    Qt Code:
    1. QWidget *wid = reinterpret_cast<QWidget *>(parent());
    2. CSheet *sheet = reinterpret_cast<CSheet *>(wid->parent());
    3. QScrollBar *scrollBar = reinterpret_cast<QScrollBar *>(sheet->parent());
    4. CTabWidget *tabWid = reinterpret_cast<CTabWidget *>(scrollBar->parent());
    5. CDataPage *dataPage = reinterpret_cast<CDataPage *>(tabWid->parent());
    6. MainWindow *mainwin = reinterpret_cast<MainWindow *>(dataPage->parent());
    7. mainwin->statusBar()->showMessage(tr("A new plot being added!!"),5000); //[I][SIZE=7]Here it is crashing[/SIZE][/I]
    To copy to clipboard, switch view to plain text mode 

    please help me someone whats going wrong in this. Thanks a lot for any help. Sujan

  2. #2
    Join Date
    Jul 2010
    Location
    Indonesia
    Posts
    83
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: type casting 5 levels down no error but not proper

    You are playing with the most dangerous features of C++, the pointer. I suggest you to use safer solutions.

    when type casting it's not giving error
    Have you check the statusBar of your mainWindow?

    I have my mainwindow which is QMainWindow the parent of all my other classes
    The assumption is, what you want is accessing mainWindow but the problems occurred on type casting few times. Why not creating better object relationships or singleton pattern. Or find mainWindow from QApplication::topLevelWindows by name.

    I am type casting upwards from 5 levels down using reinterpret_cast, to access the mainwindow to display message using statusBar().
    Do you really need to cast 5 times? That's seems odd and error prone.
    ~ We are nothing in this universe ~

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: type casting 5 levels down no error but not proper

    AARRGGHH!! Are you mad? What you have done is simply asking to fail in new and exciting ways.

    A widget's parent widget is trivially obtained with QWidget::parentWidget(). You can backtrack up the hierarchy, checking for NULL as you go, and there's no need to cast anything. When you do get to the widget you want you can do a single qobject_cast<>() and check for a NULL result before continuing.

    If you want the top-level window that contains this widget there's an even better way to do it: QWidget::window(). As an added bonus, this method won't break if you rearrange the widgets.

  4. #4
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: type casting 5 levels down no error but not proper

    I agree, that's madness.

    Simple solution: static method in your main window class:
    Qt Code:
    1. static QStatusBar* status_bar = NULL;
    2.  
    3. MainWindow::MainWindow()
    4. {
    5. status_bar = this->statusBar();
    6. }
    7.  
    8. void MainWindow::myStaticMethod( const QString& msg, int timeout )
    9. {
    10. if( status_bar )
    11. {
    12. status_bar->showMessage( msg, timeout );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: type casting 5 levels down no error but not proper

    You can always relay proper signals to some MainWindow::showStatusMessage slot. It will be a lot safer than what you are doing now.

Similar Threads

  1. Replies: 6
    Last Post: 11th October 2010, 00:15
  2. Problem with type casting
    By qtDave in forum Newbie
    Replies: 7
    Last Post: 9th November 2009, 21:57
  3. Problem regaridng Type casting QVariant
    By sudheer168 in forum Qt Programming
    Replies: 5
    Last Post: 3rd November 2009, 07:02
  4. QVariant not returning proper type for SQLite REAL
    By rakkar in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 21:25
  5. Problem with type casting?
    By Jyoti.verma in forum Qt Programming
    Replies: 2
    Last Post: 25th August 2009, 06:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.