Obj-C 实现 QFileDialog函数(getOpenFileName/getOpenFileNames/getExistingDirectory/getSaveFileName)

1.getOpenFileName

/**************************************************************************
@QFileDialog::getOpenFileName
@param pChDefFilePath:[input]Default file path
@param pChFormat:[input]Save file format
@param pChOpenFile:[output]Get the open file path
@return: true, success;
**************************************************************************/
bool MacGetOpenFileName(const char *pChDefFilePath, const char *pChFormat, char *pChOpenFile)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
bool bRet = false; NSOpenPanel *nsPanel = [NSOpenPanel openPanel];
[nsPanel setCanChooseFiles:YES];
[nsPanel setCanChooseDirectories:NO];
[nsPanel setAllowsMultipleSelection:NO]; NSString *nsDefFilePath = [[NSString alloc] initWithUTF8String: pChDefFilePath];
[nsPanel setDirectory:nsDefFilePath]; NSString *nsFormat = [[NSString alloc] initWithUTF8String: pChFormat];
if ( != [nsFormat length])
{
NSArray *nsFormatArray = [nsFormat componentsSeparatedByString:@","];
[nsPanel setAllowedFileTypes:nsFormatArray];
} memset(pChOpenFile, , );
NSInteger nsResult = [nsPanel runModal];
if (nsResult!=NSFileHandlingPanelOKButton && nsResult!=NSFileHandlingPanelCancelButton)
{
//QTBUG:recall runModal when QMenu action triggered;
[nsDefFilePath release];
nsDefFilePath = nil;
[nsFormat release];
nsFormat = nil;
[pool drain];
return MacGetOpenFileName(pChDefFilePath, pChFormat, pChOpenFile);
}
if (nsResult == NSFileHandlingPanelOKButton)
{
NSString *nsOpenFile = [[nsPanel URL] path];
const char *pChOpenFilePath = [nsOpenFile UTF8String];
while ((*pChOpenFile++ = *pChOpenFilePath++) != '\0');
bRet = true;
} [nsDefFilePath release];
nsDefFilePath = nil;
[nsFormat release];
nsFormat = nil; [pool drain];
return bRet;
}

调用例子:

char chOpenFileName[] = {};//选择文件
if (MacGetOpenFileName(strDefFile.toStdString().c_str(), "txt,png", chOpenFileName))//多个后缀用“,”间隔,支持所有文件格式用“”
{
printf("Open file path=%s",chOpenFileName);
}

2.getOpenFileNames

/**************************************************************************
@QFileDialog::getOpenFileNames
@param pChDefFilePath:[input]Default file path
@param pChFormat:[input]Save file format
@param vFileNameList:[output]Get the open file list
@return: true, success;
**************************************************************************/
bool MacGetOpenFileNames(const char *pChDefFilePath, const char *pChFormat, std::vector<std::string> &vFileNameList)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
bool bRet = false; NSOpenPanel *nsPanel = [NSOpenPanel openPanel];
[nsPanel setCanChooseFiles:YES];
[nsPanel setCanChooseDirectories:NO];
[nsPanel setAllowsMultipleSelection:YES]; NSString *nsDefFilePath = [[NSString alloc] initWithUTF8String: pChDefFilePath];
[nsPanel setDirectory:nsDefFilePath]; NSString *nsFormat = [[NSString alloc] initWithUTF8String: pChFormat];
if ( != [nsFormat length])
{
NSArray *nsFormatArray = [nsFormat componentsSeparatedByString:@","];
[nsPanel setAllowedFileTypes:nsFormatArray];
} vFileNameList.clear();
NSInteger nsResult = [nsPanel runModal];
if (nsResult!=NSFileHandlingPanelOKButton && nsResult!=NSFileHandlingPanelCancelButton)
{
//QTBUG:recall runModal when QMenu action triggered;
[nsDefFilePath release];
nsDefFilePath = nil;
[nsFormat release];
nsFormat = nil;
[pool drain];
return MacGetOpenFileNames(pChDefFilePath, pChFormat, vFileNameList);
}
if (nsResult == NSFileHandlingPanelOKButton)
{
NSArray *nsSelectFileArray = [nsPanel URLs];
unsigned int iCount = [nsSelectFileArray count];
for (unsigned int i=; i<iCount; i++)
{
std::string strSelectFile = [[[nsSelectFileArray objectAtIndex:i] path] UTF8String];
vFileNameList.push_back(strSelectFile);
} if (iCount > )
{
bRet = true;
}
} [nsDefFilePath release];
[nsFormat release]; [pool drain];
return bRet;
}

调用例子:

std::vector< std::string> vFileList;//选择文件列表
QString strDefFile;//默认文件路径
if (MacGetOpenFileNames(strDefFile.toStdString().c_str(), "txt,png", vFileList))//多个后缀用“,”间隔,支持所有文件格式“”
{
unsigned int iCount = vFileList.size();
for (unsigned int i=; i<iCount; i++)
{
printf("Selected file[%i]=%s\n", i, vFileList.at(i).c_str());
}
}

3.getExistingDirectory

/**************************************************************************
@QFileDialog::getExistingDirectory
@param pChFilePath:[input]Default select file path
@param pChAgentNums: [output]Selected directory path
@return: true, get directory path success;
**************************************************************************/ bool MacGetExistDirectoryPath(const char *pChFilePath, char *pChSelectDir)
{ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
bool bRet = false; NSOpenPanel *nsPanel = [NSOpenPanel openPanel];
[nsPanel setCanChooseFiles:NO];
[nsPanel setAllowsMultipleSelection:NO];
[nsPanel setCanChooseDirectories:YES];
NSString *nsStrFilePath = [[NSString alloc] initWithUTF8String:pChFilePath];
[nsPanel setDirectory:nsStrFilePath]; memset(pChSelectDir, , ); NSInteger nsResult = [nsPanel runModal];
if (nsResult!=NSFileHandlingPanelOKButton && nsResult!=NSFileHandlingPanelCancelButton)
{
//QTBUG:recall runModal when QMenu action triggered;
[nsStrFilePath release];
nsStrFilePath = nil;
[pool drain];
return MacGetExistDirectoryPath(pChFilePath,pChSelectDir);
}
if (nsResult == NSFileHandlingPanelOKButton)
{
NSArray *nsSelectFiles = [nsPanel filenames];
if ([nsSelectFiles count] >= )
{
NSString *nsDirectoryPath = [nsSelectFiles objectAtIndex:];
const char *pChDirectoryPath = [nsDirectoryPath UTF8String];
while ((*pChSelectDir++ = *pChDirectoryPath++) != '\0');
bRet = true;
}
} [nsStrFilePath release];
nsStrFilePath = nil;
[pool drain];
return bRet; }

调用例子:

char chDirectory[] = {};//选择文件夹
QString strDefFile;//默认文件路径
if (MacGetExistDirectoryPath(strDefFile.toStdString().c_str(), chDirectory))
{
printf("Selected diroctory=%s",chDirectory);
}

4.getSaveFileName

/**************************************************************************
@QFileDialog::getSaveFileName
@param pChDefFilePath:[input]Default file path
@param pChFormat:[input]Save file format
@param pChSaveFile:[output]Get the save file path
@return: true, success;
**************************************************************************/
bool MacGetSaveFileName(const char *pChDefFilePath, const char *pChFormat, char *pChSaveFile)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
bool bRet = false; NSSavePanel *nsPanel = [NSSavePanel savePanel];
[nsPanel setCanCreateDirectories:YES]; NSString *nsDefFilePath = [[NSString alloc] initWithUTF8String: pChDefFilePath];
[nsPanel setDirectory:nsDefFilePath]; NSString *nsFormat = [[NSString alloc] initWithUTF8String: pChFormat];
if ( != [nsFormat length])
{
NSArray *nsFormatArray = [nsFormat componentsSeparatedByString:@","];
[nsPanel setAllowedFileTypes:nsFormatArray];
} memset(pChSaveFile, , );
NSInteger nsResult = [nsPanel runModal];
if (nsResult!=NSFileHandlingPanelOKButton && nsResult!=NSFileHandlingPanelCancelButton)
{
//QTBUG:recall runModal when QMenu action triggered;
[nsDefFilePath release];
nsDefFilePath = nil;
[nsFormat release];
nsFormat = nil;
[pool drain];
return MacGetSaveFileName(pChDefFilePath, pChFormat, pChSaveFile);
}
if (nsResult == NSFileHandlingPanelOKButton)
{
NSString *nsSaveFile = [[nsPanel URL] path];
const char *pChSaveFilePath = [nsSaveFile UTF8String];
while ((*pChSaveFile++ = *pChSaveFilePath++) != '\0');
bRet = true;
} [nsDefFilePath release];
nsDefFilePath = nil;
[nsFormat release];
nsFormat = nil; [pool drain];
return bRet;
}

调用例子:

char chSaveFile[] = {};保存文件
QString strDefFile;//默认文件路径
if (MacGetSaveFileName(strDefFile.toStdString().c_str(), "txt,png", chSaveFile))//多个后缀用“,”间隔
{
printf("Save file path=%s",chSaveFile);
}

