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

Carel Combrink carel.combrink at gmail.com
Wed Apr 25 09:12:05 CEST 2012


If your requirement is " Please help me getting the mainwindow handle "
then try something like this:

    bool continueSearching = true;

    QWidget* widgetParent = NULL;

    QMainWindow* mainWindow = NULL;

    QWidget* currentWidget = this;

    while(continueSearching == true) {

        widgetParent = qobject_cast<QWidget*>(currentWidget->parent());

        if(widgetParent != NULL) {

            currentWidget = widgetParent;

            mainWindow = qobject_cast<QMainWindow*>(currentWidget);

            if(mainWindow != NULL) {

                continueSearching = false;

            }

        } else {

            continueSearching = false;

        }

    }

    if(mainWindow != NULL) {

        mainWindow->statusBar()->showMessage(tr("A new plot being
added!!"),5000);

    }


The idea is to loop through all of the parents of the widgets until one is
a QMainWindow. The other approaches can also work like a global mainwindow
or something. This can be used to get the QMainWindow of any QWidget.

Note: I have briefly tested it and it seems to work. It might have problems
but the basic idea is there.

Regards,


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

>   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;****
>
>     }****
>
> ** **
>
> [code]****
>
> QWidget *wid  = qobject_cast<QWidget *>(parent());****
>
> CSheet *sheet = qobject _cast<CSheet *>(wid->parent());****
>
> QScrollBar *scrollBar = qobject _cast<QScrollBar *>(sheet->parent());****
>
> CTabWidget *tabWid = qobject _cast<CTabWidget *>(scrollBar->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]****
>
> ** **
>
> CTabWidget is a custom class derived from QTabWidget.****
>
> ** **
>
> Please help me getting the mainwindow handle.  Thanks for your reply.****
>
> ** **
>
> ** **
>
> ** **
>
> * *
>
> *From:* Diego Iastrubni [mailto:diegoiast at gmail.com]
> *Sent:* Tuesday, April 24, 2012 5:41 PM
> *To:* Sujan Dasmahapatra
> *Cc:* interest at qt-project.org
> *Subject:* Re: [Interest] type casting 5 levels down no error but not
> proper****
>
> ** **
>
> ** **
>
> 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.
> ****
>
> ** **
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120425/d59b8f61/attachment.html>


More information about the Interest mailing list