QTimer *timer = new QTimer; // QTimer inherits QObject
timer->inherits("QTimer"); // returns true
timer->inherits("QObject"); // returns true
timer->inherits("QAbstractButton"); // returns false
-
文件权限
extern Q_CORE_EXPORT int qt_ntfs_permission_lookup;
qt_ntfs_permission_lookup++; // turn checking on
qt_ntfs_permission_lookup--; // turn it off again
QFile::Permissions pers;
QFile file("D:/DOWN/f.txt");
file.open(QFile::ReadOnly);
pers =file.permissions();
qDebug() <<pers;
file.setPermissions(QFile::ReadOwner);
qDebug() <<file.permissions();
file.setPermissions(pers);
-
QMediaPlayer本地解码器
WIN上下载k-lite或者LAV Filters安装即可。
-
GC
// 创建实例
QObjectCleanupHandler *cleaner = new QObjectCleanupHandler;
// 创建窗口
QPushButton *w = new QPushButton("Remove Me");
w->show();
// 注册第一个按钮
cleaner->add(w);
// 如果第一个按钮点击之后,删除自身
connect(w, SIGNAL(clicked()), w, SLOT(deleteLater()));
// 创建第二个按钮,注意,这个按钮没有任何动作
w = new QPushButton("Nothing");
cleaner->add(w);
w->show();
// 创建第三个按钮,删除所有
w = new QPushButton("Remove All");
cleaner->add(w);
connect(w, SIGNAL(clicked()), cleaner, SLOT(deleteLater()));
w->show();
-
弱指针
// QPointer 表现类似普通指针
QDate *mydate = new QDate(QDate::currentDate());
QPointer mypointer = mydata;
mydate->year(); // -> 2005
mypointer->year(); // -> 2005
// 当对象 delete 之后,QPointer 会有不同的表现
delete mydate;
if(mydate == NULL)
printf("clean pointer");
else
printf("dangling pointer");
// 输出 dangling pointer
if(mypointer.isNull())
printf("clean pointer");
else
printf("dangling pointer");
// 输出 clean pointer
-
内置图标
QStyle::SP_TitleBarMenuButton
-
QScroller
ui->listWidget->setHorizontalScrollMode(QListWidget::ScrollPerPixel);
QScroller::grabGesture(ui->listWidget,QScroller::LeftMouseButtonGesture);
-
QtCreator软件的配置文件
C:\Users\Administrator\AppData\Roaming\QtProject
-
守护进程
/*为了使得兼容任意程序,特意提炼出来共性,增加了多种设置。
可设置检测的程序名称。
可设置udp通信端口。
可设置超时次数。
自动记录已重启次数。
自动记录最后一次重启时间。
是否需要重新刷新桌面。
可重置当前重启次数和最后重启时间。
自动隐藏的托盘运行或者后台运行。
提供界面设置程序名称已经开启和暂停服务。*/
1#pragma execution_character_set("utf-8")
2#include "frmmain.h"
3#include "ui_frmmain.h"
4#include "qtimer.h"
5#include "qudpsocket.h"
6#include "qsharedmemory.h"
7#include "qprocess.h"
8#include "qdatetime.h"
9#include "qapplication.h"
10#include "qdesktopservices.h"
11#include "qmessagebox.h"
12#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
13#include "qstandardpaths.h"
14#endif
15
16#include "app.h"
17
18frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
19{
20 ui->setupUi(this);
21 this->initForm();
22}
23
24frmMain::~frmMain()
25{
26 delete ui;
27}
28
29void frmMain::changeEvent(QEvent *event)
30{
31 //隐藏当前界面,最小化到托盘
32 if(event->type() == QEvent::WindowStateChange) {
33 if(windowState() & Qt::WindowMinimized) {
34 hide();
35 }
36 }
37
38 QWidget::changeEvent(event);
39}
40
41void frmMain::initForm()
42{
43 count = 0;
44 ok = false;
45
46 //每秒钟定时询问心跳
47 timerHeart = new QTimer(this);
48 timerHeart->setInterval(2000);
49 connect(timerHeart, SIGNAL(timeout()), this, SLOT(sendHearData()));
50
51 //从6050端口开始,如果绑定失败则将端口加1,直到绑定成功
52 udp = new QUdpSocket(this);
53 int port = 6050;
54 while(!udp->bind(port)) {
55 port++;
56 }
57
58 connect(udp, SIGNAL(readyRead()), this, SLOT(readData()));
59
60 if (App::TargetAppName.isEmpty()) {
61 ui->btnStart->setText("启动");
62 ui->btnStart->setEnabled(false);
63 timerHeart->stop();
64 } else {
65 ui->btnStart->setText("暂停");
66 ui->btnStart->setEnabled(true);
67 timerHeart->start();
68 }
69
70 ui->txtAppName->setText(App::TargetAppName);
71 ui->txtAppName->setFocus();
72}
73
74void frmMain::sendHearData()
75{
76 udp->writeDatagram("hello", QHostAddress::LocalHost, App::TargetAppPort);
77
78 //判断当前是否没有回复
79 if (!ok) {
80 count++;
81 } else {
82 count = 0;
83 ok = false;
84 }
85
86 //如果超过规定次数没有收到心跳回复,则超时重启
87 if (count >= App::TimeoutCount) {
88 timerHeart->stop();
89
90 QSharedMemory mem(App::TargetAppName);
91 if (!mem.create(1)) {
92 killApp();
93 }
94
95 QTimer::singleShot(1000 , this, SLOT(killOther()));
96 QTimer::singleShot(3000 , this, SLOT(startApp()));
97 QTimer::singleShot(4000 , this, SLOT(startExplorer()));
98 }
99}
100
101void frmMain::killApp()
102{
103 QProcess *p = new QProcess;
104 p->start(QString("taskkill /im %1.exe /f").arg(App::TargetAppName));
105}
106
107void frmMain::killOther()
108{
109 QProcess *p = new QProcess;
110 p->start(QString("taskkill /im %1.exe /f").arg("WerFault"));
111
112 //重建缓存,彻底清除托盘图标
113 if (App::ReStartExplorer) {
114 QProcess *p1 = new QProcess;
115 p1->start("taskkill /f /im explorer.exe");
116 }
117}
118
119void frmMain::startApp()
120{
121 if (ui->btnStart->text() == "开始" || ui->btnStart->text() == "启动") {
122 count = 0;
123 return;
124 }
125
126 QProcess *p = new QProcess;
127 p->start(QString(""%1/%2.exe"").arg(qApp->applicationDirPath()).arg(App::TargetAppName));
128
129 count = 0;
130 ok = true;
131 timerHeart->start();
132
133 App::ReStartCount++;
134 App::ReStartLastTime = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
135 App::writeConfig();
136
137 ui->labCount->setText(QString("已重启 %1 次").arg(App::ReStartCount));
138 ui->labInfo->setText(QString("最后一次重启在 %1").arg(App::ReStartLastTime));
139}
140
141void frmMain::startExplorer()
142{
143 //取得操作系统目录路径,指定操作系统目录下的explorer程序,采用绝对路径,否则在64位操作系统下无效
144#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
145 QString str = QStandardPaths::writableLocation(QStandardPaths::ApplicationsLocation);
146#else
147 QString str = QDesktopServices::storageLocation(QDesktopServices::ApplicationsLocation);
148#endif
149
150 if (App::ReStartExplorer) {
151 str = QString("%1\\Windows\\explorer.exe").arg(str.mid(0, 2));
152 QProcess *p = new QProcess(this);
153 p->start(str);
154 }
155}
156
157void frmMain::readData()
158{
159 QByteArray tempData;
160 do {
161 tempData.resize(udp->pendingDatagramSize());
162 udp->readDatagram(tempData.data(), tempData.size());
163 QString data = QLatin1String(tempData);
164 if (data.right(2) == "OK") {
165 count = 0;
166 ok = true;
167 }
168 } while (udp->hasPendingDatagrams());
169}
170
171void frmMain::on_btnOk_clicked()
172{
173 App::TargetAppName = ui->txtAppName->text();
174 if (App::TargetAppName == "") {
175 QMessageBox::critical(this, "提示", "应用程序名称不能为空!");
176 ui->txtAppName->setFocus();
177 return;
178 }
179
180 App::writeConfig();
181 ui->btnStart->setEnabled(true);
182}
183
184void frmMain::on_btnStart_clicked()
185{
186 count = 0;
187 if (ui->btnStart->text() == "暂停") {
188 timerHeart->stop();
189 ui->btnStart->setText("开始");
190 } else {
191 timerHeart->start();
192 ui->btnStart->setText("暂停");
193 }
194}
195
196void frmMain::on_btnReset_clicked()
197{
198 App::ReStartCount = 0;
199 App::ReStartLastTime = "2019-01-01 12:00:00";
200 App::writeConfig();
201
202 ui->txtAppName->setText(App::TargetAppName);
203 ui->labCount->setText(QString("已重启 %1 次").arg(App::ReStartCount));
204 ui->labInfo->setText(QString("最后一次重启在 %1").arg(App::ReStartLastTime));
205 QMessageBox::information(this, "提示", "重置配置文件成功!");
206}
主程序使用核心代码:
1#include "applive.h"
2#include "qmutex.h"
3#include "qudpsocket.h"
4#include "qstringlist.h"
5#include "qapplication.h"
6#include "qdatetime.h"
7#include "qdebug.h"
8
9#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
10
11QScopedPointer<AppLive> AppLive::self;
12AppLive *AppLive::Instance()
13{
14 if (self.isNull()) {
15 QMutex mutex;
16 QMutexLocker locker(&mutex);
17 if (self.isNull()) {
18 self.reset(new AppLive);
19 }
20 }
21
22 return self.data();
23}
24
25AppLive::AppLive(QObject *parent) : QObject(parent)
26{
27 udpServer = new QUdpSocket(this);
28
29 QString name = qApp->applicationFilePath();
30 QStringList list = name.split("/");
31 appName = list.at(list.count() - 1).split(".").at(0);
32}
33
34void AppLive::readData()
35{
36 QByteArray tempData;
37
38 do {
39 tempData.resize(udpServer->pendingDatagramSize());
40 QHostAddress sender;
41 quint16 senderPort;
42 udpServer->readDatagram(tempData.data(), tempData.size(), &sender, &senderPort);
43 QString data = QLatin1String(tempData);
44
45 if (data == "hello") {
46 udpServer->writeDatagram(QString("%1OK").arg(appName).toLatin1(), sender, senderPort);
47 }
48 } while (udpServer->hasPendingDatagrams());
49}
50
51bool AppLive::start(int port)
52{
53 bool ok = udpServer->bind(port);
54 if (ok) {
55 connect(udpServer, SIGNAL(readyRead()), this, SLOT(readData()));
56 qDebug() << TIMEMS << "Start AppLive Ok";
57 }
58
59 return ok;
60}
61
62void AppLive::stop()
63{
64 udpServer->abort();
65 disconnect(udpServer, SIGNAL(readyRead()), this, SLOT(readData()));
66}
-
Qt密钥生成器
在很多商业软件中,需要提供一些可以试运行的版本,这样就需要配套密钥机制来控制,纵观大部分的试用版软件,基本上采用以下几种机制来控制。
远程联网激活,每次启动都联网查看使用时间等,这种方法最完美,缺点是没法联网的设备就歇菜了。
通过获取本地的硬盘+CPU等硬件的编号,做一个运算,生成一个激活码,超过半数的软件会采用此方法,缺点是不能自由控制软件的其他参数,比如软件中添加的设备数量的控制。
设定一个运行到期时间+数量限制+已运行时间的密钥文件,发给用户配套软件使用,缺点是如果仅仅设置的是运行到期时间,用户可以更改电脑时间来获取更长的使用时间,在电脑不联网的情况下。
本demo采用抛砖引玉的形式,用第三种方法来实现,密钥文件采用最简单的异或加密,可以自行改成其他加密方法。
核心代码:
1#include "frmmain.h"
2#include "ui_frmmain.h"
3#include "qmessagebox.h"
4#include "qfile.h"
5
6frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
7{
8 ui->setupUi(this);
9 this->initForm();
10}
11
12frmMain::~frmMain()
13{
14 delete ui;
15}
16
17void frmMain::initForm()
18{
19 QStringList min;
20 min << "1" << "5" << "10" << "20" << "30";
21 for (int i = 1; i <= 24; i++) {
22 min << QString::number(i * 60);
23 }
24
25 ui->cboxMin->addItems(min);
26 ui->cboxMin->setCurrentIndex(1);
27 ui->dateEdit->setDate(QDate::currentDate());
28
29 for (int i = 5; i <= 150; i = i + 5) {
30 ui->cboxCount->addItem(QString("%1").arg(i));
31 }
32}
33
34QString frmMain::getXorEncryptDecrypt(const QString &data, char key)
35{
36 //采用异或加密,也可以自行更改算法
37 QByteArray buffer = data.toLatin1();
38 int size = buffer.size();
39 for (int i = 0; i < size; i++) {
40 buffer[i] = buffer.at(i) ^ key;
41 }
42
43 return QLatin1String(buffer);
44}
45
46void frmMain::on_btnOk_clicked()
47{
48 bool useDate = ui->ckDate->isChecked();
49 bool useRun = ui->ckRun->isChecked();
50 bool useCount = ui->ckCount->isChecked();
51
52 if (!useDate && !useRun && !useCount) {
53 if (QMessageBox::question(this, "询问", "确定要生成没有任何限制的密钥吗?") != QMessageBox::Yes) {
54 return;
55 }
56 }
57
58 QString strDate = ui->dateEdit->date().toString("yyyy-MM-dd");
59 QString strRun = ui->cboxMin->currentText();
60 QString strCount = ui->cboxCount->currentText();
61 QString key = QString("%1|%2|%3|%4|%5|%6").arg(useDate).arg(strDate).arg(useRun).arg(strRun).arg(useCount).arg(strCount);
62
63 QFile file(QApplication::applicationDirPath() + "/key.db");
64 file.open(QFile::WriteOnly | QIODevice::Text);
65 file.write(getXorEncryptDecrypt(key, 110).toLatin1());
66 file.close();
67 QMessageBox::information(this, "提示", "生成密钥成功,将 key.db 文件拷贝到对应目录即可!");
68}
69
70void frmMain::on_btnClose_clicked()
71{
72 this->close();
73}
QString frmMain::getWMIC(const QString &cmd)
{
//获取cpu名称:wmic cpu get Name
//获取cpu核心数:wmic cpu get NumberOfCores
//获取cpu线程数:wmic cpu get NumberOfLogicalProcessors
//查询cpu序列号:wmic cpu get processorid
//查询主板序列号:wmic baseboard get serialnumber
//查询BIOS序列号:wmic bios get serialnumber
//查看硬盘:wmic diskdrive get serialnumber
QProcess p;
p.start(cmd);
p.waitForFinished();
QString result = QString::fromLocal8Bit(p.readAllStandardOutput());
QStringList list = cmd.split(" ");
result = result.remove(list.last(), Qt::CaseInsensitive);
result = result.replace("\r", "");
result = result.replace("\n", "");
result = result.simplified();
return result;
}
QString frmMain::getCpuName()
{
return getWMIC("wmic cpu get name");
}
QString frmMain::getCpuId()
{
return getWMIC("wmic cpu get processorid");
}
QString frmMain::getDiskNum()
{
return getWMIC("wmic diskdrive where index=0 get serialnumber");
}
-
QProcess使用
bat进程启动运行
QProcess process;
process.start("del /s *.txt");
// same as process.start("del", QStringList() << "/s" << "*.txt");
空格不用管在要执行的路径前和后加上双引号就可以。
比如process.start("D:\Program Files\XiaZaiBa.exe");
写成process.start("\"D:\Program Files\XiaZaiBa.exe\"");就可以了。
process.setWorkingDirectory( "./subdir" );
process.start( QApplication::applicationDirPath() + "subdir/sub.exe" );
不用vbs的bat启动,给入监控端口参数启动
QProcess process; QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("TMPDIR", "C:\\MyApp\\temp"); // Add an environment variable
process.setProcessEnvironment(env);
process.start("myapp");
//启用VBS脚本
wscript.exe
//system("WScript.exe ../engine/HideRun.vbs");
SHELLEXECUTEINFOA ShExecInfo;
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFOA);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "I:/QTCreator/meidayunpan/prj_meida/git_lsx/MideaDisk/engine/aria2c.exe";
ShExecInfo.lpParameters = "--conf-path=aria2.conf";//传出去的参数
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
BOOL b_ret = ShellExecuteExA(&ShExecInfo);
QProcess* process = new QProcess(this);
process->start("../engine/aria2c.exe", QStringList() << "--conf-path=aria2.conf");
QObject::connect(process, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT([&](QProcess::ProcessError){
switch (error)
{
case QProcess::FailedToStart:
QMessageBox::information(0, "FailedToStart", "FailedToStart");
break;
case QProcess::Crashed:
QMessageBox::information(0, "Crashed", "Crashed");
break;
case QProcess::Timedout:
QMessageBox::information(0, "FailedToStart", "FailedToStart");
break;
case QProcess::WriteError:
QMessageBox::information(0, "Timedout", "Timedout");
break;
case QProcess::ReadError:
QMessageBox::information(0, "ReadError", "ReadError");
break;
case QProcess::UnknownError:
QMessageBox::information(0, "UnknownError", "UnknownError");
break;
default:
QMessageBox::information(0, "default", "default");
break;
}
}));
//启动
QObject::connect(process, SIGNAL(started()), this, SLOT([&](){
QMessageBox::information(0, "start", "start");
}));
//状态改变
QObject::connect(process, SIGNAL(stateChanged(QProcess::ProcessState)), this, SLOT([&](QProcess::ProcessState newState){
QMessageBox::information(0, "startChange", QString::number(newState));
}));
//退出
QObject::connect(process, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT([&](int exitCode, QProcess::ExitStatus exitStatus){
QMessageBox::information(0, QString::number(exitCode), QString::number(exitStatus));
}));
void showEvent(QShowEvent *e){
setAttribute(Qt::WA_Mapped);
QWidget::showEvent(e);
}
-
QML加载
m_pQuickWidget = new QQuickWidget(this);
m_pQuickWidget->setFixedSize(1131,586);
m_pQuickWidget->setAttribute(Qt::WA_AlwaysStackOnTop);
m_pQuickWidget->setClearColor(QColor(Qt::transparent));
m_pQuickWidget->setSource(QUrl(QStringLiteral("qrc:/UI/Viewer.qml")));
m_pQuickWidget->rootObject()->setWidth(1131);
m_pQuickWidget->rootObject()->setHeight(586);
m_pQuickWidget->move(this->width()/2.0 - m_pQuickWidget->width()/2.0,342);
m_pQuickWidget->show();
-
内存SQL
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
用记事本打开对应的ui文件,找到为空的地方,删除即可
-
常见问题
图片访问,需编辑qrc文件,添加资源,使用:/全路径名访问。
Vs下qt/Qt setting选择项添加相应模块
QPalette::Window表示背景色,QPalette::WindowText表示前景色
qmake -tp vc,qt工程转vc工程,同理,vs下qt选项也可以转qt工程
无法打开stddef与corecrt等文件,需要安装win kits 10 sdk,配置pro目录。或者直接使用vs2017安装更新vs sdk
Qml计时:console.time() console.endtime()\
Qmake执行失败,路径必须为全英文
类成员线程快速创建,头文件QtConcurrentrun,命名空间QtConcurrent::run
编码换行符错误:
QTextCodec *codec = QTextCodec::codecForName("UTF-8");//情况2
QTextCodec::setCodecForTr(codec);
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
选项/设置/文本编辑:主要是设置成UTF-8格式,再把BOM设置成都添加。
10.构造与析构函数无法解析,执行qmake
-
指示器样式
例如 ::down-arrow,::menu-indicator{} ::up-arrow:disabled,::up-arrow:off{}。
-
管理员运行
QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\" #以管理员运行
QMAKE_LFLAGS += /SUBSYSTEM:WINDOWS,\"5.01\" #VS2013 在XP运行
-
标题栏高度
style()->pixelMetric(QStyle::PM_TitleBarHeight); PM_TitleBarHeight点进去你会发现新大陆
-
获取类属性
const QMetaObject *metaobject = object->metaObject();
int count = metaobject->propertyCount();
for (int i = 0; i < count; ++i) {
QMetaProperty metaproperty = metaobject->property(i);
const char *name = metaproperty.name();
QVariant value = object->property(name);
qDebug() << name << value;
}
-
每个控件都有ID
a.setAttribute(Qt::AA_NativeWindows);可以让每个控件都拥有独立的句柄。
-
2K,4K屏支持
#if (QT_VERSION > QT_VERSION_CHECK(5,6,0))
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
-
线程
QThread用法:启动与退出_qthread restart-CSDN博客
http://m.codes51.com/itwd/1338157.html
Qt使用多线程的一些心得_qt emit 处理时间长-CSDN博客
-
换肤
//移除原有样式
style()->unpolish(ui->btn);
//重新设置新的该控件的样式。
style()->polish(ui->btn);
-
webengine模块发布
发布程序的时候带上QtWebEngineProcess.exe+translations文件夹+resources文件夹
-
启用独立控制台
运行文件附带调试输出窗口 CONFIG += console pro
-
中文编译乱码
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
Qt creator 的菜单里 编辑-Select Encoding,可以选择编码。以及工具-选项->文本编辑器->行为->文件编码里要选择UTF-8,BOM: 如果是UTF-8则自动添加。
QString::fromLocal8Bit
-
重复文件保存
QString path = "F:/down/";
QFile m_saveFile(path + strFileName);
int i = 1;
while (m_saveFile.exists()) {
m_saveFile.setFileName(path + strFileName + QString::number(i));
++i;
}
int indexPt = strFileName.lastIndexOf('.');
path.append(strFileName.mid(0, indexPt));
path.append(QString::number(i));
path.append(strFileName.mid(indexPt, strFileName.length() - indexPt));
m_saveFile.setFileName(path);
if (m_saveFile.open(QIODevice::WriteOnly))
{ //打开失败
m_saveFile.write(stuBytes.m_pChar, stuBytes.m_size);
m_saveFile.close();
}
QT实现windows窗口内嵌_qt dialog窗口嵌入在主窗口内部-CSDN博客
QT样式美化 之 qss入门_qt tablewidget qss-CSDN博客
C++ QT实现验证码功能_qt中实现手机验证码的方法-CSDN博客
C++ 实现QT信号槽_c++ 加qt写一个好的示波器-CSDN博客
Electron qt开发教程_qt electron混合开发-CSDN博客