Obj-c Implementing the Qfiledialog function

Source: Internet
Author: User

Obj-c Implementing the Qfiledialog function (getopenfilename/getopenfilenames/getexistingdirectory/getsavefilename)

/**************************************************************************
@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] Initwithformat: @ "%s", Pchdeffilepath];
[Nspanel Setdirectory:nsdeffilepath];

NSString *nsformat = [[NSString alloc] Initwithformat: @ "%s", Pchformat];
if (0! = [Nsformat length])
{
Nsarray *nsformatarray = [Nsformat componentsseparatedbystring:@ ","];
[Nspanel Setallowedfiletypes:nsformatarray];
}

memset (pchopenfile, 0, 256);
if (Nsfilehandlingpanelokbutton = = [Nspanel RunModal])
{
NSString *nsopenfile = [[Nspanel URL] path];
int ilen = [Nsopenfile length];
const char *pchopenfilepath = [Nsopenfile utf8string];
memcpy (Pchopenfile, Pchopenfilepath, Ilen);
BRet = true;
}

[Nsdeffilepath release];
[Nsformat release];

[Pool drain];
return bRet;
}

eg
Char chopenfilename[256] = {0};//Select file
if (Macgetopenfilename (strdeffile.tostdstring (). C_STR (), "Txt,png", Chopenfilename))//Multiple suffixes with "," interval, supporting all file formats with ""
{
printf ("Open file path=%s", chopenfilename);
}

/**************************************************************************
@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] Initwithformat: @ "%s", Pchdeffilepath];
[Nspanel Setdirectory:nsdeffilepath];

NSString *nsformat = [[NSString alloc] Initwithformat: @ "%s", Pchformat];
if (0! = [Nsformat length])
{
Nsarray *nsformatarray = [Nsformat componentsseparatedbystring:@ ","];
[Nspanel Setallowedfiletypes:nsformatarray];
}

Vfilenamelist.clear ();
if (Nsfilehandlingpanelokbutton = = [Nspanel RunModal])
{
Nsarray *nsselectfilearray = [Nspanel URLs];
unsigned int iCount = [Nsselectfilearray count];
for (unsigned int i=0; i<icount; i++)
{
std::string strselectfile = [[[[Nsselectfilearray objectatindex:i] path] utf8string];
Vfilenamelist.push_back (Strselectfile);
}

if (ICount > 0)
{
BRet = true;
}
}

[Nsdeffilepath release];
[Nsformat release];

[Pool drain];
return bRet;
}

eg
std::vector< std::string> vfilelist;//Select File list
QString strdeffile;//Default File path
if (Macgetopenfilenames (strdeffile.tostdstring (). C_STR (), "Txt,png", vfilelist))//Multiple suffixes with "," interval, supporting all file formats ""
{
unsigned int iCount = Vfilelist.size ();
for (unsigned int i=0; i<icount; i++)
{
printf ("Selected file[%i]=%s\n", I, vfilelist.at (i). C_STR ());
}
}

/**************************************************************************
@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] initwithformat:@ "%s", Pchfilepath];
[Nspanel Setdirectory:nsstrfilepath];

memset (pchselectdir, 0, 256);
if (Nsfilehandlingpanelokbutton = = [Nspanel RunModal])
{
Nsarray *nsselectfiles = [Nspanel filenames];
if ([Nsselectfiles count] >= 1)
{
NSString *nsdirectorypath = [Nsselectfiles objectatindex:0];
int ilen = [Nsdirectorypath length];
const char *pchdirectorypath = [Nsdirectorypath utf8string];
memcpy (Pchselectdir, Pchdirectorypath, Ilen);
BRet = true;
}
}

[Pool drain];
return bRet;
}

eg
Char chdirectory[256] = {0};//Select folder
QString strdeffile;//Default File path
if (Macgetexistdirectorypath (strdeffile.tostdstring (). C_STR (), chdirectory))
{
printf ("Selected diroctory=%s", chdirectory);
}

/**************************************************************************
@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] Initwithformat: @ "%s", Pchdeffilepath];
[Nspanel Setdirectory:nsdeffilepath];

NSString *nsformat = [[NSString alloc] Initwithformat: @ "%s", Pchformat];
if (0! = [Nsformat length])
{
Nsarray *nsformatarray = [Nsformat componentsseparatedbystring:@ ","];
[Nspanel Setallowedfiletypes:nsformatarray];
}

memset (pchsavefile, 0, 256);
if (Nsfilehandlingpanelokbutton = = [Nspanel RunModal])
{
NSString *nssavefile = [[Nspanel URL] path];
int ilen = [Nssavefile length];
const char *pchsavefilepath = [Nssavefile utf8string];
memcpy (Pchsavefile, Pchsavefilepath, Ilen);
BRet = true;
}

[Nsdeffilepath release];
[Nsformat release];

[Pool drain];
return bRet;
}

eg
Char chsavefile[256] = {0}; Save file
QString strdeffile;//Default File path
if (Macgetsavefilename (strdeffile.tostdstring (). C_STR (), "Txt,png", Chsavefile))//Multiple suffixes with "," interval
{
printf ("Save file path=%s", chsavefile);
}

Obj-c Implementing the Qfiledialog function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.