*Moderate dig*
The problem with calling deleteLater() on the QDialog or deleting it after exec() is, that for a short moment the dialog will freeze and then disappear when clicking a button that causes the function to return, instead of just disappearing normally (or fade-away animation under Vista/W7). That simply is not nice.
I have made a dirty workaround which does not take the lag from the main event loop, but at least lets the animation play smoothly:
dialogdestroyer.h:
#ifndef DIALOGDESTROYER_H
#define DIALOGDESTROYER_H
#include <QThread>
#include <QDialog>
class DialogDestroyer
: public QThread{
public:
DialogDestroyer();
void DelayedDestruction
(QDialog* lDialog
);
private:
void run();
};
#endif // DIALOGDESTROYER_H
#ifndef DIALOGDESTROYER_H
#define DIALOGDESTROYER_H
#include <QThread>
#include <QDialog>
class DialogDestroyer : public QThread
{
public:
DialogDestroyer();
void DelayedDestruction(QDialog* lDialog);
private:
void run();
QDialog* mDialog;
};
#endif // DIALOGDESTROYER_H
To copy to clipboard, switch view to plain text mode
dialogdestroyer.cpp:
#include <QTimer>
#include "dialogdestroyer.h"
DialogDestroyer::DialogDestroyer() {
}
void DialogDestroyer
::DelayedDestruction(QDialog* lDialog
) {
mDialog = lDialog;
this->start();
}
void DialogDestroyer::run() {
msleep(300);
mDialog->deleteLater();
this->deleteLater();
}
#include <QTimer>
#include "dialogdestroyer.h"
DialogDestroyer::DialogDestroyer() {
}
void DialogDestroyer::DelayedDestruction(QDialog* lDialog) {
mDialog = lDialog;
this->start();
}
void DialogDestroyer::run() {
msleep(300);
mDialog->deleteLater();
this->deleteLater();
}
To copy to clipboard, switch view to plain text mode
Somewhere:
void MainWindow::on_actionOpen_triggered() {
int lResult = lDiag->exec();
// Do stuff...
}
(new DialogDestroyer())->DelayedDestruction(lDiag);
}
void MainWindow::on_actionOpen_triggered() {
QFileDialog* lDiag = new QFileDialog(this, QString("Select file to open"),
QString("D:\\"), QString("Text files (*.txt)"));
int lResult = lDiag->exec();
if(lResult != QDialog::Rejected) {
// Do stuff...
}
(new DialogDestroyer())->DelayedDestruction(lDiag);
}
To copy to clipboard, switch view to plain text mode
There should be a better way/implementation, in my honest opinion.
Bookmarks