Obj-C 实现 QFileDialog函数的更多相关文章

  1. 关于obj和基本类通过函数参数传进去执行是否改变原来的值

    var obj = { p1 : 1, p2 : 2 }; (function(_/* 这个东东是地址的应用哦 */){ _.p1 = 3, _.p2 = 4 })(obj) var i = 2; ( ...

  2. Javascript函数的几种写法

    最近在看某个插件的源码时,总是看到各种不同风格的js函数的写法.(怪我只是初级水平,看的一头雾水) 于是想找点资料,总结总结,心里不清不楚的总是很别扭! 1.常规写法 // 函数写法 function ...

  3. JavaScript箭头函数 和 generator

    箭头函数: 用箭头定义函数........           var fun = x=>x*x alert(fun(2))            //单参数   var fun1 = ()=& ...

  4. 应用C#和SQLCLR编写SQL Server用户定义函数

    摘要: 文档阐述使用C#和SQLCLR为SQL Server编写用户定义函数,并演示用户定义函数在T-SQL中的应用.文档中实现的 Base64 编码解码函数和正则表达式函数属于标量值函数,字符串分割 ...

  5. 常量函数、常量引用参数、常量引用返回值[C++]

    1. 关于常量引用正像在C语言中使用指针一样,C++中通常使用引用 有一个函数... foo()并且这个函数返回一个引用...... & foo()...., 一个指向位图(Bitmap)的引 ...

  6. javascript 函数及作用域总结介绍

    在js中使用函数注意三点: 1.函数被调用时,它是运行在他被声明时的语法环境中的: 2.函数自己无法运行,它总是被对象调用的,函数运行时,函数体内的this指针指向调用该函数的对象,如果调用函数时没有 ...

  7. Python中的repr()函数

    Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数. 函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式. 在python的官方AP ...

  8. 【C++对象模型】函数返回C++对象的问题

    在深入C++对象模型中,对于形如 CObj obj1 = Get(obj2); 的形式,编译器会在将其改变为如下 Get(obj, CObj&  obj1); 将赋值操作符左边的变量作为函数的 ...

  9. JavaScript学习总结-技巧、有用函数、简洁方法、编程细节

    整理JavaScript方面的一些技巧.比較有用的函数,常见功能实现方法,仅作參考 变量转换 //edit http://www.lai18.com var myVar = "3.14159 ...

随机推荐

  1. nodejs模块学习: connect2解析

    nodejs模块学习: connect2 解析 nodejs 发展很快,从 npm 上面的包托管数量就可以看出来.不过从另一方面来看,也是反映了 nodejs 的基础不稳固,需要开发者创造大量的轮子来 ...

  2. Spring 加载静态资源

    <mvc:default-servlet-handler/> JSP 中通过标签加载js文件或者link标签加载css文件等静态资源时要在springmvc的xml文件中配置以上设置请求就 ...

  3. Unity3D Image 组件附入图片问题

    作为新手经常会看到有个Image的组件 代码中理所当然的public 发现图片并不能附入其中, 解决办法直接 public Sprite 就可以了

  4. linux+windows mysql导入导出sql文件

    linux下 一.导出数据库用mysqldump命令(注意mysql的安装路径,即此命令的路径):1.导出数据和表结构:mysqldump -u用户名 -p密码 数据库名 > 数据库名.sql# ...

  5. [luogu P2184] 贪婪大陆 [树状数组][线段树]

    题目背景 面对蚂蚁们的疯狂进攻,小FF的Tower defence宣告失败……人类被蚂蚁们逼到了Greed Island上的一个海湾.现在,小FF的后方是一望无际的大海, 前方是变异了的超级蚂蚁. 小 ...

  6. Docker初步了解

    Docker 是什么 https://www.docker.com/ Docker 这个单词英文原意是码头工人,搬运工的意思,这个搬运工搬运的是各种应用的容器. 官方的说法是,Docker 是提供给开 ...

  7. SQL Prompt 破解之道

    1. 下载SQL Prompt 5.3.4.1,是个压缩包,里面有三个文件 免登录免积分下载地址:http://download.csdn.net/detail/caizz520/4557385 1) ...

  8. TypeScript--变量及类型的那些事儿

    变量(类型)声明 格式:关键字  变量名称:类型=值 (强类型)     /   关键字  变量名称=值 例子: Array数组声明   Tuple元组类型 声明一个包含多类型的数组: Enum枚举类 ...

  9. HTML5 开发APP(打开相册以及图片上传)

    我们开发app,常常会遇到让用户上传文件的功能.比如让用户上传头像.我公司的业务要求是让用户上传支付宝收款二维码,来实现用户提现的功能.想要调用相册要靠HTML Plus来实现.先上效果图 基本功能是 ...

  10. Bower的入门

    接触Bower一年多了,今天对于它的有一些疑问,并且写此博文记录之. 首先把Bower的官网附上: https://bower.io/ (一)什么是Bower Bower:就是一个前端包管理工具.能够 ...