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;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
public slots:
void openFirstChild();
void openSecondChild();
private:
Ui::MainWindow *ui;
ChildWindow1 *firstChild;
ChildWindow2 *secondChild;
};
#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;
};
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *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;
}
#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;
}
To copy to clipboard, switch view to plain text mode
childwindow1.h:
#include <QMainWindow>
namespace Ui {
class ChildWindow1;
}
{
Q_OBJECT
public:
explicit ChildWindow1
(QWidget *parent
= 0);
~ChildWindow1();
private:
Ui::ChildWindow1 *ui;
void DoSomething();
};
#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();
};
To copy to clipboard, switch view to plain text mode
childwindow1.cpp
#include "childwindow1.h"
ChildWindow1
::ChildWindow1(QWidget *parent
) : ui(new Ui::ChildWindow1)
{
ui->setupUi(this);
doSomething();
}
void ChildWindow1::doSomething()
{
// Stuff happens
}
#include "childwindow1.h"
ChildWindow1::ChildWindow1(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::ChildWindow1)
{
ui->setupUi(this);
doSomething();
}
void ChildWindow1::doSomething()
{
// Stuff happens
}
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?
Bookmarks