Results 1 to 14 of 14

Thread: A problem with MDI windows

  1. #1
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default A problem with MDI windows

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A problem with MDI windows

    QMdiSubWindow has a QMdiSubWindow::mdiArea() method that returns the MDI area.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A problem with MDI windows

    Any chance for a short code example?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A problem with MDI windows

    Qt Code:
    1. class Window : public QMdiSubWindow {
    2. Q_OBJECT
    3. public:
    4. Window() : QMdiSubWindow() {
    5. QWidget *w = ...;
    6. setWidget(w);
    7. }
    8.  
    9. public slots:
    10. void openNewWindow() {
    11. mdiArea()->addSubWindow(new Window);
    12. }
    13. };
    To copy to clipboard, switch view to plain text mode 

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A problem with MDI windows

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A problem with MDI windows

    Quote Originally Posted by Datakim View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A problem with MDI windows

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A problem with MDI windows

    Quote Originally Posted by Datakim View Post
    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    Datakim (15th April 2012)

  10. #9
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A problem with MDI windows

    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!

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A problem with MDI windows

    You shouldn't be accessing the main window through the QObject::parent() method.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A problem with MDI windows

    Quote Originally Posted by wysota View Post
    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:
    Qt Code:
    1. jasenSiirto *jassiir = new jasenSiirto(this);
    2. jassiir->setWindowTitle("Jäsenten siirto");
    3. ui->mdiArea->addSubWindow(jassiir, Qt::SubWindow);
    4. jassiir->show();
    To copy to clipboard, switch view to plain text mode 

    And then used the parent value from the created window constructor

    Qt Code:
    1. jasenSiirto::jasenSiirto(QWidget *parent) :
    2. QDialog(parent),
    3. ui(new Ui::jasenSiirto)
    To copy to clipboard, switch view to plain text mode 

    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?

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A problem with MDI windows

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Jul 2011
    Posts
    12
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A problem with MDI windows

    Quote Originally Posted by wysota View Post
    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?

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: A problem with MDI windows

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Windows to Linux Problem
    By thehilmisu in forum Newbie
    Replies: 2
    Last Post: 22nd December 2011, 09:12
  2. Replies: 19
    Last Post: 3rd April 2009, 23:17
  3. Problem with linking QT at windows.
    By zygmunt in forum Installation and Deployment
    Replies: 1
    Last Post: 21st October 2008, 21:53
  4. problem with Qt/ Windows--pls help
    By swamyonline in forum Installation and Deployment
    Replies: 8
    Last Post: 7th July 2008, 20:39
  5. Printing problem in windows
    By joseph in forum Qt Programming
    Replies: 6
    Last Post: 12th July 2007, 08:04

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.