QTranslator doesn't work - qt

#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

Related

Qt cannot include ui header file

I am try to build a simple qt project. For the project, the source file main.cpp is created using a general c++ editor, and the .ui file is created using qt design form, and they both are placed in the same directory. Then a .pro file is created, which reference both the main.cpp and .ui file.
The .pro file is here:
TEMPLATE = app
TARGET = gotocell
SOURCES += main.cpp
FORMS += gotocelldialog.ui
QT += widgets
And the main.cpp is:
#include <QApplication>
#include <QDialog>
#include "ui_gotocelldialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Ui::GoToCellDialog ui;
QDialog *dialog = new QDialog;
ui.setupUi(dialog);
dialog->show();
return app.exec();
}
This follows the example code: gotocell1
But this line of codes
#include "ui_gotocelldialog.h"
causes the error: no such file or directory.
I am using QtCreator4.5. It is said that QtCreator will automatically generate the header files for the created ui how to create header file. What is going wrong here?
The generated source file is created and stored within your build folder. It does not get added to your project tree view as part of QtCreator. You can find your build folder directory by looking under Tools->Options->Build&Run.
I usually add the generated files to my own project tree view as I usually like to inspect it, but since is auto-generated from the ui file, it is not committed to version control.

How to install Box2D-qml with mingw32

My environment:
Qt 5.3.1 + Mingw32
Windows 7
I tired these:
Extract box2d-qml in C:\Qt\Qt5.3.1\5.3\mingw482_32\imports and rename it as Box2D
Open cmd and cd into Box2D's directory .
Input qmake in cmd, no output
Input mingw32-make, no error output.
Input mingw32-make install, no error output. and Box2D.2.0 was generated under C:\Qt\Qt5.3.1\5.3\mingw482_32\qml directory.
Then i create empty quick project and add import Box2D 2.0 into qml.
Moving the mouse cursor on import Box2D 2.0,QtCreator pop up a message:
But when i run it got error:
QQmlApplicationEngine failed to load component
qrc:///main.qml:3 plugin cannot be loaded for module "Box2D": ?v???O?C?? 'C:/Qt/Qt5.3.1/5.3/mingw482_32/qml/Box2D.2.0/Box2D.dll' ????? Qt ?????????????C?u???????g?p?????????B (?f?o?b?N???????[?X?????C?u?????????g?p???邱??????????)
The main Cpp:
#include <QGuiApplication>
#include <QQmlApplicationEngine>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
You have 2 options to use the plugin:
Installing into $QTPATH/qml (or $QTPATH/imports) folder as system-wide QML plugin. In this case you just have to do next steps:
download the source from Github and open it with QtCreator (don't put it into system folder, put the source into some user folder)
Go to Projects tab / Run / Add deploy step
add install to Make arguments
go to Edit tab, right click on the project and select Run qmake
now build the project (don't forget to set Release profile)
right click on the project and select Deploy;
The plugin dll (include all you need, Box2D code etc) will be installed into qt folder.
.
Compiling the plugin as part of your project. In this case you just have to do next steps:
download the source from Github and put the plugin source folder into your project folder (myproject/qml-box2d/, for example)
add the line into your .pro file: include(qml-box2d/box2d_lib.pri)
add this code into your main.cpp file:
#include <box2dplugin.h> // <-- this line
int main(int argc, char *argv[])
{
...
Box2DPlugin plugin; // and these
plugin.registerTypes("Box2D"); // 2 lines
...
}
Rerun qmake and recomplile the project. Now you can use Box2D items in your QML files.

Error while compiling with qmake, qt?

I tried to write a simple qt application and I get this error when I try to compile it
#include <QApplication>
#include <QTextEdit>
int main(int argv, char **args)
{
QApplication app(argv, args);
QTextEdit textEdit;
textEdit.show();
return app.exec();
}
_
qmake file.cpp
/home/j/qtf.cpp:4: Extra characters after test expression.
/home/j/qtf.cpp:6: Extra characters after test expression.
/home/j/qtf.cpp:8: Extra characters after test expression.
/home/j/qtf.cpp:11: Extra characters after test expression.
Where is the error
In order to compile your application you have to:
Create a project file,
Generate makefiles,
Build the project.
To perform these steps simply invoke the corresponding commands:
qmake -project (generates project file)
qmake (generates makefile)
make (build the project)

Why doesn't qmake add "QT += widgets" to *.pro files automatically?

I am trying to build a simple "Hello World" example in QT5.
I am compiling the code using the below steps
qmake-qt5 -project
qmake-qt5
make
I am getting the below error
main.cpp:1:24: fatal error: QApplication: No such file or directory
#include <QApplication>
^
compilation terminated.
When I read QT forums, its mentioned to add "QT += widgets" to the *.pro file. After doing this the code is compiled.
Question: Why do I need to add "QT += widgets" to the *.pro file manually ? Why doesn't qmake do it automatically ?
Note: I am using Ubuntu
Code
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}
The QT variable in the .pro file is used to specify the modules which are used in the project.
And as the qmake manual mentions:
By default, QT contains both core and gui, ensuring that standard GUI applications can be built without further configuration.
The Qt Widgets module is not linked by default and has thus to be specified in the .pro file with QT += widgets.

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

Resources