Obj-C 实现 QFileDialog函数

本文详细介绍了Mac平台下Objective-C语言中QFileDialog的四个关键函数的使用方法,包括获取打开文件名、获取打开文件列表、获取现有目录和获取保存文件名,并提供了具体的调用例子。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (0 != [nsFormat length])
    {
        NSArray *nsFormatArray = [nsFormat componentsSeparatedByString:@","];
        [nsPanel setAllowedFileTypes:nsFormatArray];
    }
    
    memset(pChOpenFile, 0, 256);
    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[256] = {0};//选择文件
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 (0 != [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=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;
}

调用例子:

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());
    }
}

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, 0, 256);
    
    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] >= 1)
        {
            NSString *nsDirectoryPath = [nsSelectFiles objectAtIndex:0];
            const char *pChDirectoryPath = [nsDirectoryPath UTF8String];
            while ((*pChSelectDir++ = *pChDirectoryPath++) != '\0');
            bRet = true;
        }
    }
    
    [nsStrFilePath release];
    nsStrFilePath = nil;
    [pool drain];
    return bRet;

}

调用例子:

char chDirectory[256] = {0};//选择文件夹
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 (0 != [nsFormat length])
    {
        NSArray *nsFormatArray = [nsFormat componentsSeparatedByString:@","];
        [nsPanel setAllowedFileTypes:nsFormatArray];
    }
    
    memset(pChSaveFile, 0, 256);
    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[256] = {0};保存文件
QString strDefFile;//默认文件路径
if (MacGetSaveFileName(strDefFile.toStdString().c_str(), "txt,png", chSaveFile))//多个后缀用“,”间隔
{
    printf("Save file path=%s",chSaveFile);
}

转载于:https://www.cnblogs.com/sz-leez/p/4314629.html

from PySide2.QtWidgets import QFrame, QDialog, QVBoxLayout, QHBoxLayout, \ QPushButton, QLineEdit, QWidget, QLabel, QSpinBox, QComboBox, QCheckBox, \ QFileDialog from PySide2 import QtCore from maya import cmds, mel, OpenMaya import xgenm as xg # mel.eval('source "xgen.mel"; xgen()') # UTILS ----------------------------------------------------------------------------------- def geometry_instancer_to_interactive_grooming(descriptions, prefix=None): if prefix: interactive_descriptions = cmds.xgmGroomConvert(descriptions, prefix=prefix) else: interactive_descriptions = cmds.xgmGroomConvert(descriptions) return cmds.listRelatives(interactive_descriptions, parent=True) def rebuildHair(base): cmds.xgmRebuildSplineDescription(base, cv=cmds.getAttr('{0}.cvCount'.format(base))) def query_shape(obj): try: shp = cmds.listRelatives(obj, c=True)[0] except: raise ValueError('Can\'t find description\'s shape for {}'.format(obj)) return shp def query_base(shp): try: base = cmds.listConnections('{}.inSplineData'.format(shp))[0] except: raise ValueError('Can\'t find splineBase for {}'.format(shp)) return base def convert_to_descriptions(xgen_insts): # xgen_insts = cmds.ls(sl=True, tr=True) for xgen_inst in xgen_insts: cmds.setAttr('{}.v'.format(xgen_inst), 1) descriptions = geometry_instancer_to_interactive_grooming(xgen_insts) cmds.hide(xgen_insts) return xgen_insts, descriptions def export_description_to_alembic(description, file_path): command = '-file "{}"'.format(file_path) command += ' -df ogawa' command += ' -fr 1 1' command += ' -wfw' command += ' -obj {}'.format(description) cmds.xgmSplineCache(export=True, j=command) def extract_guide_curves(xgen_inst): guidesName = xg.descriptionGuides(xgen_inst) cmds.select(guidesName, r = True) curves = mel.eval('xgmCreat
03-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值