PDA

View Full Version : Translation problems



Skepsor
27th October 2009, 13:49
Hello everyone; I'm new at the forum, and I need some help with translations.

I've made a minimal application for testing translations before going on production, and I realised that its not working. I did it as some tuts said and using c++ qt programming guide as guideline, but I'm still getting the error "instantiate QApplication object first" and what is most strange is that im only seen it (the message) when debugging with gdb. Of course translations are not working, and the text appears in its original language (spanish).
Im running on windows xp with mingw, qt4.5.1. I haven't tested it in Linux yet but from now on I've not had problems with this (linux windows, windows linux)
Here is my code:


int main(int argc, char *argv[]){
QApplication *myApp = new QApplication(argc, argv, true);
QWidget *mainWindow;
QString *trstring;
QLabel *theLabel;
QGridLayout *theGridLayout;
QTranslator theTranslator;

theGridLayout = new QGridLayout(0);

trstring = new QString(QApplication::tr("Hola"));
theLabel = new QLabel(0);
theLabel->setText(*trstring);

if(!mainWindow->centralWidget())
mainWindow->setCentralWidget(new QWidget(0));

theGridLayout->addWidget(theLabel, 0, 0, Qt::AlignLeft);
mainWindow->centralWidget()->setLayout(theGridLayout);

if(!theTranslator.load("englishTranslations", QDir::currentPath()+"/translations"))
exit(0);
myApp->installTranslator(&theTranslator);
mainWindow->show();
return (myApp->exec());
}


I don't know why is it failling, I've added the TRANSLATIONS line to the .pro file, and I've done the lupdate and lrelease execution with success. And later the translation with the liguist utility.

What I'm missing ? What could be wrong ? Is it so difficult ? Everyone seems to be doing it as something quite simple....

Thanks, regards

Nahuel

wysota
27th October 2009, 14:10
1. Create the application object on the stack.
2. You are not creating the main window anywhere but you are using it (hence the app should immediately crash - if it doesn't it means you create the window somewhere else before QApplication object is created).
3. Actually it shouldn't even compile as your mainWindow variable is a QWidget and you use it as if it were QMainWindow. If it compiles, it means that you have given us the wrong code.

Skepsor
27th October 2009, 14:23
Oh sorry....I added some things in the way.
Here the right code:



int main(int argc, char *argv[]){
QApplication *myApp = new QApplication(argc, argv, true);
QMainWindow *mainWindow;
QString *trstring;
QLabel *theLabel;
QGridLayout *theGridLayout;
QTranslator theTranslator;

mainWindow = new QMainWindow(0, Qt::Widget);
theGridLayout = new QGridLayout(0);

trstring = new QString(QApplication::tr("Hola"));
theLabel = new QLabel(0);
theLabel->setText(*trstring);

if(!mainWindow->centralWidget())
mainWindow->setCentralWidget(new QWidget(0));

theGridLayout->addWidget(theLabel, 0, 0, Qt::AlignLeft);
mainWindow->centralWidget()->setLayout(theGridLayout);

if(!theTranslator.load("englishTranslations", QDir::currentPath()+"/translations"))
exit(0);
myApp->installTranslator(&theTranslator);
mainWindow->show();
return (myApp->exec());
}


What do you mean with the first point ?
1. Create the application object on the stack. ?

Please heelp!

Thanks

wysota
27th October 2009, 15:38
I mean create it on the stack and not on the heap like you do now.

http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/first_steps/stack_and_heap.html

Also please state if you have any static or global objects.

Skepsor
27th October 2009, 15:55
Thanks for the reply.
Respect the static or global objects, i must said that what you see is what you get :). That is the code im trying, theres nothing else.
Well the stack creation as I understand neither is working.
I've tried:


QApplication app(argv, argc, true);
QApplication *myApp;
myApp = &app;

and later using the pointer myApp.

and also tryied:


QApplication app(argv, argc, true);

and later using directly app.

If this is wrong, very very wrong, It would be nice if you could just type how to do it

Thanks



Thanks.

wysota
27th October 2009, 16:08
I'm sure there is at least a bunch of include statements.

Skepsor
27th October 2009, 16:14
No, theres not bunch of includes.

take a look

#include<iostream>
#include<QtGui/QApplication>
#include<QtGui/QMainWindow>
#include<QtCore/QString>
#include<QtGui/QLabel>
#include<QtGui/QGridLayout>
#include<QtCore/QTranslator>

Regards

wysota
27th October 2009, 16:27
To be honest I don't get any warnings from your code (although I'm running Linux). Are you sure this is the same code you get warnings from?

