1
votes

I have a Qt Designer Class called MainWindow which is a QMainWindow. This class creates an MDI Area and has children windows, as MDI should.

One of these MDI children, we'll call wndChild which is also a QMainWindow, needs to spawn a "sibling" (that is, an MDI child of its parent, not an MDI child of its own). I figured the best way to do that would be to create a public function in the parent (MainWindow) which then creates the new MDI child.

The problem is, I'm not sure how to call this function from the child. I tried something like:

MainWindow *mdiparent=this->parentWidget();
mdiparent->spawnOtherChild();

But QMainWindow::parentWidget returns a pointer to a QWidget, not a QMainWindow, so I of course get an error.

How might I go about doing this?

2

2 Answers

3
votes

Use qobject_cast to get a pointer to MainWindow :

MainWindow *mdiparent = qobject_cast<MainWindow*>(this->parentWidget());
mdiparent->spawnOtherChild();
2
votes

You could define a signal to be emitted by the MDI child. Connect that to a slot in your MainWindow and spawn the other child window from the slot handler.