PDA

View Full Version : AddSubWindow() doesn't work



Higgs
16th February 2014, 20:08
I have simple MainWindow. MdiArea is in it. I can't create more small mdichild.
code of MainWindow constructor:

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
mdiArea = new QMdiArea;
setCentralWidget(mdiArea);
MdiChild *one = CreateChild();
MdiChild *two = CreateChild();
mdiArea->addSubWindow(one);
mdiArea->addSubWindow(two);

connect(ui->actionNew_Document,SIGNAL(triggered()),this,SLOT(C reateNewDocument()));


}


MdiChild* MainWindow::CreateChild()
{
MdiChild *newChild = new MdiChild;
static int count = 0;

newChild->setWindowTitle(QString("%1 - %2").arg(count++).arg("Document"));

return newChild;

}
void MainWindow::CreateNewDocument()
{
mdiArea->addSubWindow(CreateChild()); //This doesn't work
}


I commented "mdiArea->addSubWindow(CreateChild());" and added QMessageBox and that worked. so slot function is executed. but can't figure out why addSubWindow doesn't work.

10048

When I press New Document action,new window doesn't appear.

anda_skoa
16th February 2014, 20:16
Have you tried calling show() on the new window?

Cheers,
_

Higgs
16th February 2014, 20:45
I have tried but window is being immedietly closed as I see. But I can't understand why we need "show" method.
I haven't call show method here:

MdiChild *one = CreateChild();
MdiChild *two = CreateChild();
mdiArea->addSubWindow(one);
mdiArea->addSubWindow(two);

Code in CreateNewWindow I changed to:

void MainWindow::CreateNewDocument()
{
QWidget *xx = new QWidget;
xx->show();
mdiArea->addSubWindow(xx);
}


But window is dissappeared immedietly and I don't know why.




P.S Sorry for my English language mistakes. It's my second language.

anda_skoa
16th February 2014, 22:11
I have tried but window is being immedietly closed as I see. But I can't understand why we need "show" method.
I haven't call show method here:

MdiChild *one = CreateChild();
MdiChild *two = CreateChild();
mdiArea->addSubWindow(one);
mdiArea->addSubWindow(two);

Those are created in the constructor, i.e. before their parent is shown. They get shown when their parent is.
Windows created afterwards are not automatically shown but when requested.




Code in CreateNewWindow I changed to:

void MainWindow::CreateNewDocument()
{
QWidget *xx = new QWidget;
xx->show();
mdiArea->addSubWindow(xx);
}


But window is dissappeared immedietly and I don't know why.


About about calling show on the MDI window?



mdiArea->addSubWindow(CreateChild())->show();




P.S Sorry for my English language mistakes. It's my second language.

Not my first language either :)

Cheers,
_

Higgs
16th February 2014, 22:32
Thank you for your reply.
I was nearly near with solution :D
I at first wrote this:

mdiArea->addSubWindow(CreateChild()->show());