我的航拍直升機 控制基站軟體的編寫曆程(2.1)

來源:互聯網
上載者:User

    這幾天,由於幾門考試,再加上挑戰杯全國總決賽的事,我不得不將活停了下來!現在煩心的事基本都過去了,繼續!以前考試對我來說不重要,現在不一樣了,已經保研了,如果掛一門的話,就竹籃打水一場空了!

     在用QTCreator開發程式時,工程目錄不能有空格和漢字.

  QT的版本管理:

QT_creator 允許同時安裝多個版本,並且很容易的進行版本切換。

Qt Creator 可以自動選擇在系統內容變中的版本,我們如果覺得系統內容變數中的QT版本已經合適了,就不用做任何版本切換操作。否則,可以添加其他版本,通過Tools -> Options... -> Qt Versions。如果,在windows平台上用MinGW來編譯QT,就需要告訴Qt Creator MinGW的目錄。同過Tools -> Options... -> Qt4 -> Qt Versions -> MinGw Directory來設定MinGW的目錄。

  一旦在windows上安裝Qt Creator,安裝程式會自動將系統內容變數配置好,一般不用再手動設定系統內容變數。如果系統內容變數中沒有設定QT的變數,可以同過Tools ->Options來設定系統內容變數。

     建立預設的工程,需要預設的Qt版本進行編譯。

 

 

寫一個簡單的qt常式——文本尋找常式(Text Finder):

第一步:建立新工程

      建立一個Qt4 Gui Application,選擇QWidget作為Text Finder的基類。在類名中輸入textfinder,點擊確定。最後產生5個檔案,分別是:

textfinder.pro

textfinder.ui

textfinder.h

textfinder.cpp

main.cpp

 

第二步:設計介面,並產生相應的代碼,並實現尋找功能

       2.1  設計使用者介面(在這裡提一下,我私人覺得QT的介面比MFC的好多了!)

     在工程瀏覽視窗中雙擊textfinder.ui,就會彈出介面設計視窗。

        

      如,用一個QLabel,QLineEdit取名lineEdit,QPushButon取名findButton,QTextEdit取名textEdit。用一個QGridLayout來布局QLabel,QLineEdit和QTextEdit。用一個QVBoxLayout將QTextEdit和QGridLayout布局好。

      設計完之後如所示(怎麼樣?是比MFC強多了吧!呵呵):

                   

 

      接下來處理標頭檔,textfind.h檔案中已經有一些必須的代碼,例如,includes,a constructor,a destructor,及Ui對象等。我們需要添加私人的槽(slot), on_findButton_clicked()來處理find操作。還要添加一個私人的函數loadTextFile()來讀和顯示QTextEdit的內容。代碼如下:

  private slots:
        void on_findButton_clicked();

  private:
        Ui::TextFinder *ui;
        void loadTextFile();

2.3  處理源檔案

      我們首先將textfinder.cpp中的loadTextFile()函數實現,程式碼片段如下:

   void TextFinder::loadTextFile()
    {
        QFile inputFile(":/input.txt");
        inputFile.open(QIODevice::ReadOnly);

        QTextStream in(&inputFile);
        QString line = in.readAll();
        inputFile.close();

        ui->textEdit->setPlainText(line);
        QTextCursor cursor = ui->textEdit->textCursor();
        cursor.movePosition(QTextCursor::Start, QTextCursor::MoveAnchor, 1);
    }

上面代碼中我們開啟文字檔用到了QFile,讀文本用到了QTextStream,將文本顯示到textEdit上用到了setPlainText().所以在textfinder.cpp中要包含QFile和QTextStream,如下所示

#include <QtCore/QFile>
#include <QtCore/QTextStream>

 

2.4 對槽的處理

     在槽 on_findButton_clicked()中,我們用find()函數尋找要找的字串。程式碼片段如下:

   void TextFinder::on_findButton_clicked()
    {
        QString searchString = ui->lineEdit->text();
        ui->textEdit->find(searchString, QTextDocument::FindWholeWords);
    }

 

最後,我們完成了以上的函數之後,需要在建構函式中調用loadTextFile(),代碼如下所示:

TextFinder::TextFinder(QWidget *parent)
        : QWidget(parent), ui(new Ui::TextFinder)
    {
        ui->setupUi(this);
        loadTextFile();
    }

 

     對於槽的使用,需要一個ui_textfinder.h檔案,這個檔案是編譯時間介面檔案產生的,在編譯之前是沒有這個檔案的,並且textfinder.cpp中 include"ui_textfinder.h"也提示找不到 ui_textfinder.h檔案。當編譯之後,產生了ui_textfinder.h時,這個問題就消失了!

第三步:添加資源檔

     到上面,編譯可以通過,但是編譯完之後不能運行,提示"Exited with code 0."。估計就是由沒有添加文字檔“input.txt”造成的。

        接下來就逐步添加資源檔:

     單擊 file->new->Resource file->OK. 在name欄中輸入"textfinder.qrc",

  

   

  單擊"next",

單擊"Finish" 。

在工程視窗中雙擊 “textfinder.pro”, 單擊右側的“Add”->"Add Prefix"。設定好prefix的目錄,然後單擊“Add”->"Add Files",添加相應的資源檔就可以了。

 

第四步:編譯運行

 

完工!!!

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.