From 34572dadc6a4efce87a639f38a989a3117c8ca9f Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 16 Jan 2013 15:12:29 +0100 Subject: [PATCH] Dialog testing tool: Control life cycle of non-modal file dialogs. Keep the non-modal file dialog around and delete only on request, such that one can simulate repeated invocations of show() on the same dialog for testing native dialogs. Change-Id: I80d0f1dfafbc02a31be192098121654a01025174 Reviewed-by: Shawn Rutledge --- tests/manual/dialogs/filedialogpanel.cpp | 34 +++++++++++++++++++----- tests/manual/dialogs/filedialogpanel.h | 8 ++++++ 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/tests/manual/dialogs/filedialogpanel.cpp b/tests/manual/dialogs/filedialogpanel.cpp index f33c27f502..70e01b1203 100644 --- a/tests/manual/dialogs/filedialogpanel.cpp +++ b/tests/manual/dialogs/filedialogpanel.cpp @@ -105,11 +105,12 @@ inline void setComboBoxValue(QComboBox *c, int v) c->setCurrentIndex(c->findData(QVariant(v))); } -static inline void addButton(const QString &description, QBoxLayout *layout, QObject *receiver, const char *slotFunc) +static inline QPushButton *addButton(const QString &description, QBoxLayout *layout, QObject *receiver, const char *slotFunc) { QPushButton *button = new QPushButton(description); QObject::connect(button, SIGNAL(clicked()), receiver, slotFunc); layout->addWidget(button); + return button; } // A line edit for editing the label fields of the dialog, keeping track of whether it has @@ -159,6 +160,7 @@ FileDialogPanel::FileDialogPanel(QWidget *parent) , m_selectedFileName(new QLineEdit(this)) , m_nameFilters(new QPlainTextEdit) , m_selectedNameFilter(new QLineEdit(this)) + , m_deleteNonModalDialogButton(0) { // Options QGroupBox *optionsGroupBox = new QGroupBox(tr("Options")); @@ -201,6 +203,8 @@ FileDialogPanel::FileDialogPanel(QWidget *parent) buttonLayout->addSpacerItem(new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::MinimumExpanding)); addButton(tr("Show modal"), buttonLayout, this, SLOT(showModal())); addButton(tr("Show non-modal"), buttonLayout, this, SLOT(showNonModal())); + m_deleteNonModalDialogButton = + addButton(tr("Delete non-modal"), buttonLayout, this, SLOT(deleteNonModalDialog())); addButton(tr("getOpenFileName"), buttonLayout, this, SLOT(getOpenFileName())); addButton(tr("getOpenFileNames"), buttonLayout, this, SLOT(getOpenFileNames())); addButton(tr("getSaveFileName"), buttonLayout, this, SLOT(getSaveFileName())); @@ -218,6 +222,7 @@ FileDialogPanel::FileDialogPanel(QWidget *parent) gridLayout->addWidget(labelsGroupBox, 1, 0); gridLayout->addWidget(buttonsGroupBox, 1, 1); + enableDeleteNonModalDialogButton(); restoreDefaults(); } @@ -231,11 +236,28 @@ void FileDialogPanel::showModal() void FileDialogPanel::showNonModal() { - QFileDialog *dialog = new QFileDialog(this); - dialog->setAttribute(Qt::WA_DeleteOnClose); - applySettings(dialog); - dialog->setWindowTitle(tr("Non-Modal File Dialog Qt %1").arg(QLatin1String(QT_VERSION_STR))); - dialog->show(); + if (m_nonModalDialog.isNull()) { + static int n = 0; + m_nonModalDialog = new QFileDialog(this); + m_nonModalDialog->setWindowTitle(tr("Non-Modal File Dialog #%1 Qt %2") + .arg(++n) + .arg(QLatin1String(QT_VERSION_STR))); + enableDeleteNonModalDialogButton(); + } + applySettings(m_nonModalDialog); + m_nonModalDialog->show(); +} + +void FileDialogPanel::deleteNonModalDialog() +{ + if (!m_nonModalDialog.isNull()) + delete m_nonModalDialog; + enableDeleteNonModalDialogButton(); +} + +void FileDialogPanel::enableDeleteNonModalDialogButton() +{ + m_deleteNonModalDialogButton->setEnabled(!m_nonModalDialog.isNull()); } QString FileDialogPanel::filterString() const diff --git a/tests/manual/dialogs/filedialogpanel.h b/tests/manual/dialogs/filedialogpanel.h index bc8b66535b..a0f746e6d0 100644 --- a/tests/manual/dialogs/filedialogpanel.h +++ b/tests/manual/dialogs/filedialogpanel.h @@ -44,7 +44,9 @@ #include #include +#include +class QPushButton; class QCheckBox; class QComboBox; class QLineEdit; @@ -60,6 +62,7 @@ public: public slots: void showModal(); void showNonModal(); + void deleteNonModalDialog(); void getOpenFileNames(); void getOpenFileName(); void getSaveFileName(); @@ -68,6 +71,9 @@ public slots: void showAcceptedResult(); void restoreDefaults(); +private slots: + void enableDeleteNonModalDialogButton(); + private: QString filterString() const; QFileDialog::Options options() const; @@ -87,7 +93,9 @@ private: QList m_labelLineEdits; QPlainTextEdit *m_nameFilters; QLineEdit *m_selectedNameFilter; + QPushButton *m_deleteNonModalDialogButton; QString m_result; + QPointer m_nonModalDialog; }; #endif // FILEDIALOGPANEL_H