當前位置:首頁 » 文件管理 » qt打開文件夾選擇文件

qt打開文件夾選擇文件

發布時間: 2025-02-23 00:07:24

① Qt 有沒有文件夾對話框,用來選擇文件夾用的

QUrl QFileDialog::getExistingDirectoryUrl(QWidget * parent = 0, const QStrir

② qt 打開文件的幾種方式

在/usr 目錄下有configurefile1.xml 文件,在程序的執行文件所在目錄下有個test文件夾,test里有configurefile2.xml ;configurefile3.xml 一:工程里使用後綴為qrc的資源文件: 比如 資源文件里的內容為:<RCC<qresource <file alias="configfile1"/usr/configurefile1.xml</file <file alias="configfile2"test/configurefile2.xml</file <filetest/configurefile3.xml</file </qresource</RCC(其中test是在工程執行文件所在路徑下的一個文件夾) 這樣在程序中: QFile file1(":/configfile1"); QFile file2(":/configfile2"); QFile file3(":/test/configurefile3.xml"); file1.open(QFile::ReadOnly); file2.open(QFile::ReadOnly); file3.open(QFile::ReadOnly);上述三條打開文件的操作都可以成功打開對應文件, 但是如果在資源文件里 如果對某個文件的引用里加了alias別名的操作,比如QFile file2(":/test/configurefile2.xml"); file2.open(QFile::ReadOnly);將打開文件失敗。 QT里:/表示對資源的引用,不是表示當前目錄 二 使用相對路徑 QFile file3("test/configurefile3.xml"); file3.open(QFile::ReadOnly);OPEN成功 三 使用絕對路徑

③ Qt 如何在打開的文件夾中滑鼠選中某文件

是在打開的文件夾中自動選中某文件吧?不需要用滑鼠。

試試看這一段:


boolOpenFolderAndSelectFile(constchar*filePath)
{
#ifdefQ_OS_WIN

LPITEMIDLISTpidl;
LPCITEMIDLISTcpidl;
LPSHELLFOLDERpDesktopFolder;
ULONGchEaten;
HRESULThr;
WCHARwfilePath[MAX_PATH+1]={0};

::CoInitialize(NULL);

if(SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))
{
//IShellFolder::ParseDisplayName要傳入寬位元組
LPWSTRlpWStr=NULL;
//#ifdef_UNICODE
//_tcscpy(wfilePath,strFilePath);
//lpWStr=wfilePath;
//#else
MultiByteToWideChar(CP_ACP,0,(LPCSTR)filePath,-1,wfilePath,MAX_PATH);
lpWStr=wfilePath;
//#endif

hr=pDesktopFolder->ParseDisplayName(NULL,0,lpWStr,&chEaten,&pidl,NULL);
if(FAILED(hr))
{
pDesktopFolder->Release();
::CoUninitialize();
returnFALSE;
}

cpidl=pidl;

//SHOpenFolderAndSelectItems是非公開的API函數,需要從shell32.dll獲取
//該函數只有XP及以上的系統才支持,Win2000和98是不支持的,考慮到Win2000
//和98已經基本不用了,所以就不考慮了,如果後面要支持上述老的系統,則要
//添加額外的處理代碼
HMODULEhShell32DLL=::LoadLibraryA("shell32.dll");
//ASSERT(hShell32DLL!=NULL);
if(hShell32DLL!=NULL)
{
typedefHRESULT(WINAPI*pSelFun)(LPCITEMIDLISTpidlFolder,UINTcidl,LPCITEMIDLIST*apidl,DWORDdwFlags);
pSelFunpFun=(pSelFun)::GetProcAddress(hShell32DLL,"SHOpenFolderAndSelectItems");
//ASSERT(pFun!=NULL);
if(pFun!=NULL)
{
hr=pFun(cpidl,0,NULL,0);//第二個參數cidl置為0,表示是選中文件
if(FAILED(hr))
{
::FreeLibrary(hShell32DLL);
pDesktopFolder->Release();
::CoUninitialize();
returnFALSE;
}
}

::FreeLibrary(hShell32DLL);
}
else
{
pDesktopFolder->Release();
::CoUninitialize();
returnFALSE;
}

//釋放pDesktopFolder
pDesktopFolder->Release();
}
else
{
::CoUninitialize();
returnFALSE;
}

::CoUninitialize();
returnTRUE;

#else
QStringpathIn(filePath);
QStringListscriptArgs;
scriptArgs<<QLatin1String("-e")<<QString::fromLatin1("tellapplication"Finder"torevealPOSIXfile"%1"").arg(pathIn.replace('\','/'));
QProcess::execute(QLatin1String("/usr/bin/osascript"),scriptArgs);
scriptArgs.clear();
scriptArgs<<QLatin1String("-e")<<QLatin1String("tellapplication"Finder"toactivate");
QProcess::execute("/usr/bin/osascript",scriptArgs);
returntrue;
#endif
}
熱點內容
scratch少兒編程課程 發布:2025-04-16 17:11:44 瀏覽:642
榮耀x10從哪裡設置密碼 發布:2025-04-16 17:11:43 瀏覽:368
java從入門到精通視頻 發布:2025-04-16 17:11:43 瀏覽:88
php微信介面教程 發布:2025-04-16 17:07:30 瀏覽:310
android實現陰影 發布:2025-04-16 16:50:08 瀏覽:794
粉筆直播課緩存 發布:2025-04-16 16:31:21 瀏覽:346
機頂盒都有什麼配置 發布:2025-04-16 16:24:37 瀏覽:213
編寫手游反編譯都需要學習什麼 發布:2025-04-16 16:19:36 瀏覽:818
proteus編譯文件位置 發布:2025-04-16 16:18:44 瀏覽:367
土壓縮的本質 發布:2025-04-16 16:13:21 瀏覽:594