PDA

View Full Version : Mainwindow keeps popping on top of child mainwindows



ce_nort
19th April 2016, 23:04
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:



#include <QMainWindow>
#include "childWindow1.h"
#include "childWindow2.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void openFirstChild();
void openSecondChild();

private:
Ui::MainWindow *ui;
ChildWindow1 *firstChild;
ChildWindow2 *secondChild;
};


mainwindow.cpp:



#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

connect(ui->firstButton, SIGNAL(clicked(bool)), this, SLOT(openFirstChild()));
connect(ui->secondButton, SIGNAL(clicked(bool)), this, SLOT(openSecondChild()));
}

void MainWindow::openFirstChild()
{
firstChild = new ChildWindow1(this);
firstChild->show();
}

void MainWindow::openSecondChild()
{
secondChild = new ChildWindow2(this);
secondChild->show();
}

MainWindow::~MainWindow()
{
delete firstChild;
delete secondChild;
delete ui;
}


childwindow1.h:



#include <QMainWindow>

namespace Ui {
class ChildWindow1;
}

class ChildWindow1 : public QMainWindow
{
Q_OBJECT

public:
explicit ChildWindow1(QWidget *parent = 0);
~ChildWindow1();

private:
Ui::ChildWindow1 *ui;

void DoSomething();
};



childwindow1.cpp



#include "childwindow1.h"

ChildWindow1::ChildWindow1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ChildWindow1)
{
ui->setupUi(this);

doSomething();
}

void ChildWindow1::doSomething()
{
// Stuff happens
}


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/65783-Qt-Application-Windows-Always-on-Top-of-External-Application-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?

d_stranz
20th April 2016, 15:46
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:



void MainWindow::openFirstChild()
{
firstChild = new ChildWindow1();
firstChild->show();
}

ce_nort
20th April 2016, 15:52
Yeah, it was just buried as part of a slightly different question so my searches missed it initially. Thanks for the confirmation!