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 <shawn.rutledge@digia.com>
This commit is contained in:
Friedemann Kleint 2013-01-16 15:12:29 +01:00 committed by The Qt Project
parent 5d2bb24cc9
commit 34572dadc6
2 changed files with 36 additions and 6 deletions

View File

@ -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

View File

@ -44,7 +44,9 @@
#include <QGroupBox>
#include <QFileDialog>
#include <QPointer>
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<LabelLineEdit *> m_labelLineEdits;
QPlainTextEdit *m_nameFilters;
QLineEdit *m_selectedNameFilter;
QPushButton *m_deleteNonModalDialogButton;
QString m_result;
QPointer<QFileDialog> m_nonModalDialog;
};
#endif // FILEDIALOGPANEL_H