refine dialogs

Use QDialogButtonBox in all dialogs.
This commit is contained in:
Le Tan 2017-02-26 19:59:42 +08:00
parent c83fcf6ce9
commit 517d1ed139
14 changed files with 64 additions and 95 deletions

View File

@ -9,8 +9,6 @@ VDirInfoDialog::VDirInfoDialog(const QString &title, const QString &info,
setupUI();
connect(nameEdit, &QLineEdit::textChanged, this, &VDirInfoDialog::enableOkButton);
connect(okBtn, &QPushButton::clicked, this, &VDirInfoDialog::accept);
connect(cancelBtn, &QPushButton::clicked, this, &VDirInfoDialog::reject);
enableOkButton();
}
@ -25,19 +23,16 @@ void VDirInfoDialog::setupUI()
nameEdit->selectAll();
nameLabel->setBuddy(nameEdit);
okBtn = new QPushButton(tr("&OK"));
okBtn->setDefault(true);
cancelBtn = new QPushButton(tr("&Cancel"));
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QHBoxLayout *topLayout = new QHBoxLayout();
topLayout->addWidget(nameLabel);
topLayout->addWidget(nameEdit);
QHBoxLayout *btmLayout = new QHBoxLayout();
btmLayout->addStretch();
btmLayout->addWidget(okBtn);
btmLayout->addWidget(cancelBtn);
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
QVBoxLayout *mainLayout = new QVBoxLayout();
@ -45,7 +40,7 @@ void VDirInfoDialog::setupUI()
mainLayout->addWidget(infoLabel);
}
mainLayout->addLayout(topLayout);
mainLayout->addLayout(btmLayout);
mainLayout->addWidget(m_btnBox);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(mainLayout);
@ -54,6 +49,7 @@ void VDirInfoDialog::setupUI()
void VDirInfoDialog::enableOkButton()
{
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
okBtn->setEnabled(!nameEdit->text().isEmpty());
}

View File

@ -5,7 +5,7 @@
class QLabel;
class QLineEdit;
class QPushButton;
class QDialogButtonBox;
class QString;
class VDirInfoDialog : public QDialog
@ -25,8 +25,7 @@ private:
QLabel *infoLabel;
QLabel *nameLabel;
QLineEdit *nameEdit;
QPushButton *okBtn;
QPushButton *cancelBtn;
QDialogButtonBox *m_btnBox;
QString title;
QString info;

View File

@ -9,8 +9,6 @@ VFileInfoDialog::VFileInfoDialog(const QString &title, const QString &info,
setupUI();
connect(nameEdit, &QLineEdit::textChanged, this, &VFileInfoDialog::enableOkButton);
connect(okBtn, &QPushButton::clicked, this, &VFileInfoDialog::accept);
connect(cancelBtn, &QPushButton::clicked, this, &VFileInfoDialog::reject);
enableOkButton();
}
@ -25,19 +23,16 @@ void VFileInfoDialog::setupUI()
nameEdit->selectAll();
nameLabel->setBuddy(nameEdit);
okBtn = new QPushButton(tr("&OK"));
okBtn->setDefault(true);
cancelBtn = new QPushButton(tr("&Cancel"));
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QHBoxLayout *topLayout = new QHBoxLayout();
topLayout->addWidget(nameLabel);
topLayout->addWidget(nameEdit);
QHBoxLayout *btmLayout = new QHBoxLayout();
btmLayout->addStretch();
btmLayout->addWidget(okBtn);
btmLayout->addWidget(cancelBtn);
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
QVBoxLayout *mainLayout = new QVBoxLayout();
@ -45,7 +40,7 @@ void VFileInfoDialog::setupUI()
mainLayout->addWidget(infoLabel);
}
mainLayout->addLayout(topLayout);
mainLayout->addLayout(btmLayout);
mainLayout->addWidget(m_btnBox);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(mainLayout);
@ -54,6 +49,7 @@ void VFileInfoDialog::setupUI()
void VFileInfoDialog::enableOkButton()
{
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
okBtn->setEnabled(!nameEdit->text().isEmpty());
}

View File

@ -5,7 +5,7 @@
class QLabel;
class QLineEdit;
class QPushButton;
class QDialogButtonBox;
class QString;
class VFileInfoDialog : public QDialog
@ -25,8 +25,7 @@ private:
QLabel *infoLabel;
QLabel *nameLabel;
QLineEdit *nameEdit;
QPushButton *okBtn;
QPushButton *cancelBtn;
QDialogButtonBox *m_btnBox;
QString title;
QString info;

View File

@ -13,8 +13,6 @@ VInsertImageDialog::VInsertImageDialog(const QString &title, const QString &defa
connect(imageTitleEdit, &QLineEdit::textChanged, this, &VInsertImageDialog::enableOkButton);
connect(pathEdit, &QLineEdit::textChanged, this, &VInsertImageDialog::enableOkButton);
connect(browseBtn, &QPushButton::clicked, this, &VInsertImageDialog::handleBrowseBtnClicked);
connect(okBtn, &QPushButton::clicked, this, &VInsertImageDialog::accept);
connect(cancelBtn, &QPushButton::clicked, this, &VInsertImageDialog::reject);
enableOkButton();
}
@ -52,21 +50,17 @@ void VInsertImageDialog::setupUI()
topLayout->setColumnStretch(1, 1);
topLayout->setColumnStretch(2, 0);
okBtn = new QPushButton(tr("&OK"));
okBtn->setDefault(true);
cancelBtn = new QPushButton(tr("&Cancel"));
QHBoxLayout *btmLayout = new QHBoxLayout();
btmLayout->addStretch();
btmLayout->addWidget(okBtn);
btmLayout->addWidget(cancelBtn);
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
imagePreviewLabel = new QLabel();
imagePreviewLabel->setVisible(false);
QVBoxLayout *mainLayout = new QVBoxLayout();
mainLayout->addLayout(topLayout);
mainLayout->addLayout(btmLayout);
mainLayout->addWidget(m_btnBox);
mainLayout->addWidget(imagePreviewLabel);
setLayout(mainLayout);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
@ -79,6 +73,7 @@ void VInsertImageDialog::enableOkButton()
if (imageTitleEdit->text().isEmpty() || !image) {
enabled = false;
}
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
okBtn->setEnabled(enabled);
}

View File

@ -9,6 +9,7 @@
class QLabel;
class QLineEdit;
class QPushButton;
class QDialogButtonBox;
class VInsertImageDialog : public QDialog
{
@ -40,8 +41,7 @@ private:
QLabel *pathLabel;
QLineEdit *pathEdit;
QPushButton *browseBtn;
QPushButton *okBtn;
QPushButton *cancelBtn;
QDialogButtonBox *m_btnBox;
QLabel *imagePreviewLabel;
QString title;

View File

@ -8,8 +8,6 @@ VNewDirDialog::VNewDirDialog(const QString &title, const QString &info, const QS
setupUI();
connect(nameEdit, &QLineEdit::textChanged, this, &VNewDirDialog::enableOkButton);
connect(okBtn, &QPushButton::clicked, this, &VNewDirDialog::accept);
connect(cancelBtn, &QPushButton::clicked, this, &VNewDirDialog::reject);
}
void VNewDirDialog::setupUI()
@ -24,19 +22,16 @@ void VNewDirDialog::setupUI()
nameEdit->selectAll();
nameLabel->setBuddy(nameEdit);
okBtn = new QPushButton(tr("&OK"));
okBtn->setDefault(true);
cancelBtn = new QPushButton(tr("&Cancel"));
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QHBoxLayout *topLayout = new QHBoxLayout();
topLayout->addWidget(nameLabel);
topLayout->addWidget(nameEdit);
QHBoxLayout *btmLayout = new QHBoxLayout();
btmLayout->addStretch();
btmLayout->addWidget(okBtn);
btmLayout->addWidget(cancelBtn);
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
QVBoxLayout *mainLayout = new QVBoxLayout();
@ -44,14 +39,15 @@ void VNewDirDialog::setupUI()
mainLayout->addWidget(infoLabel);
}
mainLayout->addLayout(topLayout);
mainLayout->addLayout(btmLayout);
setLayout(mainLayout);
mainLayout->addWidget(m_btnBox);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(mainLayout);
setWindowTitle(title);
}
void VNewDirDialog::enableOkButton(const QString &editText)
{
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
okBtn->setEnabled(!editText.isEmpty());
}

View File

@ -5,7 +5,7 @@
class QLabel;
class QLineEdit;
class QPushButton;
class QDialogButtonBox;
class QString;
class VNewDirDialog : public QDialog
@ -24,8 +24,7 @@ private:
QLabel *nameLabel;
QLineEdit *nameEdit;
QPushButton *okBtn;
QPushButton *cancelBtn;
QDialogButtonBox *m_btnBox;
QString title;
QString info;

View File

@ -8,8 +8,6 @@ VNewFileDialog::VNewFileDialog(const QString &title, const QString &info, const
setupUI();
connect(nameEdit, &QLineEdit::textChanged, this, &VNewFileDialog::enableOkButton);
connect(okBtn, &QPushButton::clicked, this, &VNewFileDialog::accept);
connect(cancelBtn, &QPushButton::clicked, this, &VNewFileDialog::reject);
}
void VNewFileDialog::setupUI()
@ -24,19 +22,16 @@ void VNewFileDialog::setupUI()
nameEdit->selectAll();
nameLabel->setBuddy(nameEdit);
okBtn = new QPushButton(tr("&OK"));
okBtn->setDefault(true);
cancelBtn = new QPushButton(tr("&Cancel"));
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QHBoxLayout *topLayout = new QHBoxLayout();
topLayout->addWidget(nameLabel);
topLayout->addWidget(nameEdit);
QHBoxLayout *btmLayout = new QHBoxLayout();
btmLayout->addStretch();
btmLayout->addWidget(okBtn);
btmLayout->addWidget(cancelBtn);
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
nameEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
QVBoxLayout *mainLayout = new QVBoxLayout();
@ -44,14 +39,16 @@ void VNewFileDialog::setupUI()
mainLayout->addWidget(infoLabel);
}
mainLayout->addLayout(topLayout);
mainLayout->addLayout(btmLayout);
setLayout(mainLayout);
mainLayout->addWidget(m_btnBox);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setLayout(mainLayout);
setWindowTitle(title);
}
void VNewFileDialog::enableOkButton(const QString &editText)
{
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
okBtn->setEnabled(!editText.isEmpty());
}

View File

@ -5,7 +5,7 @@
class QLabel;
class QLineEdit;
class QPushButton;
class QDialogButtonBox;
class QString;
class VNewFileDialog : public QDialog
@ -25,7 +25,7 @@ private:
QLabel *nameLabel;
QLineEdit *nameEdit;
QPushButton *okBtn;
QPushButton *cancelBtn;
QDialogButtonBox *m_btnBox;
QString title;
QString info;

View File

@ -13,8 +13,6 @@ VNewNotebookDialog::VNewNotebookDialog(const QString &title, const QString &info
connect(nameEdit, &QLineEdit::textChanged, this, &VNewNotebookDialog::enableOkButton);
connect(pathEdit, &QLineEdit::textChanged, this, &VNewNotebookDialog::enableOkButton);
connect(browseBtn, &QPushButton::clicked, this, &VNewNotebookDialog::handleBrowseBtnClicked);
connect(okBtn, &QPushButton::clicked, this, &VNewNotebookDialog::accept);
connect(cancelBtn, &QPushButton::clicked, this, &VNewNotebookDialog::reject);
enableOkButton();
}
@ -45,20 +43,17 @@ void VNewNotebookDialog::setupUI()
topLayout->addWidget(browseBtn, 1, 2);
topLayout->addWidget(importCheck, 2, 1);
okBtn = new QPushButton(tr("&OK"));
okBtn->setDefault(true);
cancelBtn = new QPushButton(tr("&Cancel"));
QHBoxLayout *btmLayout = new QHBoxLayout();
btmLayout->addStretch();
btmLayout->addWidget(okBtn);
btmLayout->addWidget(cancelBtn);
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QVBoxLayout *mainLayout = new QVBoxLayout();
if (infoLabel) {
mainLayout->addWidget(infoLabel);
}
mainLayout->addLayout(topLayout);
mainLayout->addLayout(btmLayout);
mainLayout->addWidget(m_btnBox);
setLayout(mainLayout);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setWindowTitle(title);
@ -66,6 +61,7 @@ void VNewNotebookDialog::setupUI()
void VNewNotebookDialog::enableOkButton()
{
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
okBtn->setEnabled(!pathEdit->text().isEmpty() && !nameEdit->text().isEmpty());
}

View File

@ -8,6 +8,7 @@ class QLineEdit;
class QPushButton;
class QString;
class QCheckBox;
class QDialogButtonBox;
class VNewNotebookDialog : public QDialog
{
@ -35,8 +36,7 @@ private:
QLineEdit *pathEdit;
QCheckBox *importCheck;
QPushButton *browseBtn;
QPushButton *okBtn;
QPushButton *cancelBtn;
QDialogButtonBox *m_btnBox;
QString title;
QString info;

View File

@ -10,8 +10,6 @@ VNotebookInfoDialog::VNotebookInfoDialog(const QString &title, const QString &in
setupUI();
connect(nameEdit, &QLineEdit::textChanged, this, &VNotebookInfoDialog::enableOkButton);
connect(okBtn, &QPushButton::clicked, this, &VNotebookInfoDialog::accept);
connect(cancelBtn, &QPushButton::clicked, this, &VNotebookInfoDialog::reject);
enableOkButton();
}
@ -35,14 +33,12 @@ void VNotebookInfoDialog::setupUI()
topLayout->addRow(nameLabel, nameEdit);
topLayout->addRow(pathLabel, pathEdit);
okBtn = new QPushButton(tr("&OK"));
okBtn->setDefault(true);
cancelBtn = new QPushButton(tr("&Cancel"));
QHBoxLayout *btmLayout = new QHBoxLayout();
btmLayout->addStretch();
btmLayout->addWidget(okBtn);
btmLayout->addWidget(cancelBtn);
// Ok is the default button.
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(m_btnBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
pathEdit->setMinimumWidth(okBtn->sizeHint().width() * 3);
QVBoxLayout *mainLayout = new QVBoxLayout();
@ -50,7 +46,7 @@ void VNotebookInfoDialog::setupUI()
mainLayout->addWidget(infoLabel);
}
mainLayout->addLayout(topLayout);
mainLayout->addLayout(btmLayout);
mainLayout->addWidget(m_btnBox);
setLayout(mainLayout);
mainLayout->setSizeConstraint(QLayout::SetFixedSize);
setWindowTitle(title);
@ -58,6 +54,7 @@ void VNotebookInfoDialog::setupUI()
void VNotebookInfoDialog::enableOkButton()
{
QPushButton *okBtn = m_btnBox->button(QDialogButtonBox::Ok);
okBtn->setEnabled(!nameEdit->text().isEmpty());
}

View File

@ -5,7 +5,7 @@
class QLabel;
class QLineEdit;
class QPushButton;
class QDialogButtonBox;
class QString;
class VNotebookInfoDialog : public QDialog
@ -26,8 +26,7 @@ private:
QLabel *nameLabel;
QLineEdit *nameEdit;
QLineEdit *pathEdit;
QPushButton *okBtn;
QPushButton *cancelBtn;
QDialogButtonBox *m_btnBox;
QString title;
QString info;