導航:首頁 > 文件管理 > qt如何獲取選擇文件

qt如何獲取選擇文件

發布時間:2024-02-09 10:08:51

㈠ qt文件讀取

1、Qt 作為一個通用開發庫,提供了跨平台的文件操作能力。文件操作是應用程序必不可少的部分。
2、Qt5增加了QFileDevice類。途中所涉及的類及其用途簡要說明如下:
· QFlie:訪問本地文件或者嵌入資源;
· QTemporaryFile:創建和訪問本地文件系統的臨時文件;
· QBuffer:讀寫QByteArray;
· QProcess:運行外部程序,處理進程間通訊;
· QTcpSocket:TCP協議網路數據傳輸;
· QUdpSocket:傳輸 UDP 報文;
· QSslSocket:使用 SSL/TLS 傳輸數據;
· QFileDevice:新增加的類,提供了有關文件操作的通用實現。
3、這其中,QProcess、QTcpSocket、QUdpSoctet和QSslSocket是順序訪問設備。所謂「順序訪問」,是指它們的數據只能訪問一遍:從頭走到尾,從第一個位元組開始訪問,直到最後一個位元組,中途不能返回去讀取上一個位元組;QFile、QTemporaryFile和QBuffer是隨機訪問設備,可以訪問任意位置任意次數,還可以使用QIODevice::seek()函數來重新定位文件訪問位置指針。
4、QFile主要提供了有關文件的各種操作,比如打開文件、關閉文件、刷新文件等。我們可以使用QDataStream或QTextStream類來讀寫文件,也可以使用QIODevice提供的read()、readLine()、readAll()以及write()這樣的函數。值得注意的是,有關文件本身的信息,比如文件名、文件所在目錄的名字等,則是通過QFileInfo獲取,而不是自己分析文件路徑字元串。
5、舉個例子,打開文件時,需要參數指定打開文件的模式:
Constant Value Description
QIODevice::NotOpen 0x0000 The device is not open.
QIODevice::ReadOnly 0x0001 The device is open for reading.
QIODevice::WriteOnly 0x0002 The device is open for writing.
QIODevice::ReadWrite ReadOnly | WriteOnly The device is open for reading and writing.
QIODevice::Append 0x0004 The device is opened in append mode, so that all data is written to the end of the file.
QIODevice::Truncate 0x0008 If possible, the device is truncated before it is opened. All earlier contents of the device are lost.
QIODevice::Text 0x0010 When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.QIODevice::Unbuffered 0x0020 Any buffer in the device is bypassed.

㈡ QT裡面用treeview顯示的目錄和文件選中後怎麼打開

1、需要引入兩個庫:
#include <windows.h>;#include <shellapi.h>。
2、使用ShellExecuteA這個函數。//調用計算器
3、ShellExecuteA(NULL,"open","calc.exe",NULL,NULL,SW_SHOWNORMAL);
4、實現本文標題的功能:ShellExecuteA(0,"open","explorer.exe","C:\\WINDOWS\\a.png",NULL,SW_SHOWNORMAL)。

㈢ 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
}

㈣ 《qt》中如何提取當前目錄下指定後綴名的文件

QFileInfoList InfoList = QDir(qApp->applicationDirPath()).entryInfoList();//獲取當前目錄所有文件
QFileInfoList SuffixInfoList;//定義放提取文件的List
//遍歷
foreach(QFileInfo fileInfo, QFileInfoList)
{
if(!fileInfo.isFile()) continue;//不是文件繼續,只用於加速,可不加
//後綴不區分大小寫,需要區分直接用「==」
if(0==fileInfo.suffix().compare(後綴), Qt::Caseinsensitive)
{
SuffixInfoList << fileInfo;//指定後綴,加入列表
}
}

以上純為手打,不是Copy的,可能有些拼寫錯誤大小寫錯誤什麼的,望諒解

㈤ Qt如何讀取數據文件

#include <QFile>

#include <QString>
#include <QIODevice>
#include <QDebug>
#include <QTextStream>

void Read(QString Filename)//名字自己定,需要帶路徑
{

QFile mFile(Filename);

if(!mFile.open(QFile::ReadOnly|QFile::Text))

{

qDebug()<<"could not open file for read!";

return;

}

QTextStream in(&mFile);

QString mtext = in.readAll();

// mtext = mtext.trimmed();

qDebug()<<mtext;

if(mtext == "")

qDebug()<<" read over!";

mFile.flush();

mFile.close();
}

㈥ 如何用Qt打開excel並獲取其中的內容

1.如下,是下載的一個Excel VBA參考手冊,內容不算太全!

2.Excel讀取
為了便於測試,假設已存在一個excel文件,操作內容已經被紅色標記出來。如下所示:
主要讀取內容:
標題
工作表數目
工作表名稱
起始行
起始列
行數
列數
單元格內容
代碼如下:

QAxObject excel("Excel.Application");
excel.setProperty("Visible", true);
QAxObject *work_books = excel.querySubObject("WorkBooks");
work_books->dynamicCall("Open (const QString&)", QString("E:/test.xlsx"));
QVariant title_value = excel.property("Caption"); //獲取標題

qDebug()<<QString("excel title : ")<<title_value;

QAxObject *work_book = excel.querySubObject("ActiveWorkBook");

QAxObject *work_sheets = work_book->querySubObject("Sheets"); //Sheets也可換用WorkSheets

閱讀全文

與qt如何獲取選擇文件相關的資料

熱點內容
pdf安裝後打開文件自動退出 瀏覽:493
游戲卸載後怎麼刪掉數據 瀏覽:72
少兒編程加盟網站哪個好 瀏覽:133
蘋果7下載不了東西 瀏覽:304
列印機一次性可以打多少份文件 瀏覽:864
qq個人說明關於惡魔 瀏覽:508
美圖秀秀溶圖教程貼吧 瀏覽:240
如何備份access資料庫 瀏覽:188
小米平板2win10編程 瀏覽:232
txt文件占內存多少 瀏覽:453
java論文3000字 瀏覽:121
linuxftp看不到文件 瀏覽:929
安卓手機被偷了密碼嗎 瀏覽:351
舊手機怎麼將裡面數據清理干凈 瀏覽:686
小學編程校隊的學生有哪些特點 瀏覽:401
做機床維修和數控編程哪個好 瀏覽:877
學習汽車防盜編程哪裡好 瀏覽:977
mac里的文件消失 瀏覽:603
mstsc用戶名密碼 瀏覽:135
歸並排序java非遞歸 瀏覽:882

友情鏈接