Obj-C 实现 QFileDialog函数
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函数的更多相关文章
- 关于obj和基本类通过函数参数传进去执行是否改变原来的值
var obj = { p1 : 1, p2 : 2 }; (function(_/* 这个东东是地址的应用哦 */){ _.p1 = 3, _.p2 = 4 })(obj) var i = 2; ( ...
- Javascript函数的几种写法
最近在看某个插件的源码时,总是看到各种不同风格的js函数的写法.(怪我只是初级水平,看的一头雾水) 于是想找点资料,总结总结,心里不清不楚的总是很别扭! 1.常规写法 // 函数写法 function ...
- JavaScript箭头函数 和 generator
箭头函数: 用箭头定义函数........ var fun = x=>x*x alert(fun(2)) //单参数 var fun1 = ()=& ...
- 应用C#和SQLCLR编写SQL Server用户定义函数
摘要: 文档阐述使用C#和SQLCLR为SQL Server编写用户定义函数,并演示用户定义函数在T-SQL中的应用.文档中实现的 Base64 编码解码函数和正则表达式函数属于标量值函数,字符串分割 ...
- 常量函数、常量引用参数、常量引用返回值[C++]
1. 关于常量引用正像在C语言中使用指针一样,C++中通常使用引用 有一个函数... foo()并且这个函数返回一个引用...... & foo()...., 一个指向位图(Bitmap)的引 ...
- javascript 函数及作用域总结介绍
在js中使用函数注意三点: 1.函数被调用时,它是运行在他被声明时的语法环境中的: 2.函数自己无法运行,它总是被对象调用的,函数运行时,函数体内的this指针指向调用该函数的对象,如果调用函数时没有 ...
- Python中的repr()函数
Python 有办法将任意值转为字符串:将它传入repr() 或str() 函数. 函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式. 在python的官方AP ...
- 【C++对象模型】函数返回C++对象的问题
在深入C++对象模型中,对于形如 CObj obj1 = Get(obj2); 的形式,编译器会在将其改变为如下 Get(obj, CObj& obj1); 将赋值操作符左边的变量作为函数的 ...
- JavaScript学习总结-技巧、有用函数、简洁方法、编程细节
整理JavaScript方面的一些技巧.比較有用的函数,常见功能实现方法,仅作參考 变量转换 //edit http://www.lai18.com var myVar = "3.14159 ...
随机推荐
- 前端解读Webview
作为盛行已久的开发方式,Hybrid的相关介绍已经是相当普遍了.不过看到博客园里基本上都是从android或者ios的角度来讲解的,对于h5的前端来说看起来只能是一直半解.感觉有必要从前端的角度来理解 ...
- css自定义动画在微信中无法执行的原因
这是我去年年底遇到的一个问题, 在这个过程中我发现了一个比较有趣的问题. 我们在做抽奖的时候,微信分享到朋友圈的页面里,安卓机器无法执行页面中的自定义动画(元宝的位移,进入按钮的放大缩小等等).这让我 ...
- 最近快速的过了一遍php基础语法
把in_array() 写成is_array() ;结果自己坑了自己一万:打脸一万下,先记账上
- java后端程序员1年工作经验总结
java后端1年经验和技术总结(1) 1.引言 毕业已经一年有余,这一年里特别感谢技术管理人员的器重,以及同事的帮忙,学到了不少东西.这一年里走过一些弯路,也碰到一些难题,也受到过做为一名开发却经常为 ...
- hdfs文件系统架构详解
hdfs文件系统架构详解 官方hdfs分布式介绍 NameNode *Namenode负责文件系统的namespace以及客户端文件访问 *NameNode负责文件元数据操作,DataNode负责文件 ...
- IE浏览器兼容
IE6下面元素的宽高小于16PX时 会默认以16PX显示(最小宽高) 解决办法:设置元素overflow:hidden; 当文字全是字母或数字时会超容器对溢出隐藏的样式失效, 解决办法:设置下父级 ...
- jQuery扩展方法笔记
一.方式列表: 1.jQuery.extend(Object); // jQuery 本身的扩展方法 2.jQuery.fn.extend(Object); // jQuery 所选对象扩展方法 二. ...
- PHP ORM笔记
1.ORM是什么? 经常听到程序员的面试中会问到对ORM的了解,但是一直不知道ORM是个什么鬼东西,知道有一天在百度上顺带看到才发现ORM就是我们平时在框架中一直使用的数据库对象操作.ORM(Obje ...
- Jmeter连接DB2/ORACLE/MYSQL数据库
连接DB2 1.将db2数据库驱动db2java.jar.db2jcc.jar放入jmeter的lib/下,同时也要放入本地jdk目录下例如:C:\Program Files\Java\jdk1.7. ...
- Linux 文本行列转换
使用awk进行行列转换 假设有一个文件 test ,包含的数据遵循以下格式: 同一行数据,单词之间为1个空格" " 每一行都有同样多的数据,个数相同 处理命令: awk '{for ...