forked from feiyangqingyun/QWidgetDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7979fa
commit 3ea100d
Showing
42 changed files
with
1,582 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
TEMPLATE = subdirs | ||
CONFIG += ordered | ||
SUBDIRS += keytool | ||
SUBDIRS += keydemo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include "appkey.h" | ||
#include "qmutex.h" | ||
#include "qfile.h" | ||
#include "qtimer.h" | ||
#include "qdatetime.h" | ||
#include "qapplication.h" | ||
#include "qmessagebox.h" | ||
|
||
AppKey *AppKey::self = NULL; | ||
AppKey *AppKey::Instance() | ||
{ | ||
if (!self) { | ||
QMutex mutex; | ||
QMutexLocker locker(&mutex); | ||
if (!self) { | ||
self = new AppKey; | ||
} | ||
} | ||
|
||
return self; | ||
} | ||
|
||
AppKey::AppKey(QObject *parent) : QObject(parent) | ||
{ | ||
keyData = ""; | ||
keyUseDate = false; | ||
keyDate = "2017-01-01"; | ||
keyUseRun = false; | ||
keyRun = 1; | ||
keyUseCount = false; | ||
keyCount = 10; | ||
|
||
timer = new QTimer(this); | ||
timer->setInterval(1000); | ||
connect(timer, SIGNAL(timeout()), this, SLOT(checkTime())); | ||
startTime = QDateTime::currentDateTime(); | ||
} | ||
|
||
void AppKey::start() | ||
{ | ||
//判断密钥文件是否存在,不存在则从资源文件复制出来,同时需要设置文件写权限 | ||
QString keyName = qApp->applicationDirPath() + "/key.db"; | ||
QFile keyFile(keyName); | ||
if (!keyFile.exists() || keyFile.size() == 0) { | ||
QMessageBox::critical(0, "错误", "密钥文件丢失,请联系供应商!"); | ||
exit(0); | ||
} | ||
|
||
//读取密钥文件 | ||
keyFile.open(QFile::ReadOnly); | ||
keyData = keyFile.readLine(); | ||
keyFile.close(); | ||
|
||
//将从注册码文件中的密文解密,与当前时间比较是否到期 | ||
keyData = getXorEncryptDecrypt(keyData, 110); | ||
QStringList data = keyData.split("|"); | ||
|
||
if (data.count() != 6) { | ||
QMessageBox::critical(0, "错误", "注册码文件已损坏,程序将自动关闭!"); | ||
exit(0); | ||
} | ||
|
||
keyUseDate = (data.at(0) == "1"); | ||
keyDate = data.at(1); | ||
keyUseRun = (data.at(2) == "1"); | ||
keyRun = data.at(3).toInt(); | ||
keyUseCount = (data.at(4) == "1"); | ||
keyCount = data.at(5).toInt(); | ||
|
||
//如果启用了时间限制 | ||
if (keyUseDate) { | ||
QString nowDate = QDate::currentDate().toString("yyyy-MM-dd"); | ||
if (nowDate > keyDate) { | ||
QMessageBox::critical(0, "错误", "软件已到期,请联系供应商更新注册码!"); | ||
exit(0); | ||
} | ||
} | ||
|
||
//如果启用了运行时间显示 | ||
if (keyUseRun) { | ||
timer->start(); | ||
} | ||
} | ||
|
||
void AppKey::stop() | ||
{ | ||
timer->stop(); | ||
} | ||
|
||
void AppKey::checkTime() | ||
{ | ||
//找出当前时间与首次启动时间比较 | ||
QDateTime now = QDateTime::currentDateTime(); | ||
if (startTime.secsTo(now) >= (keyRun * 60)) { | ||
QMessageBox::critical(0, "错误", "试运行时间已到,请联系供应商更新注册码!"); | ||
exit(0); | ||
} | ||
} | ||
|
||
QString AppKey::getXorEncryptDecrypt(const QString &data, char key) | ||
{ | ||
//采用异或加密,也可以自行更改算法 | ||
QByteArray buffer = data.toLatin1(); | ||
int size = buffer.size(); | ||
for (int i = 0; i < size; i++) { | ||
buffer[i] = buffer.at(i) ^ key; | ||
} | ||
|
||
return QLatin1String(buffer); | ||
} | ||
|
||
bool AppKey::checkCount(int count) | ||
{ | ||
if (keyUseCount) { | ||
if (count >= keyCount) { | ||
QMessageBox::critical(0, "错误", "设备数量超过限制,请联系供应商更新注册码!"); | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef APPKEY_H | ||
#define APPKEY_H | ||
|
||
#include <QObject> | ||
#include <QDateTime> | ||
|
||
class QTimer; | ||
|
||
class AppKey : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
static AppKey *Instance(); | ||
explicit AppKey(QObject *parent = 0); | ||
|
||
private: | ||
static AppKey *self; | ||
|
||
QString keyData; //注册码密文 | ||
bool keyUseDate; //是否启用运行日期时间限制 | ||
QString keyDate; //到期时间字符串 | ||
bool keyUseRun; //是否启用可运行时间限制 | ||
int keyRun; //可运行时间 | ||
bool keyUseCount; //是否启用设备数量限制 | ||
int keyCount; //设备限制数量 | ||
|
||
QTimer *timer; //定时器判断是否运行超时 | ||
QDateTime startTime; //程序启动时间 | ||
|
||
private slots: | ||
void checkTime(); | ||
QString getXorEncryptDecrypt(const QString &data, char key); | ||
|
||
public slots: | ||
void start(); | ||
void stop(); | ||
bool checkCount(int count); | ||
}; | ||
|
||
#endif // APPKEY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include "frmmain.h" | ||
#include "ui_frmmain.h" | ||
#include "appkey.h" | ||
|
||
frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain) | ||
{ | ||
ui->setupUi(this); | ||
} | ||
|
||
frmMain::~frmMain() | ||
{ | ||
delete ui; | ||
} | ||
|
||
void frmMain::on_btnAdd_clicked() | ||
{ | ||
QString name = ui->lineEdit->text().trimmed(); | ||
ui->listWidget->addItem(name); | ||
|
||
//计算当前设备数量多少 | ||
AppKey::Instance()->checkCount(ui->listWidget->count()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef FRMMAIN_H | ||
#define FRMMAIN_H | ||
|
||
#include <QWidget> | ||
|
||
namespace Ui { | ||
class frmMain; | ||
} | ||
|
||
class frmMain : public QWidget | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit frmMain(QWidget *parent = 0); | ||
~frmMain(); | ||
|
||
private: | ||
Ui::frmMain *ui; | ||
|
||
private slots: | ||
void on_btnAdd_clicked(); | ||
}; | ||
|
||
#endif // FRMMAIN_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ui version="4.0"> | ||
<class>frmMain</class> | ||
<widget class="QWidget" name="frmMain"> | ||
<property name="geometry"> | ||
<rect> | ||
<x>0</x> | ||
<y>0</y> | ||
<width>400</width> | ||
<height>300</height> | ||
</rect> | ||
</property> | ||
<property name="windowTitle"> | ||
<string>frmMain</string> | ||
</property> | ||
<layout class="QGridLayout" name="gridLayout"> | ||
<item row="0" column="0" colspan="2"> | ||
<widget class="QListWidget" name="listWidget"/> | ||
</item> | ||
<item row="1" column="0"> | ||
<widget class="QLineEdit" name="lineEdit"> | ||
<property name="text"> | ||
<string>测试设备</string> | ||
</property> | ||
</widget> | ||
</item> | ||
<item row="1" column="1"> | ||
<widget class="QPushButton" name="btnAdd"> | ||
<property name="text"> | ||
<string>添加</string> | ||
</property> | ||
</widget> | ||
</item> | ||
</layout> | ||
</widget> | ||
<layoutdefault spacing="6" margin="11"/> | ||
<resources/> | ||
<connections/> | ||
</ui> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#------------------------------------------------- | ||
# | ||
# Project created by QtCreator 2019-02-17T20:23:58 | ||
# | ||
#------------------------------------------------- | ||
|
||
QT += core gui | ||
|
||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets | ||
|
||
TARGET = keydemo | ||
TEMPLATE = app | ||
MOC_DIR = temp/moc | ||
RCC_DIR = temp/rcc | ||
UI_DIR = temp/ui | ||
OBJECTS_DIR = temp/obj | ||
DESTDIR = $$PWD/../bin | ||
CONFIG += warn_off | ||
|
||
SOURCES += main.cpp | ||
SOURCES += frmmain.cpp appkey.cpp | ||
HEADERS += frmmain.h appkey.h | ||
FORMS += frmmain.ui | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#pragma execution_character_set("utf-8") | ||
#include "frmmain.h" | ||
#include "appkey.h" | ||
#include <QApplication> | ||
#include <QTextCodec> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
QApplication a(argc, argv); | ||
a.setFont(QFont("Microsoft Yahei", 9)); | ||
|
||
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) | ||
#if _MSC_VER | ||
QTextCodec *codec = QTextCodec::codecForName("gbk"); | ||
#else | ||
QTextCodec *codec = QTextCodec::codecForName("utf-8"); | ||
#endif | ||
QTextCodec::setCodecForLocale(codec); | ||
QTextCodec::setCodecForCStrings(codec); | ||
QTextCodec::setCodecForTr(codec); | ||
#else | ||
QTextCodec *codec = QTextCodec::codecForName("utf-8"); | ||
QTextCodec::setCodecForLocale(codec); | ||
#endif | ||
|
||
//启动密钥服务类 | ||
AppKey::Instance()->start(); | ||
|
||
frmMain w; | ||
w.setWindowTitle("密钥使用示例"); | ||
w.show(); | ||
|
||
return a.exec(); | ||
} |
Oops, something went wrong.