這幾天在看《C++ GUI Qt 4編程》第二版,看到第二章,用到Qt Designer部分,有點迷糊,書的版本可能有點老,命令方式的編譯運行弄得我頭大,本想先不看這部分,可是第三章要用到這里的兩個對話框,不得不硬著頭皮看下去。
最后先一步步按著書上的步驟實現(xiàn),最后想想,怎么沒有用到Qt Creator呢,再自己摸索了下,終于知道怎么在Qt Creator中怎么實現(xiàn)了,現(xiàn)在看起來很簡單,卻讓我這個新手搗鼓了半天。
就拿第二章中的gotocell作為例子吧。
第一步:打開Qt Creator -> File -> New file or Project -> Empty Qt4 Project,工程名(Name)設(shè)為gotocell,點確定(Finish)后,會生成一個空工程文件gotocell.pro。
第二步:在左視圖project區(qū)域中,可以看到gotocell工程,在gotocell上點右鍵,選擇Add New...,新建一個Qt Designer Form and Class(或者Qt Designer Form,gotocelldialog.h和gotocelldialog.cpp可以自己添加),名字名為gotocelldialog.ui,會相應(yīng)地生成Form類文件gotocelldialog.h和gotocelldialog.cpp。gotocelldialog.ui的設(shè)計就按書上的步驟來做,gotocelldialog.h和gotocelldialog.cpp中的內(nèi)容如下(與書上一樣):
gotocelldialog.h
#ifndef GOTOCELLDIALOG_H
#define GOTOCELLDIALOG_H
#include <QDialog>
#include "ui_gotocelldialog.h"
class GoToCellDialog : public QDialog, public Ui::GoToCellDialog
{
Q_OBJECT
public:
GoToCellDialog(QWidget *parent = 0);
private slots:
void on_lineEdit_textChanged();
};
#endif // GOTOCELLDIALOG_H
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());
}
然后再Add New ->C++ Source File,添加main.cpp文件,main.cpp文件內(nèi)容如下:
#include <QApplication>
#include <QDialog>
#include "gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
GoToCellDialog *dialog = new GoToCellDialog;
dialog->show();
return app.exec();
}
然后Build -> Run,就可以運行了。
ui_gotocelldialog.h文件在編譯后自動生成,該文件與gotocelldialog.ui相對應(yīng),定義了ui上的窗口部件,以及對窗口部件初始化,具體內(nèi)容如下:
/********************************************************************************
** Form generated from reading UI file 'gotocelldialog.ui'
**
** Created: Tue Jul 20 12:00:07 2010
** by: Qt User Interface Compiler version 4.6.2
**
** WARNING! All changes made in this file will be lost when recompiling UI file!
********************************************************************************/
#ifndef UI_GOTOCELLDIALOG_H
#define UI_GOTOCELLDIALOG_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QtGui/QSpacerItem>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
QT_BEGIN_NAMESPACE
class Ui_GoToCellDialog
{
public:
QVBoxLayout *verticalLayout;
QHBoxLayout *horizontalLayout;
QLabel *label;
QLineEdit *lineEdit;
QHBoxLayout *horizontalLayout_2;
QSpacerItem *horizontalSpacer;
QPushButton *okButton;
QPushButton *cancelButton;
void setupUi(QWidget *GoToCellDialog)
{
if (GoToCellDialog->objectName().isEmpty())
GoToCellDialog->setObjectName(QString::fromUtf8("GoToCellDialog"));
GoToCellDialog->resize(260, 82);
verticalLayout = new QVBoxLayout(GoToCellDialog);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
horizontalLayout = new QHBoxLayout();
horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
label = new QLabel(GoToCellDialog);
label->setObjectName(QString::fromUtf8("label"));
horizontalLayout->addWidget(label);
lineEdit = new QLineEdit(GoToCellDialog);
lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
horizontalLayout->addWidget(lineEdit);
verticalLayout->addLayout(horizontalLayout);
horizontalLayout_2 = new QHBoxLayout();
horizontalLayout_2->setObjectName(QString::fromUtf8("horizontalLayout_2"));
horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
horizontalLayout_2->addItem(horizontalSpacer);
okButton = new QPushButton(GoToCellDialog);
okButton->setObjectName(QString::fromUtf8("okButton"));
okButton->setEnabled(false);
okButton->setDefault(true);
horizontalLayout_2->addWidget(okButton);
cancelButton = new QPushButton(GoToCellDialog);
cancelButton->setObjectName(QString::fromUtf8("cancelButton"));
horizontalLayout_2->addWidget(cancelButton);
verticalLayout->addLayout(horizontalLayout_2);
#ifndef QT_NO_SHORTCUT
label->setBuddy(lineEdit);
#endif // QT_NO_SHORTCUT
retranslateUi(GoToCellDialog);
QMetaObject::connectSlotsByName(GoToCellDialog);
} // setupUi
void retranslateUi(QWidget *GoToCellDialog)
{
GoToCellDialog->setWindowTitle(QApplication::translate("GoToCellDialog", "Go to Cell", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("GoToCellDialog", "&Cell Location:", 0, QApplication::UnicodeUTF8));
okButton->setText(QApplication::translate("GoToCellDialog", "OK", 0, QApplication::UnicodeUTF8));
cancelButton->setText(QApplication::translate("GoToCellDialog", "Cancel", 0, QApplication::UnicodeUTF8));
} // retranslateUi
};
namespace Ui {
class GoToCellDialog: public Ui_GoToCellDialog {};
} // namespace Ui
QT_END_NAMESPACE
#endif // UI_GOTOCELLDIALOG_H
其中
namespace Ui
{
class GoToCellDialog: public Ui_GoToCellDialog {};
}
為命名空間,在Ui空間中生成一個類GoToCellDialog,繼承自Ui_GoToCellDialog,可以看到這個類名GoToCellDialog與我們自己在gotocelldialog.h中定義的類名相同,在這里定義命名空間的作用就是為了區(qū)分Ui類和我們自己定義的類。
書中還有一句很重要的話:由于多重繼承關(guān)系,可以直接訪問Ui::GoToCellDialog中的成員。創(chuàng)建了用戶接口后,setupUi()函數(shù)還會自動將那些符合on_objectName_signalName()命名慣例的任意槽與相應(yīng)的objectName的signalName()信號連接在一起。在這個例子中,就意味著setupUi()函數(shù)將建立如下所示的信號-槽連接關(guān)系:
connect(lineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(on_lineEdit_textChanged()));最后附上今天學(xué)到的幾個命令:
1> 在Linux命令提示中,用cd命令來改變當(dāng)前目錄。這是cd命令的一些基本用法:
1. 改變你的根路徑,鍵入cd,按回車鍵。
2. 進(jìn)入一個子目錄,鍵入cd,空格,然后是子路徑名(例如:cd Documents),再按回車鍵。
3. 進(jìn)入當(dāng)前目錄的上一級目錄,鍵入cd,空格,兩個點,然后按回車鍵。
4. 進(jìn)入一個特定的目錄,鍵入cd,空格,路徑名(例如 cd /usr/local/lib),再按回車鍵。
5. 為了確定你所在的目錄,你可以鍵入pwd,按回車鍵,你將看到你所在的當(dāng)前目錄名稱。
2> Qt相關(guān)命令
用cd命令進(jìn)入到當(dāng)前目錄后:
qmake -project 命令可生成一個與平臺無關(guān)的項目文件,此處為gotocell.pro
qmake gotocell.pro 命令可生成一個與平臺相關(guān)的makefile文件
make 命令可構(gòu)建(Build)該程序,生成可執(zhí)行文件
make clean 命令清理掉編譯產(chǎn)生的內(nèi)容,因為make命令執(zhí)行前,要先檢查是否有編譯后的文件存在,若存在,則不編譯。
./gotocell 命令可運行程序