I have a OpenGL widget inside the MainWindow.
I'd like to access MainWindow from its code.
I've tried it with the qApp->activeWindow()
and findChild()
combo but it does not work.
The WidgetOpenGLDraw constructor looks like: WidgetOpenGLDraw::WidgetOpenGLDraw(QWidget* parent):QOpenGLWidget(parent),gl(nullptr){}
EDIT 1 >
This also crashes: ((MainWindow*)qApp->activeWindow())->fname_here();
.
QMainWindow
isn't the parent of WidgetOpenGLDraw
. QMainWindow
has centralWidget()
which is the parent of WidgetOpenGLDraw
. So this might work.
MainWindow* mainWindow = qobject_cast<MainWindow*>(parentWidget()->parentWidget());
if (mainWindow) {
// do stuff
}
Looks like the QWidget* parent
in the constructor is the QMainWindow. Store this as a member and you can access it from your WidgetOpenGLDraw class.
Doesn't sound nice but it should work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.