日志
2.3 快速设计对话框
2015-11-27 09:59
//UIGoToCellDialog.h
#ifndef UIGOTOCELLDIALOG_H #define UIGOTOCELLDIALOG_H #include <QDialog> #include "ui_UIGoToCellDialog.h" //多继承 class UIGoToCellDialog : public QDialog, public Ui::UIGoToCellDialog { Q_OBJECT public: explicit UIGoToCellDialog(QWidget *parent = 0); signals: public slots: void slotInputChanged(); }; #endif // UIGOTOCELLDIALOG_H //UIGoToCellDialog.cpp #include <QtGui> #include "UIGoToCellDialog.h" UIGoToCellDialog::UIGoToCellDialog(QWidget *parent) : QDialog(parent) { setupUi(this); setWindowTitle(tr("GoToCellDialog")); m_pOKBtn->setEnabled(false); //正则表达式 QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); m_pInputEdit->setValidator(new QRegExpValidator(regExp, this)); //accept()和reject()都可以关闭对话框 //可以通过判断 QDialog::exec() 的返回值获取到用户按下的按钮 //accept() 返回 QDialog::Accepted 值为 1 //reject() 返回 QDialog::Rejected 值为 0 connect(m_pOKBtn, SIGNAL(clicked()), this, SLOT(accept())); connect(m_pCancelBtn, SIGNAL(clicked()), this, SLOT(reject())); connect(m_pInputEdit, SIGNAL(textChanged(QString)), this, SLOT(slotInputChanged())); } void UIGoToCellDialog::slotInputChanged() { //hasAcceptableInput() 使用构造函数中创建的检验器对输入值进行检测 m_pOKBtn->setEnabled(m_pInputEdit->hasAcceptableInput()); } #include <QApplication> #include "UIGoToCellDialog.h" #include <QDebug> int main(int argc, char** argv) { QApplication app(argc, argv); UIGoToCellDialog *pDialog = new UIGoToCellDialog; int rel = pDialog->exec(); if (rel == QDialog::Accepted) { qDebug()<<"accept"; } else if (rel == QDialog::Rejected) { qDebug()<<"rejected"; } app.quit(); return app.exec(); } //UIGoToCellDialog.ui <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>UIGoToCellDialog</class> <widget class="QDialog" name="UIGoToCellDialog"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>294</width> <height>71</height> </rect> </property> <property name="windowTitle"> <string>Dialog</string> </property> <layout class="QVBoxLayout" name="verticalLayout"> <item> <layout class="QHBoxLayout" name="horizontalLayout"> <item> <widget class="QLabel" name="label"> <property name="text"> <string>&Cell Location:</string> </property> </widget> </item> <item> <widget class="QLineEdit" name="m_pInputEdit"/> </item> </layout> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_2"> <item> <spacer name="horizontalSpacer"> <property name="orientation"> <enum>Qt::Horizontal</enum> </property> <property name="sizeHint" stdset="0"> <size> <width>40</width> <height>20</height> </size> </property> </spacer> </item> <item> <widget class="QPushButton" name="m_pOKBtn"> <property name="text"> <string>OK</string> </property> </widget> </item> <item> <widget class="QPushButton" name="m_pCancelBtn"> <property name="text"> <string>Cancel</string> </property> </widget> </item> </layout> </item> </layout> </widget> <resources/> <connections/> </ui> 以上效果也可以使用QDialogButtonBox实现 |
下一篇: 2.4.1 可扩展对话框
上一篇: 2.2 深入介绍信号和槽