[继续疼][这区还没有Qt程序吧]Woo咚加密 Qt版
好吧我承认W的算法已经被我拿来试刀各种编程环境了……
用过Qt的应该或多或少知道,工程中的文件存放的路径是不能包括中文和空格的。
该程序可以用来了解编写含MainWindow的Qt GUI程序,不过似乎文件里面走了很多弯路……
程序因为采取了静态链接,结果导致体积太大,所以就不发布了;动态链接会需要一些dll,也不方便。
文件列表:
WooduCoding.pro
MainWindow.h
MainWindow.cpp
main.cpp
Icon.rc
Icon.ico(程序的图标)
文件内容:
WooduCoding.pro
//=================================================================
TARGET = WooduCoding
TEMPLATE = app
SOURCES += main.cpp \
MainWindow.cpp
HEADERS += MainWindow.h
RC_FILE = Icon.rc
//=================================================================
MainWindow.h
//=================================================================
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtGui/QTextEdit>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow();
private slots:
void GetCipherText();
void GetClearText();
private:
void createActions();
QWidget *centralwidget;
QVBoxLayout *verticalLayout;
QVBoxLayout *uplayout;
QLabel *label1;
QTextEdit *txtClearText;
QHBoxLayout *midlayout;
QPushButton *btnGetCipherText;
QGridLayout *gridLayout;
QLabel *label3;
QHBoxLayout *looplayout;
QLineEdit *txtLoopFrom;
QLabel *label;
QLineEdit *txtLoopTo;
QPushButton *btnGetClearText;
QVBoxLayout *lowlayout;
QLabel *label2;
QTextEdit *txtCipherText;
};
#endif // MAINWINDOW_H
//=================================================================
MainWindow.cpp
//=================================================================
#include <QtGui>
#include "MainWindow.h"
MainWindow::MainWindow()
{
createActions();
}
void MainWindow::createActions()
{
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
centralwidget = new QWidget();
centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
setCentralWidget(centralwidget);
verticalLayout = new QVBoxLayout(centralwidget);
verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
uplayout = new QVBoxLayout();
uplayout->setObjectName(QString::fromUtf8("uplayout"));
label1 = new QLabel(centralwidget);
label1->setObjectName(QString::fromUtf8("label1"));
uplayout->addWidget(label1);
txtClearText = new QTextEdit(centralwidget);
txtClearText->setObjectName(QString::fromUtf8("txtClearText"));
txtClearText->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
uplayout->addWidget(txtClearText);
verticalLayout->addLayout(uplayout);
midlayout = new QHBoxLayout();
midlayout->setObjectName(QString::fromUtf8("midlayout"));
btnGetCipherText = new QPushButton(centralwidget);
btnGetCipherText->setObjectName(QString::fromUtf8("btnGetCipherText"));
QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(btnGetCipherText->sizePolicy().hasHeightForWidth());
btnGetCipherText->setSizePolicy(sizePolicy);
btnGetCipherText->setMaximumSize(QSize(182, 16777215));
midlayout->addWidget(btnGetCipherText);
gridLayout = new QGridLayout();
gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
gridLayout->setSizeConstraint(QLayout::SetFixedSize);
label3 = new QLabel(centralwidget);
label3->setObjectName(QString::fromUtf8("label3"));
QSizePolicy sizePolicy1(QSizePolicy::Fixed, QSizePolicy::Fixed);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(label3->sizePolicy().hasHeightForWidth());
label3->setSizePolicy(sizePolicy1);
label3->setAlignment(Qt::AlignCenter);
gridLayout->addWidget(label3, 0, 0, 1, 1);
looplayout = new QHBoxLayout();
looplayout->setObjectName(QString::fromUtf8("looplayout"));
txtLoopFrom = new QLineEdit(centralwidget);
txtLoopFrom->setObjectName(QString::fromUtf8("txtLoopFrom"));
QSizePolicy sizePolicy2(QSizePolicy::Maximum, QSizePolicy::Fixed);
sizePolicy2.setHorizontalStretch(0);
sizePolicy2.setVerticalStretch(0);
sizePolicy2.setHeightForWidth(txtLoopFrom->sizePolicy().hasHeightForWidth());
txtLoopFrom->setSizePolicy(sizePolicy2);
txtLoopFrom->setMaximumSize(QSize(25, 16777215));
looplayout->addWidget(txtLoopFrom);
label = new QLabel(centralwidget);
label->setObjectName(QString::fromUtf8("label"));
label->setAlignment(Qt::AlignCenter);
looplayout->addWidget(label);
txtLoopTo = new QLineEdit(centralwidget);
txtLoopTo->setObjectName(QString::fromUtf8("txtLoopTo"));
sizePolicy2.setHeightForWidth(txtLoopTo->sizePolicy().hasHeightForWidth());
txtLoopTo->setSizePolicy(sizePolicy2);
txtLoopTo->setMaximumSize(QSize(25, 16777215));
looplayout->addWidget(txtLoopTo);
gridLayout->addLayout(looplayout, 1, 0, 1, 1);
midlayout->addLayout(gridLayout);
btnGetClearText = new QPushButton(centralwidget);
btnGetClearText->setObjectName(QString::fromUtf8("btnGetClearText"));
sizePolicy.setHeightForWidth(btnGetClearText->sizePolicy().hasHeightForWidth());
btnGetClearText->setSizePolicy(sizePolicy);
btnGetClearText->setMaximumSize(QSize(182, 16777215));
midlayout->addWidget(btnGetClearText);
verticalLayout->addLayout(midlayout);
lowlayout = new QVBoxLayout();
lowlayout->setObjectName(QString::fromUtf8("lowlayout"));
label2 = new QLabel(centralwidget);
label2->setObjectName(QString::fromUtf8("label2"));
lowlayout->addWidget(label2);
txtCipherText = new QTextEdit(centralwidget);
txtCipherText->setObjectName(QString::fromUtf8("txtCipherText"));
txtCipherText->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
lowlayout->addWidget(txtCipherText);
verticalLayout->addLayout(lowlayout);
QWidget::setTabOrder(txtClearText, txtLoopFrom);
QWidget::setTabOrder(txtLoopFrom, txtLoopTo);
QWidget::setTabOrder(txtLoopTo, btnGetCipherText);
QWidget::setTabOrder(btnGetCipherText, txtCipherText);
QWidget::setTabOrder(txtCipherText, btnGetClearText);
setWindowTitle(QApplication::translate("MainWindow", "Woo\345\222\232\345\212\240\345\257\206 Qt\347\211\210", 0, QApplication::UnicodeUTF8));
label1->setText(QApplication::translate("MainWindow", "\346\230\216\346\226\207\350\276\223\345\205\245/\350\276\223\345\207\272\357\274\232", 0, QApplication::UnicodeUTF8));
btnGetCipherText->setText(QApplication::translate("MainWindow", "\350\216\267\345\276\227\345\257\206\346\226\207", 0, QApplication::UnicodeUTF8));
label3->setText(QApplication::translate("MainWindow", "\345\255\227\346\257\215\345\276\252\347\216\257\345\247\213\347\273\210\357\274\232", 0, QApplication::UnicodeUTF8));
txtLoopFrom->setText(QApplication::translate("MainWindow", "1", 0, QApplication::UnicodeUTF8));
label->setText(QApplication::translate("MainWindow", "\350\207\263", 0, QApplication::UnicodeUTF8));
txtLoopTo->setText(QApplication::translate("MainWindow", "10", 0, QApplication::UnicodeUTF8));
btnGetClearText->setText(QApplication::translate("MainWindow", "\350\257\273\345\217\226\346\230\216\346\226\207", 0, QApplication::UnicodeUTF8));
label2->setText(QApplication::translate("MainWindow", "\345\257\206\346\226\207\350\276\223\345\205\245/\350\276\223\345\207\272\357\274\232", 0, QApplication::UnicodeUTF8));
connect(btnGetCipherText, SIGNAL(clicked()), this, SLOT(GetCipherText()));
connect(btnGetClearText, SIGNAL(clicked()), this, SLOT(GetClearText()));
}
void MainWindow::GetCipherText()
{
QString ClearText = txtClearText->toPlainText(),
ASCIIBuffer, ResultBuffer;
int LoopFrom = txtLoopFrom->text().toInt(),
LoopTo = txtLoopTo->text().toInt(),
NumLoop = LoopFrom, i, j,
TextLength = ClearText.length(),
ASCIILength;
for (i = 0; i < TextLength; i++)
{
ASCIIBuffer = QString::number(ClearText[i].unicode());
ASCIILength = ASCIIBuffer.length();
if (i) ResultBuffer.append(',');
for (j = 0; j < ASCIILength; j++, NumLoop++)
{
if (NumLoop > LoopTo) NumLoop = LoopFrom;
//qsrand();
if (ASCIIBuffer[j].digitValue() > NumLoop)
ResultBuffer.append(QChar(qrand()%4 + 65)+'+'+QString::number(ASCIIBuffer[j].digitValue() - NumLoop));
else if (ASCIIBuffer.at(j).digitValue() < NumLoop)
ResultBuffer.append(QChar(qrand()%4 + 65)+'-'+QString::number(NumLoop - ASCIIBuffer[j].digitValue()));
else
ResultBuffer.append(QChar(qrand()%4 + 65));
}
}
txtCipherText->setPlainText(ResultBuffer);
}
void MainWindow::GetClearText()
{
QString CipherText = txtCipherText->toPlainText().append(','),
ResultBuffer;
int LoopFrom = txtLoopFrom->text().toInt(),
LoopTo = txtLoopTo->text().toInt(),
NumLoop = LoopFrom, i,
TextLength = CipherText.length(),
ASCIIBuffer = 0, Operator = 0, SingleNum = 0,
SingleChar;
for (i = 0; i < TextLength; i++)
{
if (NumLoop > LoopTo) NumLoop = LoopFrom;
SingleChar = CipherText[i+1].unicode();
switch (CipherText[i].unicode())
{
case 'A': case 'B': case 'C': case'D':
if ((SingleChar >= 'A' && SingleChar <= 'D') || SingleChar == ',')
{
ASCIIBuffer *=10;
ASCIIBuffer += NumLoop;
NumLoop++;
}
break;
case '+': Operator = 1;break;
case '-': Operator = 2;break;
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
SingleNum *= 10;
SingleNum += CipherText[i].digitValue();
if (SingleChar < '0' || SingleChar > '9')
{
if (Operator == 1) ASCIIBuffer = ASCIIBuffer * 10 + NumLoop + SingleNum;
else if (Operator == 2) ASCIIBuffer = ASCIIBuffer * 10 + NumLoop - SingleNum;
NumLoop++;
SingleNum = 0;
}
break;
case ',':
ResultBuffer.append(QChar(ASCIIBuffer));
ASCIIBuffer = 0;
break;
default:
txtClearText->setText(QString("错误:%1 %2 %3 %4 %5").arg("<font color=red>未知字符</font>").arg(CipherText[i]).arg("<font color=red>于第</font>").arg(i+1).arg("<font color=red>个字符</font>"));
return;
}
}
txtClearText->document()->clear();
txtClearText->setText(ResultBuffer);
}
//=================================================================
main.cpp
//=================================================================
#include <QApplication>
#include "MainWindow.h"
#include <QDesktopWidget>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setApplicationName("Woo咚加密 Qt版");
MainWindow mainwindow;
QDesktopWidget *desktop = QApplication::desktop();
mainwindow.show();
mainwindow.move((desktop->width()-mainwindow.width())/2, (desktop->height()-mainwindow.height())/2);
return a.exec();
}
//=================================================================
Icon.rc
//=================================================================
IDI_ICON1 ICON DISCARDABLE "Icon.ico"
//=================================================================
[[i] 本帖最后由 roywillow 于 2010-5-19 18:48 编辑 [/i]]