-1

I have a OpenGL widget inside the MainWindow.

enter image description here

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){}

EDIT1>

This also crashes: ((MainWindow*)qApp->activeWindow())->fname_here();.

Rok
  • 787
  • 9
  • 17
  • 1
    Possible duplicate of [How to access to parent widget on qt?](http://stackoverflow.com/questions/352758/how-to-access-to-parent-widget-on-qt) – LogicStuff Nov 13 '15 at 12:30
  • @LogicStuff I'm not doing anything with slots and I've already tried casting the parent argument from my constructor but it did not work. – Rok Nov 13 '15 at 12:53
  • `dynamic_cast` maybe? – LogicStuff Nov 13 '15 at 13:04
  • @LogicStuff If you meant something like this `(dynamic_cast((qApp->activeWindow())))->fname_here();` then it does not work. Did you mean I should try it with the parent constructor argument? – Rok Nov 13 '15 at 13:13

2 Answers2

1

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
}
ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35
0

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.

LiamT
  • 112
  • 5