Results 1 to 7 of 7

Thread: Simple Mdi question

  1. #1
    Join Date
    Sep 2011
    Location
    Buenos Aires
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Simple Mdi question

    Hello.
    I have a MDI area with a menu.
    that i created like this.

    Qt Code:
    1. setupUi(this);
    2. mdiArea = new QMdiArea;
    3. mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    4. mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    5. setCentralWidget(mdiArea);
    6.  
    7. setWindowTitle(tr("MDI"));
    8. setUnifiedTitleAndToolBarOnMac(true);
    To copy to clipboard, switch view to plain text mode 

    When one of hte options is clicked i create a window like this.

    Qt Code:
    1. frmUserProfile *b = new frmUserProfile;
    2. mdiArea->addSubWindow(b);
    3. b->exec();
    To copy to clipboard, switch view to plain text mode 

    Then from that window i want to open a second one when the user clicks on a button.
    the problem is that mdiArea does not exist there and if i do

    Qt Code:
    1. frmTest *t = new frmTest(this);
    2. t->exec();
    3. delete t;
    To copy to clipboard, switch view to plain text mode 

    The window is shown outside of the mdi area.

    One workaround i found is to use a signal to make the mainwindow open frmTest, but it looks like a ugly thing to do and I'm sure other problems will arise from that method.

    How can i fix the problem of Qdialogs appearing outside the MDIarea?
    I realize that maybe this is more of a c++ problem than a Qt one, sorry for that.

    Thanks!
    Last edited by slqh; 21st September 2011 at 00:04.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Simple Mdi question

    The grandparent of the first widget, if it exists, can be cast to QMdiArea to add another widget... or you can emit a signal and have the main window add the new child. The second option is more flexible, e.g. if the sub window widget is used standalone also.

    I don't know what you expect to happen when you call exec() on the child widget after adding it to the MDI area. The QDialog::exec() call is used to obtain a modal dialog, which is not particularly consistent with MDI, and breaks a simple test program on my machine.
    Last edited by ChrisW67; 21st September 2011 at 03:34.

  3. The following user says thank you to ChrisW67 for this useful post:

    slqh (21st September 2011)

  4. #3
    Join Date
    Sep 2011
    Location
    Buenos Aires
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple Mdi question

    ChrisW67 thanks for your response.
    i will try to clarify why I'm using exec there.

    The intended usage is something like this.

    The user clicks on the menu.
    A form is created where a list of the products is show wich has a button to create a new one.
    That new form should block the product list to prevent the user from clicking tome other stuff on THAT parent form only. not to prevent him or her to open other unrelated things from the menu.

    With mi current approach the third form appears outside the mdi area.
    i don't know what are you referring to when you say that it breaks your test program, here compiles and runs fine, but maybe as I'm just a beginner something is not behaving as it should and I can't notice.

    i think i will go with the signal/slot approach and prevent the user from using the intended parent widget by hiding it.

  5. #4
    Join Date
    May 2012
    Location
    Serbia, Sabac
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: Simple Mdi question

    I know it's been more than a year since the last post, but I don't think I should open another threat because of that.

    Quote Originally Posted by ChrisW67 View Post
    The grandparent of the first widget, if it exists, can be cast to QMdiArea to add another widget...
    Can you be more specific about how to do that? Maybe a short example would help..

    What I have is QMainWIndow as a parent, QMdiArea as a central widget. User opens form2 from the menu bar, and its added to QMdiSubWindow. If the users clicks the button on form2, it should open form3 and it should be added to QMdiSubWindow also.

    Thank you for your help,
    Dejan

  6. #5
    Join Date
    May 2012
    Location
    Serbia, Sabac
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: Simple Mdi question

    Anyone ?
    Dejan

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Simple Mdi question

    I think you should prefer using a signal. Here is one of many ways to organise this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. class PushButton: public QPushButton
    4. {
    5. Q_OBJECT
    6. public:
    7. PushButton(QWidget *p = 0): QPushButton(p) {
    8. connect(this, SIGNAL(clicked()), SLOT(doClicked()));
    9. }
    10.  
    11. private slots:
    12. void doClicked() {
    13. QTextEdit *edit = new QTextEdit(this); // could be any widget
    14. emit addAnotherMdiChild(edit);
    15. }
    16.  
    17. signals:
    18. void addAnotherMdiChild(QWidget *child);
    19. };
    20.  
    21.  
    22. class MainWindow: public QMainWindow
    23. {
    24. Q_OBJECT
    25. public:
    26. MainWindow(QWidget *p = 0): QMainWindow(p) {
    27. m_area = new QMdiArea(this);
    28. setCentralWidget(m_area);
    29.  
    30. PushButton *button = new PushButton(this);
    31. button->setText("Add another widget");
    32. connect(button, SIGNAL(addAnotherMdiChild(QWidget *)), SLOT(doAddAnotherMdiChild(QWidget *)));
    33. doAddAnotherMdiChild(button);
    34. }
    35.  
    36. public slots:
    37. void doAddAnotherMdiChild(QWidget *child) {
    38. (void) m_area->addSubWindow(child);
    39. child->show();
    40. }
    41.  
    42. private:
    43. QMdiArea *m_area;
    44. };
    45.  
    46.  
    47. int main(int argc, char **argv) {
    48. QApplication app(argc, argv);
    49.  
    50. MainWindow w;
    51. w.show();
    52. return app.exec();
    53. }
    54. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to ChrisW67 for this useful post:

    gojkovicde (17th December 2012)

  9. #7
    Join Date
    May 2012
    Location
    Serbia, Sabac
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Simple Mdi question

    Thank you for the advice and example, I'll get into details of it as soon as I can..
    Dejan

Similar Threads

  1. Simple question
    By zorro68 in forum Qt Programming
    Replies: 2
    Last Post: 22nd February 2011, 12:42
  2. simple MVC question!
    By landonmkelsey in forum Qt Programming
    Replies: 14
    Last Post: 19th August 2008, 12:25
  3. A simple question
    By bruccutler in forum Newbie
    Replies: 1
    Last Post: 6th April 2007, 08:54
  4. simple question
    By mickey in forum Newbie
    Replies: 2
    Last Post: 20th June 2006, 10:39
  5. Another simple question...
    By Dark_Tower in forum Newbie
    Replies: 5
    Last Post: 31st March 2006, 16:13

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
  •  
Qt is a trademark of The Qt Company.