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. Linux 多用户系统

    Linux OS是基于Unix系统开发而来,我们知道计算机是昂贵与稀缺的资源,所以一台计算机就要满足多个用户同时使用,即多用户的系统的思想. 实现方式:通过分时共享的策略.即让多个用户可以同时使用一台 ...

  2. day4作业小代码练习

    登录模块: 我们无论上那个网站,经常遇到这样的情况,让我们登录这个网站,流程图如下: 思路: 1.当我们登录网站的时候,我们首先会输入用户名,这个时候,有些网站会提醒我们用户名是否存在,如果我们输入的 ...

  3. PeopleCode事件和方法只用于online界面不能用于组件接口(component interface)

    在使用CI过程中,哪些方法是不能使用的.以下为PeopleBook解释的内容. 一.搜索框代码不执行:SearchInit, SearchSave, and RowSelect events 意味着使 ...

  4. 以芯片直读方式得到的全盘镜像解析及ext4日志区域解析

    之前在centos中分析了/dev/sda1下的结构,但当对象是一块以芯片直读方式作出来的全盘镜像呢? 这次以安卓手机的全盘镜像为对象,尝试按照ext4文件系统结构手动解析,加强对ext4文件系统.E ...

  5. TortoiseGit上传项目到github方法(超简单)

    Github是咱广大开发者用的非常多的项目版本管理网站,项目托管可以是私人的(private)或者公开的(public),私人的收费,一个月7美金.咱这里就只说我们个人使用的,一般都是代码对外开放的: ...

  6. 单例设计模式 Single

    一些人总结出来用来解决特定问题的固定的解决方案. 解决一个类在内存中只存在一个对象,想要保证对象的唯一. 1 为了避免其他程序过多的建立该类对象.禁止其他程序建立该类对象. 2 为了其他程序可以访问该 ...

  7. 18-EasyNetQ:发生错误的情况

    这一篇文章让我们看看在消息系统中可能发生的各种错误的情况下,看下EasyNetQ如何处理它们. 订阅服务挂了 当你写了一个windows 服务,用来订阅一个NewCustomerMessage消息. ...

  8. 分享网上搜到的Oracle中对判定条件where 1=1的正解

    今天在网上找到了Oracle中对判定条件where 1=1的正解,粘贴出来和大家分享下 1=1 是永恒成立的,意思无条件的,也就是说在SQL语句里有没有这个1=1都可以. 这个1=1常用于应用程序根据 ...

  9. 浅谈游戏中BOSS设计的思路

    对于大多数游戏来说,BOSS在其设计上都有着不可替代的作用,也是玩家印象最为深刻的一部分.近期自己也有在做BOSS的设计工作,有一些心得想要分享一下: 1.明确BOSS的设计目的 在设计之初,我们一定 ...

  10. 创建 Machine - 每天5分钟玩转 Docker 容器技术(46)

    对于 Docker Machine 来说,术语 Machine 就是运行 docker daemon 的主机.“创建 Machine” 指的就是在 host 上安装和部署 docker.先执行 doc ...