Joseph
Joseph

Reputation: 13218

Qt/C++: Having an MDI child call a function of its parent?

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?

Upvotes: 1

Views: 2015

Answers (2)

Arnold Spence
Arnold Spence

Reputation: 22292

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.

Upvotes: 2

Fred
Fred

Reputation: 4954

Use qobject_cast to get a pointer to MainWindow :

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

Upvotes: 3

Related Questions