C++ 入門指南 - 完成版的 GUIDemo

Encrypt 類別 (class) 為編碼功能的核心, EncryptDemo 則是用來在命令列測試 Encrypt , GUIDemo 則是運用 Encrypt 的 GUI 專案




我們接下來繼續把 GUIDemo 的功能完成,還有兩個按鈕 ClearCopy 沒有實作功能。我們打算用 clearContact() 建置 Clear 的功能,另外用 copyContact() 建置 Copy 的功能,因此要在 GUIDemo.h 加入這兩個函數成員 (function member) 的宣告,修改過後的 GUIDemo.h 如下
#ifndef GUIDEMO_H
#define GUIDEMO_H

#include <QWidget>
#include "Encrypt.h"

class QLabel;
class QLineEdit;
class QPushButton;

class GUIDemo : public QWidget {
    Q_OBJECT

public:
    GUIDemo(QWidget *parent = 0);
    QString s2q(const string &);  
    string q2s(const QString &);

public slots:
    void newEncrypt();
    void loadEncrypt();
    void saveEncrypt();
    void encodeContact();
    void decodeContact();
    void clearContact();
    void copyContact();
    void inputContact();

private:
    QLabel *display;
    QLineEdit *inputField;
    QLineEdit *outputField;
    QPushButton *newButton;
    QPushButton *loadButton;
    QPushButton *saveButton;
    QPushButton *encodeButton;
    QPushButton *decodeButton;
    QPushButton *clearButton;
    QPushButton *copyButton;
    
    QString inputText;
    QString outputText;    
    Encrypt *e;
};

#endif

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:GUIDemo.h
    功能:示範 C++ 程式 
    作者:張凱慶
    時間:西元 2012 年 10 月 */


clearContact() 很簡單,我們把 inputTextoutputTextinputFieldoutputField 都設定成空字串 (string) 就達到清空的目的,然後在 display 顯示提示訊息
void GUIDemo::clearContact() {
    inputText = "";
    outputText = "";
    inputField->setText("");
    outputField->setText("");
    
    display->setText("This is Clear button.");
}


而實作 copyContact() 需要用到 QClipboard ,因此 GUIDemo.cpp 的開頭要先引入這個程式庫 (library)
#include <QClipboard>


首先建立 QClipboard 的物件 (object) ,然後呼叫 setText() 並以 outputText 當參數 (parameter) ,這樣編碼結果就複製到系統剪貼簿了
void GUIDemo::copyContact() {
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setText(outputText);
    display->setText("This is Copy button. Result is copied to clipboard.");
}


完整的實作檔 GUIDemo.cpp 如下,記得要把 ClearCopy 兩個按鈕建立 connect
#include <QtGui>
#include "GUIDemo.h"
#include <QFile>
#include <QDataStream>
#include <QClipboard>

GUIDemo::GUIDemo(QWidget *parent) : QWidget(parent) {
    QLabel *input = new QLabel(tr("Input:"));
    QLabel *output = new QLabel(tr("Output:"));
    display = new QLabel(tr("something happened"));
    
    inputField = new QLineEdit;
    connect(inputField, SIGNAL(returnPressed()), this, SLOT(inputContact()));
    outputField = new QLineEdit;
    outputField->setReadOnly(true);
    
    newButton = new QPushButton(tr("New"));
    connect(newButton, SIGNAL(clicked()), this, SLOT(newEncrypt()));
    loadButton = new QPushButton(tr("Load"));
    connect(loadButton, SIGNAL(clicked()), this, SLOT(loadEncrypt()));
    saveButton = new QPushButton(tr("Save"));
    connect(saveButton, SIGNAL(clicked()), this, SLOT(saveEncrypt()));
    encodeButton = new QPushButton(tr("Encode"));
    connect(encodeButton, SIGNAL(clicked()), this, SLOT(encodeContact()));
    decodeButton = new QPushButton(tr("Decode"));
    connect(decodeButton, SIGNAL(clicked()), this, SLOT(decodeContact()));
    clearButton = new QPushButton(tr("Clear"));
    connect(clearButton, SIGNAL(clicked()), this, SLOT(clearContact()));
    copyButton = new QPushButton(tr("Copy"));
    connect(copyButton, SIGNAL(clicked()), this, SLOT(copyContact()));
    
    QGridLayout *layout = new QGridLayout;
    
    layout->addWidget(input, 0, 0);
    layout->addWidget(inputField, 0, 1, 1, 6, 0);
    
    layout->addWidget(output, 1, 0);
    layout->addWidget(outputField, 1, 1, 1, 6, 0);
    
    layout->addWidget(newButton, 2, 0);
    layout->addWidget(loadButton, 2, 1);
    layout->addWidget(saveButton, 2, 2);
    layout->addWidget(encodeButton, 2, 3);
    layout->addWidget(decodeButton, 2, 4);
    layout->addWidget(clearButton, 2, 5);
    layout->addWidget(copyButton, 2, 6);
    
    layout->addWidget(display, 3, 0, 1, 7, 0);
    
    setLayout(layout);
    setWindowTitle(tr("GUIDemo"));
    
    e = NULL;
}

