Results 1 to 9 of 9

Thread: see if mdi child is already open

  1. #1
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default see if mdi child is already open

    Good day.
    I am really new to QT development
    I am currently opening a mdi chlld by the following means

    Qt Code:
    1. web_view *child = new web_view;
    2. ui->mdi1->addSubWindow(child);
    3. //child->show();
    4. child->showMaximized();
    To copy to clipboard, switch view to plain text mode 

    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

    Qt Code:
    1. if (window == open ) //will nee to code to see if it is open
    2. {
    3. reload child //Code to reload the child.
    4. }
    5. else
    6. {
    7. web_view *child = new web_view;
    8. ui->mdi1->addSubWindow(child);
    9. child->showMaximized();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Help would be greatly appriciated

    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: see if mdi child is already open

    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().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: see if mdi child is already open

    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.

    Qt Code:
    1. QString isopen = "No";
    2.  
    3. foreach (QMdiSubWindow *window, ui->mdi1->subWindowList())
    4. {
    5. if(window->windowTitle().contains(tr("Webview"),Qt::CaseInsensitive)) //i set the form im looking for title to this on load
    6. {
    7. ui->mdi1->setActiveSubWindow(window);
    8.  
    9. // run function on child form
    10. // eg . window.myfunctiontorun() or child.functiontorun()
    11.  
    12.  
    13. isopen = "Yes";
    14. }
    15. }
    16.  
    17. if(isopen == "No")
    18. {
    19. web_view *child = new web_view;
    20. ui->mdi1->addSubWindow(child);
    21. child->showMaximized();
    22. }
    To copy to clipboard, switch view to plain text mode 

    Help with this regard will be greatly appriciated

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: see if mdi child is already open

    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?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: see if mdi child is already open

    On my subwindow i have a function called "reloadme"

    Qt Code:
    1. ui->mdi1->setActiveSubWindow(window);
    2. window.reloadme(); //does not work
    3.  
    4. isopen = "Yes";
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. MainWindow *mdiparent = qobject_cast<MainWindow*>(this->parentWidget());
    2. mdiparent->spawnOtherChild();
    To copy to clipboard, switch view to plain text mode 

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: see if mdi child is already open

    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 :
    Qt Code:
    1. MySubWindow* pSubWindow = qobject_cast<MySubWindow*>(window);
    2. pSubWindow->reloadme();
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. The following user says thank you to high_flyer for this useful post:

    ShapeShiftme (17th December 2010)

  8. #7
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: see if mdi child is already open

    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

  9. #8
    Join Date
    Dec 2010
    Location
    South Africa
    Posts
    56
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: see if mdi child is already open

    OK i have a problem with running the fuction i called.


    Qt Code:
    1. MySubWindow* pSubWindow = qobject_cast<MySubWindow*>(window);
    2. pSubWindow->reloadme();
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void frm_web::reloadme( ){
    2. extern QString urltogo;
    3. QMessageBox msgbox;
    4. msgbox.setText("you pushed me" + urltogo);
    5. msgbox.exec();
    6. }
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. void frm_web::reloadme( ){
    2. extern QString urltogo;
    3. QMessageBox msgbox;
    4. msgbox.setText("you pushed me : " + urltogo);
    5. msgbox.exec();
    6.  
    7. ui->webView->load(QUrl(urltogo));
    8. }
    To copy to clipboard, switch view to plain text mode 

    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

  10. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: see if mdi child is already open

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. How to resize a Mdi child?
    By qlands in forum Newbie
    Replies: 2
    Last Post: 25th May 2016, 18:53
  2. Replies: 3
    Last Post: 25th August 2010, 12:39
  3. Child always on top on XP
    By mpi in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2010, 17:36
  4. Why QSqlDatabase::open() returns open?
    By gboelter in forum Newbie
    Replies: 7
    Last Post: 27th August 2009, 18:52
  5. Best way to add child windows
    By Mrdata in forum Qt Programming
    Replies: 5
    Last Post: 3rd February 2007, 13:55

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.