目录
一、前言
在Qt下通过Windows快捷方式启动应用,主要涉及到创建快捷方式并放置自定义的文件夹位置,之后调用ShellExecute函数打开对应应用程序。
二、创建快捷方式
在Windows中,快捷方式(.lnk文件)是一种特殊的文件类型,它包含指向其他文件或命令的引用。Qt本身没有直接创建快捷方式的API,但可以通过调用外部脚本(如:power shell、python等)或Windows API等方法来创建。下面以power shell脚本为例:
2.1 程序文件路径
找到并复制你想要创建快捷方式的程序路径
#你的程序文件路径
$targetPath = "C:\\you\\path\\application.exe"
2.2 设置保存路径
设置你的快捷方式要保存到哪个路径下
#快捷方式要保存到的路径
$shortcutPath = "C:\\youpath\\shortcut\\application.lnk"
$wscriptShell = New-Object -ComObject WScript.Shell
$shortcut = $wscriptShell.CreateShortCut($shortcutPath)
$shortcut.TargetPath = $targetPath
$shortcut.IconLocation = "$targetPath,0"
$shortcut.Save()
2.3 整体代码实现
#你的程序文件路径
$targetPath = "C:\\you\\path\\application.exe"
#快捷方式要保存到的路径
$shortcutPath = "C:\\youpath\\shortcut\\application.lnk"
$wscriptShell = New-Object -ComObject WScript.Shell
$shortcut = $wscriptShell.CreateShortCut($shortcutPath)
$shortcut.TargetPath = $targetPath
$shortcut.IconLocation = "$targetPath,0"
$shortcut.Save()
Write-Host "快捷方式已创建在指定文件夹。"
三、读取快捷方式
3.1 输出快捷方式
在第二步的脚本代码中加入一行输出(快捷方式的路径):
Write-Output $shortcutPath
3.2 QProcess类创建脚本并检测
脚本创建好之后,在Qt程序中通过QProcess类调用即可创建快捷方式到指定的文件夹中去,同时,在通过QProcess类调用脚本之后,应该检测其脚本的输出与结束。
void YouClass::Createlnk(const QString &scriptPath)
{
// 构建PowerShell命令和参数
QStringList command;
command << "powershell.exe";
command << "-ExecutionPolicy" << "Bypass"; // 注意:这可能会带来安全风险
command << "-File";
command << scriptPath;
// 启动进程
QProcess *process = new QProcess(this);
process->start(command.first(), command.mid(1)); // 第一个元素是程序名,其余是参数
// 链接创建快捷方法的脚本输出与结束信号和相对应的槽函数
connect(process, &QProcess::readyReadStandardOutput, this, &Widget::readStandardOutput);
connect(process, &QProcess::finished, this, &Widget::processFinished);
}
3.3 脚本输出函数
脚本输出函数:将创建的快捷方式的路径保存到Qt程序中去,方便后续启动。
void YouClass::readStandardOutput()
{
QString data = process->readAllStandardOutput();
data.replace("\\\\", "\\");//读取脚本的输出时将"\\\\"替换成"\\"
data = data.trimmed();//去除脚本输出的前后空白字符,包括\r\n
emit start(data);
qDebug() << "Output:" << data;
}
3.4 脚本结束函数
脚本结束函数:判断脚本程序的结束是否正常,以及如果出错其错误码是多少?
void YouClass::processFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << "Process finished with exit code" << exitCode;
if (exitStatus == QProcess::NormalExit && exitCode == 0) {
qDebug() << "Process finished successfully";
} else {
qDebug() << "Process finished with errors";
QString error = process->readAllStandardError();
qDebug() << "Error:" << error;
}
}
四、打开快捷方式
在Qt应用程序中调用ShellExecute
来打开一个快捷方式(.lnk
文件)。
4.1 包含头文件
首先,包含其对应的头文件
#include <windows.h> // 包含Windows API头文件
4.2 ShellExecute函数打开快捷方式
之后,通过ShellExecute函数打开快捷方式,将第三步保存的快捷方式路径作为参数传入。
ShellExecute的参数解释:
第一个参数通常为NULL,表示没有父窗口
第二个参数为"open",表示执行默认操作(打开文件)
第三个参数是文件路径
第四个参数是传递给程序的参数(对于打开文件,通常为NULL)
第五个参数是工作目录(对于打开文件,通常也为NULL)
第六个参数定义了窗口的显示方式(SW_SHOWNORMAL表示正常显示)
void openShortcutOrFile(const QString &filePath) {
// 将QString转换为LPCSTR(即const char*)
const char *filePathCStr = filePath.toLocal8Bit().constData();
// 调用ShellExecute
HINSTANCE result = ShellExecuteA(NULL, "open", filePathCStr, NULL, NULL, SW_SHOWNORMAL);
// 检查ShellExecute的返回值
if (result <= (HINSTANCE)32) {
// ShellExecute失败
if (result == 0) {
// 直接失败,没有错误码
qDebug() << "Failed to open file with ShellExecute, no error code available.";
} else {
// 获取并打印错误码
DWORD error = GetLastError();
qDebug() << "Failed to open file with ShellExecute, error:" << error;
}
}
}
结语
在Qt下通过Windows快捷方式启动应用,主要涉及到创建快捷方式并放置自定义的文件夹位置,之后调用ShellExecute函数打开对应应用程序。
读者有任何问题都可以在评论区留言,博主看到会去解答。同时也欢迎各路大佬批评指正!