本站電子書

您可以在這些電子書店找到本站電子書: Google Play 圖書iBooks StoreHyReadReadmooPubu
===>>>本站推出學習遊戲意見調查<<<===《如何自學程式設計》入選 Google Play 台灣地區2016年度最佳書籍

C++ 入門指南 - 整合 Encrypt

GUIDemo 是的圖形介面使用者軟體,編碼、解碼的計算核心是由 Encrypt 來完成




簡單說,我們在 GUIDemo 中建立一個 Encrypt 型態的變數成員 (variable member) 即可
Encrypt *e;


另外我們替編碼結果建立一個儲存的變數成員
QString outputText;


還要加入新建 Encrypt 的 newEncrypt() ,以及處理編碼、解碼的 encodeContact() 、 decodeContact() ,然後把 Encrypt.h 與 Encrypt.cpp 加入 GUIDemo 專案中(資料夾)。修改過後的 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 encodeContact();
    void decodeContact();
    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 月 */


newEncrypt() 的功能很簡單,就是用 new 新建一個 Encrypt 物件 (object) 給 e ,然後在 display 顯示提示訊息
void GUIDemo::newEncrypt() {
    e = new Encrypt;
    display->setText("This is New button. Encrypt code is " + s2q(e->getcArray()));
}


encodeContact() 設計的稍微複雜一點,額外處理使用者沒有輸入文字或沒有建立 Encrypt 的情況,程式如下
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.");
        }
    }
}


decodeContact() 亦同,只有 toEncode() 換成 toDecode()
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.");
        }
    }
}


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

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"));
    saveButton = new QPushButton(tr("Save"));
    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"));
    copyButton = new QPushButton(tr("Copy"));
    
    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::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::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 月 */


因為加入新檔案,所以要重新 qmake 一下,然後再編譯執行囉! New 按鈕



Encode 按鈕



Decode 按鈕



好了,下面繼續介紹如何存檔與載入 Encrypt 物件哩!


中英文術語對照
變數成員variable member
物件object


您可以繼續參考
GUI 篇


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


參考資料
Qt Developer Network

沒有留言: