导航:首页 > 文件管理 > 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如何获取选择文件相关的资料

热点内容
ugt型刀怎么编程铣外圆 浏览:972
win10主题绅士 浏览:319
苹果7p的双镜头怎么用 浏览:439
enbx文件怎么打开 浏览:632
前戏特别长的电影 浏览:348
文件管理的五大职业是指什么 浏览:351
cad桌面应用程序 浏览:998
少女卖春电影 浏览:61
如何复制word整个文件 浏览:632
和谐网站来一个 浏览:80
360wifi微信无法打开图片 浏览:185
下午我打算去看电影的英文 浏览:592
家政app有哪些优势 浏览:537
18g压缩文件如何传输 浏览:559
微信重新登陆好麻烦 浏览:439
第一次男老师和女学生电影 浏览:135
编程指令goo表示什么意思 浏览:901
网站品牌推广费用多少 浏览:220
免费日本hs网站 浏览:131
有2d剧情分屏的播放器 浏览:477

友情链接