PDA

View Full Version : A problem with MDI windows



Datakim
15th April 2012, 17:24
Hello all, I have a question that I hope someone can answer.

I am trying to create a window with multiple child MDI windows.

I have a main window, and I add MDI child window to it like this:

(create window and such)
ui->mdiArea->addSubWindow(test, Qt::SubWindow);

This seems to work in that the new window appears as an MDI child window under the primary window.

However, what I would now like to do is have this new MDI child window create another child window like itself. Basically I want to achieve the same effect as if I had run another addSubWindow from the mainwindow, but I need it to run from a button in the newly created MDI child window, and I don't know how I should go about doing this.

I was wondering if there is some way of pointing from the child window to the parent so I could run the "addSubWindow" command again from the parent mainwindow? Or is there some other way.

Hopefully I managed to explain my dilemma properly.

Thanks!

wysota
15th April 2012, 17:35
QMdiSubWindow has a QMdiSubWindow::mdiArea() method that returns the MDI area.

Datakim
15th April 2012, 17:57
Any chance for a short code example? :)

wysota
15th April 2012, 18:05
class Window : public QMdiSubWindow {
Q_OBJECT
public:
Window() : QMdiSubWindow() {
QWidget *w = ...;
setWidget(w);
}

public slots:
void openNewWindow() {
mdiArea()->addSubWindow(new Window);
}
};

By the way, it is much better to add new windows to the MDI area from within elsewhere than the subwindow class itself. Your initial approach is much better than this.

Datakim
15th April 2012, 18:42
I need to be able to open the new mdiwindow through a button click on the mdi child window.

So basically I have:

Mainwindow
|
mdichildwindow1 (this I can create from the Mainwindow using ui->mdiArea->addSubWindow...)

And I need to create mdichildwindow2 by pressing a button on mdichildwindow1 so it will look like this

Mainwindow
|......... |
mdi1.. mdi2..

How would I go about opening it from the main window?

Should I create a function or something in the mainwindow, and then somehow call it with the necessary parameters from the child window?

How would I go about doing that?

Thanks

wysota
15th April 2012, 20:58
I need to be able to open the new mdiwindow through a button click on the mdi child window.
That doesn't mean the code for that needs to be in the child window class.

Datakim
15th April 2012, 21:08
But how do I execute the code from the mainwindow

So basically I have:
mainmdiwindow
and
mdichild1

and I have a button on the mdichild1

I can create the mdichild1 from a button in mainmdiwindow just fine. But now I need to be able to create mdichild2 from a button in mdichild1 in such a way that it appears in the same mdi area as mdichild1.

What kind of code do I need to place on the button clicked event in the mdichild1 in order to call say a function on mainmdiwindow to create a mdichild2 under mainmdiwindow

Sorry if I am asking idiotic questions, but I am very much a newbie and just learning.

wysota
15th April 2012, 21:12
But how do I execute the code from the mainwindow

For example you can use signals and slots. Also you can always pass a pointer to one object to another object that needs to access its functions.

Datakim
15th April 2012, 21:19
Ahh. I figured out the problem.

When I was creating the mdichild window, I did not pass a (this) pointer parameter, but I still tried to use the childwindows parent value to try and access the mainmdiwindow. Which was ofcourse a default of 0 since I did not send a pointer.

I feel like an idiot now :)

I managed to get it to work now. Thank you very much for your help!

wysota
15th April 2012, 21:23
You shouldn't be accessing the main window through the QObject::parent() method.

Datakim
16th April 2012, 00:21
You shouldn't be accessing the main window through the QObject::parent() method.

Why not?

This is what I am doing now.

I create a new window in the mainwindow with:


jasenSiirto *jassiir = new jasenSiirto(this);
jassiir->setWindowTitle("Jäsenten siirto");
ui->mdiArea->addSubWindow(jassiir, Qt::SubWindow);
jassiir->show();


And then used the parent value from the created window constructor



jasenSiirto::jasenSiirto(QWidget *parent) :
QDialog(parent),
ui(new Ui::jasenSiirto)


I then managed to use the signaling system to contact the mainwindow and get it to create another mdichild window.

It seemed to work atleast. Are there some stability/security/other problems with this approach? How should I do it then?

wysota
16th April 2012, 09:03
Ah, you're doing it this way... Splitting the code into two parts (one for creating the dialog, one for adding it to the mdi area) doesn't make much sense. And you don't need to set the parent yourself since addSubWindow will reparent the widget to the mdi area.

Datakim
17th April 2012, 05:30
Ah, you're doing it this way... Splitting the code into two parts (one for creating the dialog, one for adding it to the mdi area) doesn't make much sense. And you don't need to set the parent yourself since addSubWindow will reparent the widget to the mdi area.

Can you give an example or point to somewhere where there is one, for a way to do it that makes sense?

wysota
17th April 2012, 08:12
The simplest solution I can think of right now is to emit a signal from the child window that the user requests a new window to be opened and connect that signal to a slot in the main window that will open the new window. Then the complete code can be in one place in the main window as you can open the first child window using the exact same slot.