[SOLVED] QMdiArea::addSubWindow() issue
-
Hi everyone,
It is the first time I'm using MdiArea object and to test it I did a little program.
I created a MainWindow with a QMdiArea as central widget, a toolbox and a statusbar. In the toolbox there is a new button that should open a new sub window, inside the sub window there is only a QPushButton. When I click on the "New" button (actionNew) the confirmation message appears on the statusbar but no sub window is opened. I searched on the qt help and on the net, I did several changes on the code but all without success.
Below the code:
@class MainWindow : public QMainWindow {
Q_OBJECT
Ui::MainWindow *ui;public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();public slots:
void _add_subwin();
void statusbar_msg(const QString &);
};MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
// SETUP USER-INTERFACE
ui->setupUi(this);
// ADDS A SUB-WINDOW (NOT WANTED!!)
QMdiSubWindow *subwin1 = ui->mdiArea->addSubWindow(new QPushButton(this));
subwin1->setAttribute(Qt::WA_DeleteOnClose);// CONNECTIONS
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(_add_subwin()));
}MainWindow::~MainWindow() {
ui->mdiArea->closeAllSubWindows();
delete ui;
}void MainWindow::_add_subwin() {
// ADDS THE SUB-WINDOW
QMdiSubWindow *subwin1 = ui->mdiArea->addSubWindow(new QPushButton(this));
subwin1->setAttribute(Qt::WA_DeleteOnClose);
//subwin->show();
//ui->mdiArea->update();// STATUSBAR MESSAGE
statusbar_msg("New window added");
}void MainWindow::statusbar_msg(const QString &msg) {
// SENDS TO THE STATUS BAR A MESSAGE (TIMEOUT = 5 seconds)
ui->statusBar->showMessage(msg, 5000);
}@As you can see, the only way I managed to open a subwindow is from the constructor (not the behavior wanted).
I tried QMdiSubWindow::show() and QMdiArea::update() from the MainWindow::_add_subwin() function but doesn't works.Thanks in advance for your help.
Sebastian -
I've had focusing issues when opening multiple subwindows unless I create them in the following manner:
@
QMdiSubWindow* sw = new QMdiSubWindow( ui->mdiArea );
sw->setWidget( new QPushButton( sw ) );
sw->setAttribute( Qt::WA_DeleteOnClose );
ui->mdiArea->addSubWindow( sw );
sw->show();
@perhaps the above pattern could work for you.
-
Hello joeuser,
Thanks, this solved the problem! But I would like to understand why.
From the code above, the reasons I see are:
- that I didn't declare as parent of the subwindow the mdiArea object (tested: not true)
- that the call of show() is mandatory (tested: true)
But, in the original code, I called show and nothing happend...
Anyone has an idea what the problem was?
-
The problem was show(). I tested the old code with show() and now it works:
@QMdiSubWindow *subwin1 = ui->mdiArea->addSubWindow(new QPushButton);
subwin1->setAttribute(Qt::WA_DeleteOnClose);
subwin1->show();@Thanks again joeuser.