1. 程式人生 > >C++ GUI Qt4編程(05)-2.2GoToCell

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

初始化 new pub 對話 argc color text qt4 dial

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