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. 最近学习java时的记录

    1.java 的变量一共分为三种,类变量,局部变量,成员变量 类变量就是 加static修饰符的变量 2.java 的修饰符可分为两大类,一 可访问修饰符 protected private publ ...

  2. Tomcat Connector的三种运行模式

    详情参考: http://tomcat.apache.org/tomcat-7.0-doc/apr.html http://www.365mini.com/page/tomcat-connector- ...

  3. Codeforces Round #418 (Div. 2).C two points

    C. An impassioned circulation of affection time limit per test 2 seconds memory limit per test 256 m ...

  4. DOM0级事件处理、DOM2级事件处理

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Android使用ViewPager实现导航菜单

    首先设置页面的Fragment布局: public class TabFragment extends ListFragment { @Override public void onViewCreat ...

  6. 51nod_1181:质数中的质数

    题目链接 #include<bits/stdc++.h> using namespace std; typedef long long LL; const LL N=1e6; //vect ...

  7. SSH三大框架的各自工作流程

    一.Struts2的工作流程:1.用户在客户端发起请求,客户端会初始化一个servlet容器请求:2.servlet容器把请求会传递给context容器,context容器找到目标web工程.3.进行 ...

  8. tensorflow max_pool(最大池化)应用

    1.最大池化 max pooling是CNN当中的最大值池化操作,其实用法和卷积很类似. tf.nn.max_pool(value, ksize, strides, padding, name=Non ...

  9. Linux编译安装程序(使用configure、make、 make install)

    以安装vim为例. (vim 是vi的升级版本,它不仅兼容vi的所有指令,而且还有一些新的特性在里面). 1.获取源文件 首先进入/usr/local下(只是为了方便处理安装文件,位置随意) 用git ...

  10. (转)log4j(三)——如何控制不同级别的日志信息的输出?

    一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 1 老规矩,先来个栗子,然后再聊聊感受 package test.log4j.test3; import org.apac ...