PDA

View Full Version : Open subwindow



olidem
14th May 2009, 11:50
Hi!

Although this is a often treated problem, I am confused about the thousand possibilities that exist to realise what I want.

I have a slot being connected to a button that shall open a subwindow.

- this subwindow is a subclass of QWidget
- it shall not appear in the taskbar (on Windows)
- it shall have a minimize-, maximize- and close-button
- it shall have a system menu in the upper left corner
- it shall be resizable
- it shall be MODAL, the window containing the button to open the subwindow shall be blocked as long as the subwindow is open.

So far I have the following:


void MainWindow::openSubWindow()
{
SubWindowWidget w(customer);
QDialog diag(0, Qt::Window );
diag.setSizeGripEnabled(true);
QHBoxLayout * lay = new QHBoxLayout;
lay->addWidget(&w);
diag.setLayout(lay);
diag.exec();
}


First problem of this: the destructor of w is called twice!
Can I somehow omit the usage of a QDialog?

spirit
14th May 2009, 12:13
try this


void MainWindow::openSubWindow()
{
SubWindowWidget *w = new SubWindowWidget(customer);
w->setAttribute(Qt::WA_DeleteOnClose);
w->setAttribute(Qt::WA_ShowModal);
w->show();
// QDialog diag(0, Qt::Window );
// diag.setSizeGripEnabled(true);
// QHBoxLayout * lay = new QHBoxLayout;
// lay->addWidget(&w);
// diag.setLayout(lay);
// diag.exec();
}

spirit
14th May 2009, 12:13
btw, customer is a widget?

olidem
14th May 2009, 12:21
NICE NICE NICE NICE !!!!

Thx, this is what i was looking for.

The only thing that is still not how I want it is the fact the the subwindow gets an entry in the Windows taskbar.

spirit
14th May 2009, 12:23
try this


void MainWindow::openSubWindow()
{
SubWindowWidget *w = new SubWindowWidget(0);
w->setAttribute(Qt::WA_DeleteOnClose);
w->setAttribute(Qt::WA_ShowModal);
w->show();
}

that's why I was asking about customer. :)

wysota
14th May 2009, 13:27
The only thing that is still not how I want it is the fact the the subwindow gets an entry in the Windows taskbar.

Pass the main window as the parent of the dialog.

olidem
14th May 2009, 13:50
Cool, that's it.

I just had to additionally specify

w->setWindowFlags(Qt::Window);

To summarise, I use the following code now:



void MainWindow::openSubWindow()
{
SubWindowWidget *w = new SubWindowWidget(0);
w->setParent(this);
w->setWindowFlags(Qt::Window);
w->setAttribute(Qt::WA_DeleteOnClose);
w->setAttribute(Qt::WA_ShowModal);
w->show();
}


Thanks for all your help again!

Problem solved, thread can be closed.

spirit
14th May 2009, 13:53
it's better to do like this


void MainWindow::openSubWindow()
{
SubWindowWidget *w = new SubWindowWidget(this);
w->setAttribute(Qt::WA_DeleteOnClose);
w->setAttribute(Qt::WA_ShowModal);
w->show();
}

EDITED: you can also remove w->setAttribute(Qt::WA_DeleteOnClose); because since you pass a parent in SubWindowWidget it will be deleted when a parent is being deleted.

wysota
14th May 2009, 14:12
I'd do it like this:

void MainWindow::openSubWindow()
{
QDialog dlg(this);
QHBoxLayout *l = new QHBoxLayout(&dlg);
SubWindowWidget *w = new SubWindowWidget;
l->addWidget(w);
dlg.exec();
}