void GUIDemo::newEncrypt() {
    e = new Encrypt;
    display->setText("This is New button. Encrypt code is " + s2q(e->getcArray()));
}

void GUIDemo::loadEncrypt() {
    QFile file("encryptor");
    if (file.open(QIODevice::ReadOnly)) {
        QDataStream in(&file);
        QString temp;
        in >> temp;
        
        if (e == NULL) {
            e = new Encrypt;
            e->setcArray(q2s(temp));
        }
        else {
            e->setcArray(q2s(temp));
        }
        
        display->setText("This is Load button. Encrypt object is loaded.");
    }
    else {
        display->setText("This is Load button. Encrypt object is not loaded.");
    }    
}

void GUIDemo::saveEncrypt() {
    if (e != NULL) {
        QFile file("encryptor");
        file.open(QIODevice::WriteOnly);
        QDataStream out(&file);
        out << s2q(e->getcArray());
        
        display->setText("This is Save button. Encrypt object is saved.");
    }
    else {
        display->setText("This is Save button. There is no Encrypt Object.");
    }
}

void GUIDemo::encodeContact() {
    inputText = inputField->text();
    
    if (inputText == "") {
        display->setText("This is Encode button. No input string!!");
    }
    else {
        if (e == NULL) {
            display->setText("This is Encode button. No Encrypt object!!");
        }
        else {
            outputText = s2q(e->toEncode(q2s(inputText)));
            outputField->setText(outputText);
            display->setText("This is Encode button. The result is above.");
        }
    }
}

void GUIDemo::decodeContact() {
    inputText = inputField->text();
    
    if (inputText == "") {
        display->setText("This is Encode button. No input string!!");
    }
    else {
        if (e == NULL) {
            display->setText("This is Encode button. No Encrypt object!!");
        }
        else {
            outputText = s2q(e->toDecode(q2s(inputText)));
            outputField->setText(outputText);
            display->setText("This is Decode button. The result is above.");
        }
    }
}

void GUIDemo::clearContact() {
    inputText = "";
    outputText = "";
    inputField->setText("");
    outputField->setText("");
    
    display->setText("This is Clear button.");
}

void GUIDemo::copyContact() {
    QClipboard *clipboard = QApplication::clipboard();
    clipboard->setText(outputText);
    display->setText("This is Copy button. Result is copied to clipboard.");
}

void GUIDemo::inputContact() {
    inputText = inputField->text();
    display->setText("Your input is '" + inputText + "'.");
}

QString GUIDemo::s2q(const string &s) {  
    return QString(QString::fromLocal8Bit(s.c_str()));  
}  

string GUIDemo::q2s(const QString &s) {  
    return string((const char *)s.toLocal8Bit());  
} 

/* 《程式語言教學誌》的範例程式
    http://pydoing.blogspot.com/
    檔名:GUIDemo.cpp
    功能:示範 C++ 程式 
    作者:張凱慶
    時間:西元 2012 年 10 月 */


重新編譯執行, Clear ok



Copy 也 ok



好了,我們已經學完開發 GUIDemo 了,下一步是?


中英文術語對照
類別class
函數成員function member
字串string
程式庫library
物件object
參數parameter


您可以繼續參考
GUI 篇


相關目錄
回 C++ 入門指南
回 C++ 教材目錄
回首頁


參考資料
Qt Developer Network

沒有留言: