C++ 入門指南 - GUIDemo.cpp




GUIDemo.cpp 的程式原始碼如下

#include <QtGui>
#include "GUIDemo.h"
#include <QClipboard>
#include <QFile>
#include <QDataStream>

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);
    
    layout->addWidget(output, 1, 0);
    layout->addWidget(outputField, 1, 1, 1, 6);
    
    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);
    
    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.h
    功能:示範 C++ 程式 
    作者:張凱慶
    時間:西元 2012 年 10 月 */


您可以繼續參考
範例程式碼


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


參考資料
C++ reference
Qt Developer Network

沒有留言: