-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
47 lines (39 loc) · 1.46 KB
/
main.cpp
File metadata and controls
47 lines (39 loc) · 1.46 KB
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
#include "mainwindow.h"
#include <QApplication>
#include <QFileInfo>
#include <QDir>
#include <QTimer>
#include <QMessageBox>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// 设置应用程序信息,用于保存设置
QApplication::setApplicationName("TidyText");
QApplication::setOrganizationName("TidyText");
QApplication::setApplicationVersion("1.0.0");
MainWindow w;
// 处理命令行参数
QStringList arguments = QApplication::arguments();
// 第一个参数是程序自身,所以从索引1开始
if (arguments.size() > 1) {
// 可能有多个文件被选中(但右键菜单通常是一次打开一个文件)
for (int i = 1; i < arguments.size(); ++i) {
QString filePath = arguments[i];
// 确保路径格式正确(有些时候传递的路径可能包含引号)
filePath = filePath.trimmed();
if (filePath.startsWith('"') && filePath.endsWith('"')) {
filePath = filePath.mid(1, filePath.length() - 2);
}
// 检查文件是否存在
QFileInfo fileInfo(filePath);
if (fileInfo.exists() && fileInfo.isFile()) {
// 使用 QTimer 确保窗口完全显示后再打开文件
QTimer::singleShot(100, [&w, filePath]() {
w.openFileFromCommandLine(filePath);
});
}
}
}
w.show();
return a.exec();
}