Skip to content

Commit e981a17

Browse files
committed
fix: config path error on mac
1 parent f9fefb8 commit e981a17

File tree

4 files changed

+26
-0
lines changed

4 files changed

+26
-0
lines changed

QtScrcpy/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
163163
set(QC_UTIL_SOURCES ${QC_UTIL_SOURCES}
164164
util/mousetap/cocoamousetap.h
165165
util/mousetap/cocoamousetap.mm
166+
util/path.h
167+
util/path.mm
166168
)
167169
endif()
168170
source_group(util FILES ${QC_UTIL_SOURCES})

QtScrcpy/util/config.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#include <QDebug>
55

66
#include "config.h"
7+
#ifdef Q_OS_OSX
8+
#include "path.h"
9+
#endif
710

811
#define GROUP_COMMON "common"
912

@@ -125,7 +128,15 @@ const QString &Config::getConfigPath()
125128
QFileInfo fileInfo(s_configPath);
126129
if (s_configPath.isEmpty() || !fileInfo.isDir()) {
127130
// default application dir
131+
// mac系统当从finder打开app时,默认工作目录不再是可执行程序的目录了,而是"/"
132+
// 而Qt的获取工作目录的api都依赖QCoreApplication的初始化,所以使用mac api获取当前目录
133+
#ifdef Q_OS_OSX
134+
// get */QtScrcpy.app path
135+
s_configPath = Path::GetCurrentPath();
136+
s_configPath += "/Contents/MacOS/config";
137+
#else
128138
s_configPath = "config";
139+
#endif
129140
}
130141
}
131142
return s_configPath;

QtScrcpy/util/path.h

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
class Path {
4+
public:
5+
static const char* GetCurrentPath();
6+
};

QtScrcpy/util/path.mm

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "path.h"
2+
3+
#import <Cocoa/Cocoa.h>
4+
5+
const char* Path::GetCurrentPath() {
6+
return [[[NSBundle mainBundle] bundlePath] UTF8String];
7+
}

0 commit comments

Comments
 (0)