Results 1 to 3 of 3

Thread: Mainwindow keeps popping on top of child mainwindows

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Mainwindow keeps popping on top of child mainwindows

    I have a MainWindow class that has buttons to open child windows. The Mainwindow class is a sort of control center that consolidates a few different programs into one access point. A simplified example of what I have done:

    mainwindow.h:

    Qt Code:
    1. #include <QMainWindow>
    2. #include "childWindow1.h"
    3. #include "childWindow2.h"
    4.  
    5. namespace Ui {
    6. class MainWindow;
    7. }
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit MainWindow(QWidget *parent = 0);
    15. ~MainWindow();
    16.  
    17. public slots:
    18. void openFirstChild();
    19. void openSecondChild();
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. ChildWindow1 *firstChild;
    24. ChildWindow2 *secondChild;
    25. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9.  
    10. connect(ui->firstButton, SIGNAL(clicked(bool)), this, SLOT(openFirstChild()));
    11. connect(ui->secondButton, SIGNAL(clicked(bool)), this, SLOT(openSecondChild()));
    12. }
    13.  
    14. void MainWindow::openFirstChild()
    15. {
    16. firstChild = new ChildWindow1(this);
    17. firstChild->show();
    18. }
    19.  
    20. void MainWindow::openSecondChild()
    21. {
    22. secondChild = new ChildWindow2(this);
    23. secondChild->show();
    24. }
    25.  
    26. MainWindow::~MainWindow()
    27. {
    28. delete firstChild;
    29. delete secondChild;
    30. delete ui;
    31. }
    To copy to clipboard, switch view to plain text mode 

    childwindow1.h:

    Qt Code:
    1. #include <QMainWindow>
    2.  
    3. namespace Ui {
    4. class ChildWindow1;
    5. }
    6.  
    7. class ChildWindow1 : public QMainWindow
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. explicit ChildWindow1(QWidget *parent = 0);
    13. ~ChildWindow1();
    14.  
    15. private:
    16. Ui::ChildWindow1 *ui;
    17.  
    18. void DoSomething();
    19. };
    To copy to clipboard, switch view to plain text mode 

    childwindow1.cpp

    Qt Code:
    1. #include "childwindow1.h"
    2.  
    3. ChildWindow1::ChildWindow1(QWidget *parent) :
    4. QMainWindow(parent),
    5. ui(new Ui::ChildWindow1)
    6. {
    7. ui->setupUi(this);
    8.  
    9. doSomething();
    10. }
    11.  
    12. void ChildWindow1::doSomething()
    13. {
    14. // Stuff happens
    15. }
    To copy to clipboard, switch view to plain text mode 

    Assume that the ChildWindow2 class is similar to ChildWindow1, and that there is a ChildWindow3, ChildWindow4, etc. Frequently multiple child windows will be open with users going back and forth between them. The problem that I've been having is that whenever one of the child windows is closed or when a dialog (even a QFileDialog) that one of the child windows opens is closed, the Mainwindow pops on top of all child windows. It's very irritating for the user. I don't really want to use the WindowStaysOnTopHint or WindowStaysOnBottomHint because the user is often doing other things and would like to select which window is on top or bottom at a given moment. Is there a way to keep the mainwindow from popping on top of everything when a child window/dialog is closed? Am I doing something incorrectly? Any help would be much appreciated.

    EDIT:


    I just found this thread that may be related: http://www.qtcentre.org/threads/6578...cation-Windows . Would it be safe to assume based on the linked thread that I need to make it so the Mainwindow is not the parent of the child windows?
    Last edited by ce_nort; 20th April 2016 at 00:14.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Mainwindow keeps popping on top of child mainwindows

    I was about to say, I answered exactly the same question last week, but you did your homework and searched for the answer. Do not make your ChildWindow instances children of the MainWindow:

    Qt Code:
    1. void MainWindow::openFirstChild()
    2. {
    3. firstChild = new ChildWindow1();
    4. firstChild->show();
    5. }
    To copy to clipboard, switch view to plain text mode 

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

    ce_nort (20th April 2016)

  4. #3
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Mainwindow keeps popping on top of child mainwindows

    Yeah, it was just buried as part of a slightly different question so my searches missed it initially. Thanks for the confirmation!

Similar Threads

  1. Replies: 4
    Last Post: 17th January 2011, 15:05
  2. Replies: 4
    Last Post: 17th April 2010, 03:42
  3. Child Widgets In MainWindow
    By RY in forum Newbie
    Replies: 3
    Last Post: 4th October 2008, 09:39
  4. Closing all of the mainWindow's child dialogs
    By JPNaude in forum Qt Programming
    Replies: 4
    Last Post: 2nd October 2008, 14:18
  5. MainWindow in front of child modeless dialog
    By jiveaxe in forum Qt Programming
    Replies: 4
    Last Post: 10th August 2007, 17:18

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.