代码如下(示例):
#ifndef GOTOCELL_H #define GOTOCELL_H #include#include class QPushButton; class QLabel; class QLineEdit; class GoToCell : public QDialog { Q_OBJECT public: explicit GoToCell(QWidget *parent = 0); private slots: void slotTextChanged(); private: QPushButton * mPtr_ok; QPushButton * mPtr_cancel; QLabel * mPtr_label; QLineEdit * mPtr_lineEdit; }; #endif // GOTOCELL_H
代码如下(示例):
#include "go_to_cell.h" #include#include #include #include #include #include GoToCell::GoToCell(QWidget *parent):QDialog(parent) { this->setWindowTitle(tr("Go To Cell")); mPtr_ok = new QPushButton(tr("OK")); mPtr_cancel = new QPushButton(tr("Cancel")); mPtr_label = new QLabel(tr("&Cell Location")); //正则 QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}"); mPtr_lineEdit = new QLineEdit; //lineEdit 添加内置检验器 mPtr_lineEdit->setValidator(new QRegExpValidator(regExp,this)); mPtr_label->setBuddy(mPtr_lineEdit); QHBoxLayout * top = new QHBoxLayout; top->addWidget(mPtr_label); top->addWidget(mPtr_lineEdit); QHBoxLayout * bottom = new QHBoxLayout; bottom->addStretch(); bottom->addWidget(mPtr_ok); bottom->addWidget(mPtr_cancel); QVBoxLayout * main = new QVBoxLayout; main->addLayout(top); main->addLayout(bottom); this->setLayout(main); connect(mPtr_ok,SIGNAL(clicked()),this,SLOT(accept())); connect(mPtr_cancel,SIGNAL(clicked()),this,SLOT(reject())); connect(mPtr_lineEdit,SIGNAL(textChanged(QString)),this,SLOT(slotTextChanged())); } void GoToCell::slotTextChanged() { mPtr_ok->setEnabled(mPtr_lineEdit->hasAcceptableInput()); }



