How to run Qt5 applications with a specific translation? - qt

I'm adding translations to a Qt5 project and I'd like to run the application with a specific translation to test it with a different language. I already have the .ts files and generated the corresponding .qm files. However, I found no information on how to run Qt5 applications with a specific translation. I've found some references based on setting the LANG environment variable in linux but I had no success.
Does anyone know if it's possible to run a Qt5 application with a specific language? If it is, what's the best way to set which language is used by a Qt5 application?

Add TRANSLATIONS += lang_ua.ts to your .pro file. Then add to your main()
QApplication a(argc, argv);
QTranslator translator;
translator.load("lang_ua");
a.installTranslator(&translator);
For changing translation load another file in translator.load().
All your translations will be read with QObject::tr()

Related

Trouble with setting QT style to external QSS file

I found a QSS file online (http://tech-artists.org/forum/showthread.php?2359-Release-Qt-dark-orange-stylesheet) that I would like to use as the style in my app. I have been trying multiple different ways of importing this file into my program, but every time I run my program, it does not successfully open the file.
Currently I have the file saved as myStyle.qss in the same directory as my project. I have also been hearing about inserting the file into a qrc file. Is this necessary in order to open it, or just a convenient way of storing the file?
My code so far is:
QApplication a(argc, argv);
QFile file(":/myStyle.qss");
file.open(QIODevice::ReadOnly);
QString style(file.readAll());
a.setStyleSheet(style);
file.close();
I have seen this block of code in multiple different places, so I am fairly certain that I have the majority of it right and that my main problem is just in my file placement, or that I have the wrong file path written down.
Thanks!
":/myStyle.qss"it is path which uses for Qt resource system. As you said, this file in the same directory, so try to set nornal path, "myStyle.qss", but be carefully because Qt will search this file in the build directory, so you should place file in build directory.
But when your qss file will be done, then save it in the resources. There are many examples how to do this in the web (for example this. In this case you will need your current path ":/myStyle.qss"
Why do you need resource?
Resources are build into exe file so you never lost this files, user can't delete it or rewrite. If user delete your qss file, all your style will fail, so you should protect it.

Integrate QT GUI DLL into my application

please note that I am not a native in English. sorry for any mistake.
I am very new to QT(just started yesterday) and have only few experiences with MFC.
I want to know how to integrate QT GUI DLL into non QT application.
I made this QT GUI DLL from the wizard: I simply chosen QT Gui Application and in .pro I changed "TEMPLATE = app" to "TEMPLATE = lib", as well as changing source code.
I attached source code here, you may looks at it.
http://cfile208.uf.daum.net/attach/025A524151C3E65D1B5E63
in the zip file, sources in folder "gui" does creating GUI DLL.
sources in folder "main" actually loads DLL and try to call the function in DLL.
they compiles well, but it seems they do not work. it gets an error called "there should be only one application object" when I start main.exe
What is the problem?
Don't create QApplication object in your library. There must be only one QApplication object, and it's already created by the main app.
If you need to access QApplication object from your library, use qApp macro to obtain a pointer to the QApplication.

Where are QT platform dlls supposed to go?

I have created a DLL that uses some functionality from QTWebKit, that I then access through JNA on the java side. On my machine (which QT is installed on obviously) it works fine. When I move it to another machine to to test it that does not have qt installed I get:
Failed to load platform plugin "windows". Available platforms are:
My google fu as pointed me to the fact that I need to also include platform DLLs, namely qwindows.dll and qminimal.dll. According to the QT5 documentation in Deploying an Application on Windows it sounds like when deploying an executable it would be in a folder called platforms in the save directory as the executable.
In contrast to user plugins, Qt plugins have to be put into
subdirectories matching the plugin type. As we want to deploy the
windows platform plugin it has to be put into a "platforms"
subdirectory.
That leads me to my dilemma. I have a dll, not an executable. So I have no clue where to put the platform folder. I have tried to place it in the same directory I am executing my test application, but that did not work.
So where do I place the platform directory in order for QT to be able to find it?
Edit:
Since I have not had much feedback, maybe there is a better way to word/approach this question.
How do I tell QT where to find the platform DLLs?
It seems like there has to be a way to accomplish this. When I run it on my machine, it ends up looking in C:\Qt2\Qt5.0.2\5.0.2\msvc2012_64\plugins\platforms. So it seems there m
I have found two possible solutions for the scenario that you need to create a QApplication inside a DLL or library (basically different from the normal create Qt application that has an exe).
The easiest solution is to set the system environment variable QT_QPA_PLATFORM to the folder that you expect the platforms to be located at. I did not like this solution do to the fear that it could interfere with other applications installed on the end system.
The next solution is to take advantage of the command line parameters that a normal QT application would use. In a normal Qt application QApplication would be created in your main similar to:
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
//other code
return app.exec();
}
Well we have to create argv and argc anyway at this point so use that to our advantage.
char *argv[] = {"YourAppName","-platformpluginpath", "C:/your/path/to/dll/folder", NULL};
int argc = sizeof(argv) / sizeof(char*) - 1;
QApplication app(argc, argv);
NOTE: Even now googling for -platformpluginpath I could not find any information for it. Or information on what other command line parameters may be available to use with QT. I found it by digging into the QT source code while looking for a solution. So if anyone happens to have a link to a resource with that information it would be handy to leave a comment with it.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx#standard_search_order_for_desktop_applications
When you are running your program in Qt, it adjusts the environment variables to add some stuff on to your path.
The default "path" that your dll is aware of when it is being ran depends on the exe that loads it, and its working directory, its application directory, etc. If you test program that is loading your dll, is in the same folder as the dll, you probably just need to put qwindows.dll in "./platforms/".
You also should check into this function:
http://qt-project.org/doc/qt-4.8/qcoreapplication.html#setLibraryPaths
http://qt-project.org/doc/qt-4.8/qcoreapplication.html#libraryPaths
Hope that helps. Good luck.

Get path of file in external library

I have 2 Qt projects that I've made. One builds a library the other builds a GUI that uses that library. My project with the library includes a bunch of shaders that I've made. Within my GUI I want to reference the path to these shaders. My approach is to have a static function in my library called getLibraryPath() that returns a QString referencing the path on my file system to the library.
I've tried using QDir::currentPath() and QApplication::applicationFilePath() and other similar functions that return paths but they all return paths referencing my GUI's path, not the library's path even though the function to get the path is located inside the library.
Within my GUI I try to reference a shader by doing: Shader->addShaderFromSourceFile(QGLShader::Vertex, MyExternalLib::getLibraryPath() + "/shaders/MyVertexShader.vs");
A library is just a bunch of code. It has no notion of having its own path. The normal way of doing what you intend to is:
If the library is a separately installed product -- set a registry entry in the installer, and check it at runtime. This will be platform specific, unless the library is written in Qt -- then you can use QSettings, but make sure that you set your company name, application, etc. appropriately as it's not done by QApplication for you anymore.
If the library is installed as a part of your application, in the same folder tree: use QCoreApplication::applicationDirPath() and add a relative path between the application executable and the files you want to this path.
I was able to solve this by adding the files I want to reference to a qrc file within my library project. By doing this, I could then reference the qrc path and the files inside of them easily.

Import Qt resources when are main.cpp and GUI classes in dependend static lib project,

Scenario:
I have MSVC2005 with the Qt Visual Studio AddIn installed. In my solution there is a project A consisting of multiple plain C++ code modules, which is built to an executable A.exe. Project A does not contain a .cpp file with a main(), but has a dependency on a project B.
B is a Qt project built as a static lib. It consists on multiple GUI classes an a Qt-typical main.cpp(). Compiling an linking A and B works like a charm and I have a Qt-application. I made some forks of A (Afork1, Afork2, etc.) that all share the same interface to the GUI but have different customizations of the underlying business logic. I can batch build A.exe, Afork1.exe, ..., etc. and they all will have the same GUI, which is pretty neat.
Problem:
In B there is a .qrc file with multiple images, icons, etc. that are used in the GUI. The GUI classes are crafted with Qt Designer and I added the ressources to the GUI widgets. After building and running A.exe I cannot see these icons and images, the space for them is reserved in the widgets but not filled with the content. I guess, the ressources are not linked into the executable. Is there a way to ensure the linking of ressources that are part of Qt static lib project?
You need to initialize the resources explicitly. From the Qt docs:
If you have resources in a static
library, you might need to force
initialization of your resources by
calling Q_INIT_RESOURCE() with the
base name of the .qrc file. For
example:
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Q_INIT_RESOURCE(graphlib);
...
return app.exec();
}

Resources