Obj-C 實現 QFileDialog函數

來源:互聯網
上載者:User

標籤:

Obj-C 實現 QFileDialog函數(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};//選擇檔案
if (MacGetOpenFileName(strDefFile.toStdString().c_str(), "txt,png", chOpenFileName))//多個尾碼用“,”間隔,支援所有檔案格式用“”
{
    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;//選擇檔案清單
QString strDefFile;//預設檔案路徑
if (MacGetOpenFileNames(strDefFile.toStdString().c_str(), "txt,png", vFileList))//多個尾碼用“,”間隔,支援所有檔案格式“”
{
    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};//選擇檔案夾
QString strDefFile;//預設檔案路徑
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};儲存檔案
QString strDefFile;//預設檔案路徑
if (MacGetSaveFileName(strDefFile.toStdString().c_str(), "txt,png", chSaveFile))//多個尾碼用“,”間隔
{
    printf("Save file path=%s",chSaveFile);
}

Obj-C 實現 QFileDialog函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.