yuanzhi
yuanzhi

Reputation: 11

QT question about how to use a pointer to MainWindow

I know how to use a pointer to Widget like Widget* fatherPtr = (Widget*)parentWidget();.

But in MainWindow it doesn't work when I use MainWindow* ptr = (MainWindow*)parentWidget();. It seem that it's an invalid pointer. How to solve it? I am anxious to get the correct solution, thank you very much!

Upvotes: 0

Views: 299

Answers (1)

yuanzhi
yuanzhi

Reputation: 11

I had solved my question by using the function declared in MainWindow:

MainWindow* MainWindow::getMainWindow()
{
    foreach(QWidget *w, qApp->topLevelWidgets())
    if (MainWindow* mainWin = qobject_cast<MainWindow*>(w))
            return mainWin;
    return nullptr;
}

Thank you to everyone who helped me

Upvotes: 1

Related Questions