fix: config path error on mac

This commit is contained in:
rankun 2022-10-30 19:25:26 +08:00
parent f1e24fe81a
commit 587c1f5872
4 changed files with 26 additions and 0 deletions

View file

@ -163,6 +163,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(QC_UTIL_SOURCES ${QC_UTIL_SOURCES}
util/mousetap/cocoamousetap.h
util/mousetap/cocoamousetap.mm
util/path.h
util/path.mm
)
endif()
source_group(util FILES ${QC_UTIL_SOURCES})

View file

@ -4,6 +4,9 @@
#include <QDebug>
#include "config.h"
#ifdef Q_OS_OSX
#include "path.h"
#endif
#define GROUP_COMMON "common"
@ -125,7 +128,15 @@ const QString &Config::getConfigPath()
QFileInfo fileInfo(s_configPath);
if (s_configPath.isEmpty() || !fileInfo.isDir()) {
// default application dir
// mac系统当从finder打开app时默认工作目录不再是可执行程序的目录了而是"/"
// 而Qt的获取工作目录的api都依赖QCoreApplication的初始化所以使用mac api获取当前目录
#ifdef Q_OS_OSX
// get */QtScrcpy.app path
s_configPath = Path::GetCurrentPath();
s_configPath += "/Contents/MacOS/config";
#else
s_configPath = "config";
#endif
}
}
return s_configPath;

6
QtScrcpy/util/path.h Normal file
View file

@ -0,0 +1,6 @@
#pragma once
class Path {
public:
static const char* GetCurrentPath();
};

7
QtScrcpy/util/path.mm Normal file
View file

@ -0,0 +1,7 @@
#include "path.h"
#import <Cocoa/Cocoa.h>
const char* Path::GetCurrentPath() {
return [[[NSBundle mainBundle] bundlePath] UTF8String];
}