Skepsor
27th October 2009, 16:51
No, you wont get any message, because as I said first the message appears with gdb, at least in xp. The mainly situation is if it's enought with this code to use translations, lets say.....can you make it work copying and pasting my code ? and generating the ts and qm files?
Do you (or someone else) have some actual simple working code ? I'm kind of disappointed with it, because it should work, it must work, in fact, it must work with the first code I post, because thats how everybody is doing it, in the web, in the tuts, everywere, nobody is talking about stack or heap creation, they just do as I do just that my code is not working....jeje

Thanks for the attention, hope we can solve it, I'll be here

Regards

Skepsor
27th October 2009, 20:58
Well it seems to be that I have found the problem, but I'd like someone to explain why do I get this beheaviour.
The problem is with the .pro file, in one of its lines I could see comething like...

CONFIG = qt warn_on release

I took off the release word and it works ! and if I use the debug option also works....but now I want to know why is not working in release ? What is changing ? Which are main differences between release and debug and whe shoul I use one and when another.

Thanks for all, regards

Nahuel

wysota
27th October 2009, 22:32
But what exactly is not working (I got a bit confused) - the translations?

Skepsor
28th October 2009, 14:51
Yes yes, ofcourse wht is NOT working are translations, but I could handle it taking off the release option from the CONFIG variable in the .pro file. What I'd like to know is why is it happening....?

wysota
28th October 2009, 16:38
You are using relative paths for your translation files so probably Qt just can't find them if you start the application from another directory.

Skepsor
28th October 2009, 19:14
No man, no no no, it seems you're not reading well, or you're not reading.....lets see....lets start again.

- The main problem was with translations. They weren't working, my text wasn't getting translated.
- I discovered that taking off the option 'release' from the CONFIG directive of the proyect file makes it work.
- Translations now work, but....

Please avoid thinking weird stuff, as running app in another place, or anythings that comes up to your imagination.......is simple

What I want to know NOW, before creating another thread....is WHY the translations doesn't work if I have setted the 'release' option in the CONFIG directive of my proyect.

Is it clear now ?

Regards

wysota
29th October 2009, 10:37
- I discovered that taking off the option 'release' from the CONFIG directive of the proyect file makes it work.
- Translations now work, but....
How do you start the application then? By double clicking its icon? Where is the application binary, in what directory? Is it the same for release and debug? Because usually on Windows these land in different directories. It might be that you have two versions of the translations, one translated and the other not. Please check that.


Please avoid thinking weird stuff, as running app in another place, or anythings that comes up to your imagination.......
That's not weird stuff. Please state the directory structure of your application.


Is it clear now ?

It has been clear since some time.

Skepsor
2nd November 2009, 17:03
Hello

How do you start the application then? By double clicking its icon? Where is the application binary, in what directory? Is it the same for release and debug? Because usually on Windows these land in different directories. It might be that you have two versions of the translations, one translated and the other not. Please check that.

myApplication.exe [Enter]
In the same directory of the eclipse proyect.
Yes I guess is the same for release and debug, I'm not getting two binaries.
I don't have two translated versions.

I'm gonna expose a new view:
I created an eclipse proyect.
Then I did:
qmake -o Makefile -win32 myProyectFile.pro
Then I edited the .pro and I added CONFIG = qt warn_on release
Then:
make all
So, in the end I got this:
myProyectFile.pro
Makefile
debug\ (folder)
release\ (folder)
translations\myTranslationFile.ts
translations\myTranslationFile.qm
myBinaryApp.exe
Tha's all, and I run with:
C:\Proyect\myBinaryApp.exe [Enter].

Right? And now ? Why is the release option making my app not being translated?

Thanks and regards

wysota
2nd November 2009, 18:06
Please provide a minimal compilable example (with project file and translations) reproducing the problem.

FinderCheng
3rd November 2009, 04:10
I'd like to read your code. I also got translation problems and I found it only if the class has the macro Q_OBJECT, it could be translated. If you could not put out your code I suggest you to read Qt assistant's document.

john_god
13th November 2009, 02:05
This program works fine under linux but doesnt work on Windows.
I had the same problem and it's because of this line




.......
if(!theTranslator.load("englishTranslations", QDir::currentPath()+"/translations"))
exit(0);
.......
}



In windows you start your program from the debug or release folder, not like linux wich runs from the project directory. Just copy the translations directory to your debug or release directory and it will be fine, that way you can maintain "linux compatibility". And when you deploy it just put the translation folder as a sub folder from your directory's .exe file.

Skepsor
16th June 2010, 21:04
Hello all, thanks for your replies and sorry for my delay :).
I finally did it work, but don't remember how. What is for sure is that I re-configured the .pro file adding the debug_and_release option, and configured it to put the resulting apps into diffrent dirs with the following code:


CONFIG(release, debug|release){
DESTDIR = release/
TARGET = designer }
else {
DESTDIR = debug/
TARGET = dbgDesigner
}


Also I copied the translations/ directory to debug and release folders to avoid doubtsa bout problems loading files, or different versions of translations. I also changed the line that john_god details above. I guess all that did the trick