PDA

View Full Version : see if mdi child is already open



ShapeShiftme
16th December 2010, 03:00
Good day.
I am really new to QT development
I am currently opening a mdi chlld by the following means


web_view *child = new web_view;
ui->mdi1->addSubWindow(child);
//child->show();
child->showMaximized();

Now the above works great but the problem im having is that every time i click the button it opens a new window (child). How would i put something that says


if (window == open ) //will nee to code to see if it is open
{
reload child //Code to reload the child.
}
else
{
web_view *child = new web_view;
ui->mdi1->addSubWindow(child);
child->showMaximized();
}


Help would be greatly appriciated

Regards

high_flyer
16th December 2010, 20:26
It is strongly recommended to read the Qt docs, as they are really good, and will supply answers most of the time, including this question.
To your question:
First of all you will need to make your 'child' variable a member, so that you can hold the pointer to the window you want to check.
Then you have QMdiArea::activeSubWindow() so you can see if your 'child' is the active one.
Or, if you need some other criteria that is not satisfied by activeSubWindow(), you can iterate though all the sub windows by getting them from QMdiArea::subWindowList().

ShapeShiftme
17th December 2010, 12:25
thanks for the direction however i nwe that is what i needed to do so i google the function and saw someone else using the code and modified that.
so this is how i got it to work.

The thing i cant figure out now is that when i load the form i want to be able to run a function on it how would i do that.


QString isopen = "No";

foreach (QMdiSubWindow *window, ui->mdi1->subWindowList())
{
if(window->windowTitle().contains(tr("Webview"),Qt::CaseInsensitive)) //i set the form im looking for title to this on load
{
ui->mdi1->setActiveSubWindow(window);

// run function on child form
// eg . window.myfunctiontorun() or child.functiontorun()


isopen = "Yes";
}
}

if(isopen == "No")
{
web_view *child = new web_view;
ui->mdi1->addSubWindow(child);
child->showMaximized();
}


Help with this regard will be greatly appriciated

high_flyer
17th December 2010, 12:31
I am really at a lose to you what you mean.
You have the SubWindow pointer 'window', so just call the function you need on it.
Where is the problem?

ShapeShiftme
17th December 2010, 12:48
On my subwindow i have a function called "reloadme"


ui->mdi1->setActiveSubWindow(window);
window.reloadme(); //does not work

isopen = "Yes";

so how would i get it to run.
I saw something like


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


However this is to run a function on the mainwindow from the child and i need to run something on the child from the mainform.
If i need to modify this could you let me know howto.

Thanks for the help

high_flyer
17th December 2010, 12:56
You question is not in the scope of this forum, as it is about general OOP (not even C++) concept, in particular - polymorphism.
Please first read and understand what polymorphism is, and how it is bying used in C++, and what is the syntax for it.
The second code box you posted is exactly what you need, but instead of casting to MainWidnow, you need to cast to your own subclass.


window.reloadme();
This would not compile even if you would have correctly casted, since you have a pointer.
It should be :


MySubWindow* pSubWindow = qobject_cast<MySubWindow*>(window);
pSubWindow->reloadme();

ShapeShiftme
17th December 2010, 13:23
thanks very much. That is exactly what i needed. I will read up on polymorphism.
Its ppl like you that are willing to help when us noobs get stuck that make this site and programming in general great.

regards

ShapeShiftme
8th January 2011, 04:36
OK i have a problem with running the fuction i called.



MySubWindow* pSubWindow = qobject_cast<MySubWindow*>(window);
pSubWindow->reloadme();

This Works perfectly A message box does display with the correct url i want my webview to goto



void frm_web::reloadme( ){
extern QString urltogo;
QMessageBox msgbox;
msgbox.setText("you pushed me" + urltogo);
msgbox.exec();
}

However this next function closes the program with
"The program has unexpectedly finished." No errors are displayed



void frm_web::reloadme( ){
extern QString urltogo;
QMessageBox msgbox;
msgbox.setText("you pushed me : " + urltogo);
msgbox.exec();

ui->webView->load(QUrl(urltogo));
}

Im perhaps thinking it has to do with the reference ui but would not know how to get around it
i did try this->webView however that did not work.

Help would be greatly appreciated

high_flyer
8th January 2011, 12:11
Well, to help us help you, run this in a debugger, and step through, and see exactly on which line it crashes.
Also, the extern keyword use here is very strange - and probably points to a design problem, in OOP and C++, you hardly ever need to define extern.
You should at least test that the string really is initialized.