PDA

View Full Version : How to delete modeless dialog?



rajesh
16th June 2009, 10:34
I want to create and show multiple dialog(or widget) on multiple user click.
so I written as:


void MainWindow::onIRSignalReport()
{
...
SignalWindow *pSignalWindow = new SignalWindow();
pSignalWindow->setSignalFile(fileName);
pSignalWindow->show();
}

onIRSignalReport() getting called on menu item clicks.

lets user clicked multiple times and multiple SignalWindow is displayed.

if user close one of the user window then how I delete that pointer(pSignalWindow ) ?:(

spirit
16th June 2009, 10:38
set Qt::WA_DeleteOnClose for your window, e.g.


void MainWindow::onIRSignalReport()
{
...
SignalWindow *pSignalWindow = new SignalWindow();
pSignalWindow->setAttribute(Qt::WA_DeleteOnClose);
pSignalWindow->setSignalFile(fileName);
pSignalWindow->show();
}

rajesh
16th June 2009, 10:50
Thanks spirit.

and, how to close pSignalWindow if application closed?

if I pass this pointer also it is not getting closed.
SignalWindow *pSignalWindow = new SignalWindow((QDialog *)this);

spirit
16th June 2009, 10:53
if you close your app, SignalWindow will not be closed, since you have not passed a parent into SignalWindow ctor, line 4 of your code. so, anyway you must to close this window.

rajesh
16th June 2009, 10:57
void MainWindow::onIRSignalReport()
{
...
SignalWindow *pSignalWindow = new SignalWindow((QDialog *)this);
pSignalWindow->setAttribute(Qt::WA_DeleteOnClose);
pSignalWindow->setSignalFile(fileName);
pSignalWindow->show();
}

now I am passing parent, right? but why It is not getting closed?

spirit
16th June 2009, 11:01
I don't know, works fine for me. show us main.cpp code.

rajesh
16th June 2009, 11:12
int main(int argc, char *argv[])
{
QApplication a(argc, argv);

GUIManager * pGUIManager = GUIManager::getInstance(); // GUIManager is a singleton
pGUIManager->initialize();

return a.exec();
}

bool GUIManager::initialize()
{
bool result = true; //TODO: may be required.

pMainWindow = new MainWindow;

return result;
}

spirit
16th June 2009, 11:12
play with this test app
test.h


#ifndef TEST_H
#define TEST_H

#include <QDialog>

class Test: public QDialog
{
Q_OBJECT

public:
Test(QWidget *parent = 0);

private slots:
void addWindowWithParent();
void addWindowWithoutParent();

};

#endif//TEST_H


test.cpp


#include <QtGui>
#include "test.h"

Test::Test(QWidget *parent)
: QDialog(parent)
{
QHBoxLayout *hbl = new QHBoxLayout(this);
QPushButton *pb1 = new QPushButton(tr("Add window with parent"));
QPushButton *pb2 = new QPushButton(tr("Add window without parent"));

hbl->addWidget(pb1);
hbl->addWidget(pb2);

connect(pb1, SIGNAL(clicked()), SLOT(addWindowWithParent()));
connect(pb2, SIGNAL(clicked()), SLOT(addWindowWithoutParent()));

setWindowTitle(tr("main"));
}

void Test::addWindowWithParent()
{
Test *test = new Test(this);
test->setAttribute(Qt::WA_DeleteOnClose);
test->move(x() + 40, y() + 40);
test->setWindowTitle(tr("with parent"));
test->show();
}

void Test::addWindowWithoutParent()
{
Test *test = new Test();
test->setAttribute(Qt::WA_DeleteOnClose);
test->move(x() + 40, y() + 40);
test->setWindowTitle(tr("without parent"));
test->show();
}


main.cpp


#include <QApplication>
#include "test.h"

int main(int argc, char **argv)
{
QApplication app(argc, argv);

Test test;
test.show();

return app.exec();
}

rajesh
16th June 2009, 11:50
Hi spirit,
your test app:

closing first window not closing 2nd window.
but, closing 2nd window closing all child window(3rd window onwords).

why it is so?

spirit
16th June 2009, 11:54
if you create several windows with a parent and then close a parent window, then all windows will be closed too, but if you create several windows with parent and one (or several) windows without parent and then close a parent, then all windows wich have a parent will be close, but that windows which have not a parent will be stay opened.

rajesh
16th June 2009, 11:56
I have created several window with parent, but closing first parent not closing child window.
but closing 2nd window closing all child window.

I am using Qt4.3.1

spirit
16th June 2009, 12:00
probably, at first you create a window witout parent and then you pressed "Add with parent" button and created a window and so on. so, when you closed first window it was closed, but when you closed second one, automatically all children have been closed too.

rajesh
16th June 2009, 12:08
fisrt one also created by passing parent, but how this matters.
if I am closing 1st window it should close all child window, no matters 1st window having parent or not. it is matters of 2nd window having parent.

spirit
16th June 2009, 12:13
in main.cpp a new window is created, then when you click "Add without parent" and close first window the second will be opened. isn't it?

rajesh
16th June 2009, 12:16
I have never clicked on "Add without parent"

spirit
16th June 2009, 12:19
take a look at video, works fine.