快速设计对话框2
建立在前一篇的gotocelldialog.ui 上,通过创建一个新类,让对话框具有适当的功能,命名惯例是将该类与uic所生成的类具有相同的名字,只是没有Ui::前缀而已
源码
gotocelldialog.h
#ifndef GOTOCELLDIALOG_H #define GOTOCELLDIALOG_H #include <QDialog> #include "ui_gotocelldialog.h" class GoToCellDialog : public QDialog, public Ui::GoToCellDialog { Q_OBJECT public: GoToCellDialog(QWidget *parent = 0); private slots: void on_lineEdit_textChanged(); }; #endif
gotocelldialog.cpp
#include <QtGui> #include "gotocelldialog.h" GoToCellDialog::GoToCellDialog(QWidget *parent) : QDialog(parent) { setupUi(this); //初始化窗体 QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); lineEdit->setValidator(new QRegExpValidator(regExp, this)); connect(okButton, SIGNAL(clicked()), this, SLOT(accept())); //返回的结果变量设置为Qdialog::Accepted(1) connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject())); //返回的结果变量设置为Qdialog::Rejected(0) } void GoToCellDialog::on_lineEdit_textChanged() { okButton->setEnabled(lineEdit->hasAcceptableInput()); }
main.c
#include <QApplication> #include "gotocelldialog.h" int main(int argc, char *argv[]) { QApplication app(argc, argv); GoToCellDialog *dialog = new GoToCellDialog; dialog->show(); return app.exec(); }
运行效果:
附加小知识:
1:创建用户借口后,setupUi()函数还会自动将那些符合on_objectName_signalName()命名惯例的任意槽与相应的objectName的signaleName()信号连接在一起
2:Qt提供了三个内置检验器:QIntValidator QDoubleValidator QRegExpValidator
3: Qt的父-子对象机制是在QObject中实现的,父对象会把这个子对象添加到自己的子对象列表中,当删除这个父对象时,它会遍历子对象列表并且删除每一个子对象
4:需要明确删除的对象是那些使用new创建的并且没有父对象的对象