首页 > 代码库 > C++ GUI Qt4编程(05)-2.2GoToCell

C++ GUI Qt4编程(05)-2.2GoToCell

1. 使用Qt设计师创建GoToCell对话框。

2. gotocelldialog.cpp

 

 1 #include <QRegExp>
 2 
 3 #include "gotocelldialog.h"
 4 
 5 GoToCellDialog::GoToCellDialog(QWidget *parent)
 6     : QDialog(parent)
 7 {
 8     setupUi(this);        /*初始化窗体*/
 9     /*setupUi()函数会自动将符合on_objectName_signalName()命名惯例的任意槽
10        与相应的objectName的signalName()信号连接到一起。*/
11     
12     /*{0,2}中2前面不能有空格,有空格的话,lineEdit框中无法输入*/
13     QRegExp regExp("[A-Za-z][1-9][0-9]{0,2}");
14     /*QRegExpValidator检验器类*/
15     /*把this传递给QRegExpValidator的构造函数,使它成为GoToCellDialog对象的一个子对象*/
16     lineEdit->setValidator(new QRegExpValidator(regExp, this));
17     
18     connect(okButton, SIGNAL(clicked()), this, SLOT(accept()));
19     connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
20 }
21 
22 /*启用或禁用OK按钮*/
23 void GoToCellDialog::on_lineEdit_textChanged()
24 {
25     /*QLineEdit::hasAcceptableInput()会使用在构造函数中设置的检验器来判断行编辑器中内容的有效性*/
26     okButton->setEnabled(lineEdit->hasAcceptableInput());
27 }

 

3. gotocelldialog.h

 1 /**/
 2 #ifndef GOTOCELLDIALOG_H
 3 #define GOTOCELLDIALOG_H
 4 
 5 #include <QDialog>
 6 
 7 #include "ui_gotocelldialog.h"
 8 
 9 class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
10 {
11     Q_OBJECT
12     
13 public:
14     GoToCellDialog(QWidget *parent = 0);
15     
16 private slots:
17     void on_lineEdit_textChanged();
18 };
19 
20 #endif

4. main.cpp

 1 #include <QApplication>
 2 
 3 #include "gotocelldialog.h"
 4 
 5 int main(int argc, char *argv[])
 6 {
 7     QApplication app(argc, argv);
 8     
 9     GoToCellDialog *dialog = new GoToCellDialog;
10     dialog->show();
11     
12     return app.exec();
13 }
14 
15 #if 0
16 
17 #include <QApplication>
18 #include <QDialog>
19 
20 #include "ui_gotocelldialog.h"
21 
22 
23 int main(int argc, char *argv[])
24 {
25     QApplication app(argc, argv);
26     
27     Ui::GoToCellDialog ui;
28     QDialog *dialog = new QDialog;
29     ui.setupUi(dialog);
30     dialog->show();
31     
32     return app.exec();
33 }
34 
35 #endif

技术分享

 

C++ GUI Qt4编程(05)-2.2GoToCell