Open resource file
-
I have a file in my resource, I was wondering if is possible to run invisibly (not show the form).
-
I don't understand your question.
If with "resource file" you mean a file that you have compiled into your binary using the "Qt Resource System":http://doc.qt.digia.com/qt/resources.html, it can be opened in a similar fashion to a local file by using QFile(":/filename.foo"). Alternatively, the data of the resource file can be accessed directly via memory pointer by using QResource(":/filename.foo").data(). With both ways the file won't be "visible" unless you make it visible.
-
Yes, is Qt Resource System, but i don't want to read, i want to run.
-
Run WHAT ???
If the file is an executable you cannot run it from the memory directly! Instead you will have to save it to a local file (e.g. in TEMP folder), then use QProcess to run the executable from the local path...
@QString localPath = QString("%1/program.exe").arg(QDir::tempPath());
QFile localFile(localPath);
if(!localFile.open(QIODevice::WriteOnly))
{
qWarning("Oups!");
return;
}QResource resourceFile(":\program.exe");
if(localFile.write(resourceFile.data(), resourceFile.size()) != resourceFile.size())
{
qWarning("Oups!");
return;
}
localFile.close();QProcess::execute(localPath);@
-
@MuldeR
Not work!
Error message: no match method.
I find resourceFile.data() is const uchar *, but write must use const uchar *....QFile file(myPath+"qqq.xxx"); file.open(QIODevice::WriteOnly); // QDataStream writer(file); QResource resourceFile(":/xxx/Resources/xxx/qqq.xxx"); file.write(resourceFile.data(), resourceFile.size()); file.close();
I am using Qt 5.4.
-
No nee to use the resource class, resource files can be accessed by QFile
QFile resourceFile(":/path/to/resource");
-
This post is deleted! -
@raf924
I got this file with 0 byte.How to fixed it?
QFile file(myPath+"/qqq.xxx"); file.open(QIODevice::WriteOnly); //QDataStream writer(file); QFile resourceFile(":/xxx/Resources/xxx/qqq.xxx"); // QResource resourceFile(":/xxx/Resources/xxx/qqq.xxx"); file.write(resourceFile.readAll()); file.close();
In fact, I just want to set the default file for the beinning user.
When user install this app, then he can get those free file.But I don't know how to set those code and files....
-
@mkdh You forgot to open the resource file.