gotocelldialog.h头文件
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QDialog>
class QLabel;
class QLineEdit;
class QPushButton;
class GoToCellDialog:public QDialog
{
 Q_OBJECT
 public:
 GoToCellDialog(QDialog *parent =0);
 private slots:
 void on_lineEdit_textChanged(); 
 
 private:
 QLabel *label;
 QLineEdit *lineEdit;
 QPushButton *okButton;
 QPushButton *cancelButton;
};
#endif
gotocelldialog.cpp源文件
#include <QtGui>
#include "gotocelldialog.h"
GoToCellDialog::GoToCellDialog(QDialog *parent)
 :QDialog(parent)
{
 label = new QLabel(tr("&Cell Location"));
 lineEdit = new QLineEdit;
 label->setBuddy(lineEdit);
 
 okButton = new QPushButton(tr("OK"));
 okButton->setEnabled(false);
 okButton->setDefault(true);
 
 cancelButton = new QPushButton(tr("Cancel"));
  
 QHBoxLayout *topLayout = new QHBoxLayout;
 topLayout->addWidget(label);
 topLayout->addWidget(lineEdit);
 
 QHBoxLayout *lowerLayout = new QHBoxLayout;
 lowerLayout->addStretch();
 lowerLayout->addWidget(okButton);
 lowerLayout->addWidget(cancelButton);
 
 QVBoxLayout *mainLayout = new QVBoxLayout;
 mainLayout->addLayout(topLayout);
 mainLayout->addLayout(lowerLayout);
 
 setLayout(mainLayout);
 setWindowTitle(tr("Go to Cell"));
 setFixedHeight(sizeHint().height()); 
 
 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()));
 connect(lineEdit,SIGNAL(textChanged(const QString &)),this,SLOT(on_lineEdit_textChanged()));
}
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;
 dialog.show();
 return app.exec();
}