C++ 入門指南 - GUIDemo.cpp




GUIDemo.cpp 的程式原始碼如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#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()); 
}
 
/* 《程式語言教學誌》的範例程式
    檔名:GUIDemo.h
    功能:示範 C++ 程式
    作者:張凱慶
    時間:西元 2012 年 10 月 */


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


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


參考資料
C++ reference
Qt Developer Network

沒有留言: