简体   繁体   中英

Qt access MainWindow from QOpenGLWidget

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM