• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            千張筆記

            Email:rain_qian830@163.com
            posts - 28, comments - 42, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            QT筆記(2)-Qt Designer的使用

            Posted on 2010-07-20 20:37 千張 閱讀(7276) 評論(1)  編輯 收藏 引用 所屬分類: QT學(xué)習(xí)
            這幾天在看《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(26082);
                    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(4020, 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 命令可運行程序



            Feedback

            # re: QT筆記(2)-Qt Designer的使用  回復(fù)  更多評論   

            2012-03-02 17:15 by 張永興
            你好,我是剛學(xué)習(xí)Qt的,是在fedora下安裝的,我可以問問你一些問題嗎?
            99热热久久这里只有精品68| 国内精品九九久久久精品| 国内精品久久久久伊人av| 99久久综合国产精品免费| 亚洲国产精品无码成人片久久| 久久免费99精品国产自在现线| 99久久精品影院老鸭窝| 热re99久久6国产精品免费| 国产精品久久久久久五月尺| 国产99久久久国产精品小说| 久久久久久亚洲精品无码| 久久亚洲中文字幕精品一区| 久久亚洲AV无码西西人体| 亚洲国产成人久久笫一页| 四虎国产精品成人免费久久| 亚洲综合久久久| 日韩精品久久久久久久电影蜜臀| 久久精品国产亚洲AV麻豆网站| 久久大香香蕉国产| 日本精品久久久久中文字幕| 91精品免费久久久久久久久| 免费精品久久久久久中文字幕| 狠狠色丁香婷婷久久综合五月 | av国内精品久久久久影院| 四虎国产永久免费久久| 久久久久综合国产欧美一区二区| 亚洲国产一成久久精品国产成人综合 | 人妻精品久久无码区| 91久久精品国产免费直播| 久久久久久久综合狠狠综合| 香蕉久久夜色精品升级完成| 久久综合中文字幕| 欧美国产成人久久精品| 久久九九青青国产精品| 婷婷久久综合九色综合绿巨人| 久久久亚洲欧洲日产国码aⅴ| 国产午夜精品理论片久久 | 久久天天躁狠狠躁夜夜不卡| 久久天堂电影网| 中文字幕久久波多野结衣av| 久久91精品综合国产首页|