How to save a file from Qrc to application directory - qt

I am using Qt 5.15.1.
If I have a file in in my Qt app's qrc file, is it possible to save the file into application's directory so that I can use that file as a normal file on the file system?
QFile my_file(":/data/file.txt");
QDir my_app_dir(QCoreApplication::applicationDirPath());
in above code, how can I save my_file into my_app_dir?
Environment:
MacOS Catalina
Qt: Commercial version 5.15.1

You just have to copy the content:
QFile my_file(":/data/file.txt");
QDir my_app_dir(QCoreApplication::applicationDirPath());
QString new_path = my_app_dir.absoluteFilePath("file.txt");
if(!my_file.copy(new_path)){
qDebug() << my_file.error() << my_file.errorString();
}

Related

Wrong work folder in Raspberry QtCreator

When I try to load a file by the relative path via QFile, gives file not found.
QFile file(fileName);
So I output the current work folder to find the problem like this:
qDebug()<<QDir(".").absolutePath();
Output: /home/username
qDebug()<<qApp->applicationDirPath();
Output: /home/username/program/testApp
The second one is the right path to the work folder. So I think QFile didn't load the file from the work folder but the Linux user folder ~. Why didn't QFile load the file refer to the work folder?
I guess this may be a bug in Raspberry's QtCreator. Because the QFile load file is working well if run in the console.
Use QStandardPaths to store data to your OS paths, in this way you would refer to consistent set of locations when you retrieve the file.
For instance:
auto path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QFile afile;
QString fileName = "/myfile.txt";
path += fileName;
afile.setFileName(path);

How to create Qt GUI application from dll called by win32 console application?

(I am not native English so Sorry if I make any grammar mistake)
I am new to Qt, started 2 days ago, and had only few experiences of WINAPI and MFC.
I am thinking of...
First, creating Qt GUI application from its dll
Second, my static library file(*.lib) will call this dll file using QLibrary.
Lastly, my console application will have its lib and its header file, and dll to create Qt GUI Application.
and my console application does not have .pro file, just created on visual studio and has only .vcproj and .sln file.
Here is the source code:
http://cfile239.uf.daum.net/attach/037B654151C4FF8D2D0EB7
I copied dll, lib and its header file into its win32 console application.
and when I compile console, I get this error message.
fatal error C1083: Cannot open include file: 'QApplication': No such file or directory
I know it is absolutely right. (cuz my lib uses QLibrary and its lib and header file is included in win32 console application..)
well, actually I do not want to have .pro file including QApplication class into my console application to fix this problem.
Is there any possible way to fix it while avoiding having .pro file?
or should I create .pro and set it to have QT library?
Thank you for reading it. :D
You should indicate your compiler Qt's path.
If you use vs,choose tools->option->project and solutions,VC++ Directories,set your Qt header files path,lib path,bin path, and set Qt's bin path to environment variable PATH.
In your static library project, try this:
create.h
#pragma once
void createQt(int argc, char* argv[]);
create.cpp
#include<QtGui/QWidget>
#include<QtGui/QApplication>
#include "create.h"
#pragma comment(lib,"qtguid4.lib")
void createQt(int argc, char* argv[])
{
QApplication app(argc,argv);
QWidget w;
w.show();
app.exec();
};
in you console program:
.cpp file:
#include "stdafx.h"
#include "create.h"
#pragma comment(lib,"CreaeQt.lib")
int main(int argc, char* argv[])
{
createQt(argc,argv);
return 0;
}

qrc : cannot find file/directory issues

I'm working on a project in which all .js and .qml files are stored in the Qt Resource file (.qrc). I've tried to import an external directory in a qml file. The external directory contains other .qml files for different purposes. I don't want to include these external directories into the .qrc file.
I get an error when I add the import path saying:
qrc:\example.qml : cannot find directory
Is there any way to include an external file or directory like this.
Found a solution in the Qt forum, http://qt-project.org/forums/viewthread/7047. For accessing any file outside QRC, use "absolute filepath" of the file.
For example:
In main.cpp file:
QString path = QDir::currentPath(); //path where the exec is present
If your file is in src/file.qml of exec folder, then you can access it like
(path += "/src/file.qml";), now path is the absolute file path for file.qml. You can access it in any of the QRC file.
QQuickView view;
view.rootContext()->setContextProperty("myFile", path);
view.setSource("qrc:/main.qml");
In main.qml file:
Loader
{
id: loadItem
source: myFile
}
Item
{
Component.onCompleted: loadItem.item
}

QVideoFrame no such file or directory found

I am trying to use QVideoFrame library
"#include "
I get no such file or directory message...Any suggestions?

QTranslator doesn't work

#include<QApplication>
#include<QTranslator>
#include<QObject>
#include<QTextCodec>
#include<QWidget>
int main(int argc, char* argv[])
{
QTextCodec::setCodecForCStrings(QTextCodec::codecForLocale());
QApplication app(argc, argv);
QTranslator translator;
translator.load("app_zh_CN.qm");
app.installTranslator(&translator);
QWidget widget;
widget.setWindowTitle(QObject::tr("Hello World!"));
widget.show();
return app.exec();
}
SOURCES += \
main.cpp
TRANSLATIONS += app_zh_CN.ts
The Gui interface is "Hello World!" also.. But in my file.qm is be translate to "你好!"(chinese)...
where is the preblem ? who can help me..
Your example works for me if I put the .qm file in the "correct" spot. (See below.) Make sure you are doing all the steps:
Run lupdate to create the .ts file.
Do your translation in Linguist and save the .ts file.
Run lrelease to compile the .ts file to a .qm file.
Copy the .qm file to the correct location.
My guess is that #4 is going bad. The documentation for QTranslator::load states:
If directory is not specified, the directory of the application's
executable is used (i.e., as applicationDirPath()).
However, I had to put the .qm file in the folder above the executable to get it to work as is. Unless I'm misunderstanding the docs, this is a Qt bug, but one that is simple to workaround. If I explicitly gave the directory as app.applicationDirPath, it worked in the executable folder. You could also specify a separate directory. For example:
translator.load("app_zh_CN.qm"); works with:
[MyApp]
app_zh_CN.qm
[debug]
MyApp.exe
translator.load("app_zh_CN.qm", app.applicationDirPath()); works with:
[MyApp]
[debug]
app_zh_CN.qm
MyApp.exe

Resources