上篇文章,我们只是把窗体界面做出来了,并且里面并未有实现任何功能,因此,我们现在来实现里面的功能。
面向对象的好处就是,我们通过简单的继承,创建一个新类,而不用修改已有的类,让该类同时从Qdialog和Ui::GoToCellDialog中继承出来,并且由它来实现那些缺少的功能(从而也证明了这句话:通过简单地增加另外一个间接层就可以解决软件的任何问题)。命名的惯例是:将该类与uic所生成的类具有相同的名字,只是没有Ui::前缀而已。
封装的GoToCellDialog类如下所示
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QtGui/QDialog>
#include "ui_gotocelldialog.h"
class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
Q_OBJECT
public:
GoToCellDialog(QDialog *parent = 0);
private slots:
void on_lineEdit_textChanged(); //自动将on_objectName_signalName命名惯例的任意槽与相应的objectName的signalName()信号连接在一起
};
#endif
#include <QtGui>
#include "gotocelldialog.h"
GoToCellDialog::GoToCellDialog(QDialog *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()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject));
}
void GoToCellDialog::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
main函数则修改为如下所示
#include <QtGui/QApplication>
#include <QtGui/QDialog>
#include "gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GoToCellDialog *dialog;
dialog->show();
return app.exec();
}
现在可以看到各个按键就有作用了