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. 使用hexdump工具追踪EXT4文件系统中的一个文件

    昨天追踪EXT4文件系统的过程中出了点问题,就是找不到文件,于是试了一下追踪FAT32文件系统的,成功之后有了点信心,今天继续嗑EXT4文件系统,终于找到啦,记录一下. 操作系统:linux(cent ...

  2. mysql中的limit

    mysql中常使用limit做分页查询,使用方法也很简单: SELECT * FROM table LIMIT [offset,] rows #注: offset-偏移量,rows查询返回的行数 -- ...

  3. VBS连接远程Oracle

    原文链接:http://hi.baidu.com/coo_boi/item/5a2e1860ded285136995e6a7 连接方式还是用的ADO,驱动是MSDAORA. 使用oracle前,ora ...

  4. YARN框架详解

    YARN框架详解 YARN官方解释 YARN是什么 The fundamental(定义) idea of YARN is to split(分开) up the functionalities(功能 ...

  5. Java自学手记——注解

    注意区分注释和注解,注释是给人看的,注解是给程序看的. 注解的作用是代替配置文件,在servlet3.0中,就可以不再使用web.xml文件,而是所有配置都是用注解!比如注解类 @WebServlet ...

  6. windows调试工具列表

    摘自windbg帮助文档(windbg中输入.hh): Debugging Tools for Windows (安装WinDbg后这些工具都会安装在目录C:\Program Files (x86)\ ...

  7. 两个java项目,跨域访问时,浏览器不能正确解析数据问题

    @Controller@RequestMapping(value = "api/item/cat")public class ApiItemCatController { @Aut ...

  8. Spring和SpringMVC父子的容器之道---[上篇]

    Spring和SpringMVC作为Bean管理容器和MVC层的默认框架,已被众多WEB应用采用,而在实际开发中,由于有了强大的注解功能,很多基于XML的配置方式已经被替代,但在实际项目中,我们经常会 ...

  9. redhat设置开机自动连接网络

    一.设置开机自动连接网络1.用root账号登录2.打开etcsysconfignetwork-scrpts目录3.vi ifcfg-eth04.将ONBOOT改为yes 二.没有图形界面如何连接网络1 ...

  10. Android-重新包装Toast,自定义背景

    Android-重新包装Toast,自定义背景 2016-4-27 Android L 算是包装了一个自己使用的小工具. 使用Toast的目的是弹一个提示框.先看一下Toast.makeText方法. ...