与上例不一样,此例使用了类的方式来使用*.ui文件件:
使用了4个文件gotocelldialog.ui , gotocelldialog.h, gotocelldialog.cpp ,main.cpp,具体内容如下:
// gotocelldialog.h
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QDialog>
#include "ui_gotocelldialog.h"
//GoToCellDialog为新定义的类, 继承了*.ui文件中定义的类
class GoToCellDialog : public QDialog, public Ui::GoToCellDialog // 继承了*.ui文件中定义的类
{
Q_OBJECT // Q_OBJECT宏:支持能使用QT中的signal和slot机制
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()));
connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
}
void GoToCellDialog::on_lineEdit_textChanged()
{
okButton->setEnabled(lineEdit->hasAcceptableInput());
}
//main.cpp
#include <QApplication>
#include "gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
GoToCellDialog *dialog = new GoToCellDialog;
dialog->show();
return app.exec();
}
转载于:https://my.oschina.net/mingfu/blog/527603