loading qm file using QTranslator - qt

I'm trying to use translation files. I went through all the procedures:
created ts file, translated it, but when I run the application, the language is still the same as before.
I worked on the Nokia example, just like in the instructions.
What could be my problem?
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTranslator* translator=new QTranslator(0);
if(QFile::exists("hellotr_la.qm"))
qWarning("failed-no file");
if(! translator->load("hellotr_la.qm"))
qWarning("failed loading"); //the warning appears ****
app.installTranslator(translator);
}

Where are the .qm files located? Your code is attempting to load the file from the current working directory, which can be anything during runtime. Specify a directory path in the call to QTranslator::load:
QTranslator* translator = new QTranslator();
if (translator->load("hellotr_la", "/path/to/folder/with/qm/files")) {
app.installTranslator(translator);
}
Translations can be loaded from Qt resources, so it is a good idea to bundle them inside your executables. Then you would load them somewhat like this:
QTranslator* translator = new QTranslator();
if (translator->load("hellotr_la", ":/resources/translations")) {
app.installTranslator(translator);
}

The answer was already given in a comment, but I want to point it out clearly.
The first warning uses a wrong condition:
if(QFile::exists("hellotr_la.qm"))
qWarning("failed-no file");
It should be:
if(!QFile::exists("hellotr_la.qm"))
qWarning("failed-no file");
Since you only saw the second warning, but not the first one, the problem is indeed that the file was not found. Make sure that the working directory is what you expect it to be or (better) use the resource system as explained by andref.

Based on the example, can you simply try this :
QTranslator translator;
translator.load("hellotr_la");
app.installTranslator(&translator);
Hope it will fix your problem !
Note 1 : No pointer here.
Note 2 : No extension in your filename.

Related

Getting symlink name of ttyUSB device in Qt

I'm stuck with a problem concerning udev symlink property.
I just set udev rules so that my TTL-232R USB device has a symlink name.
However, I don't know how to extract this symlink name on my Qt project (which is actually not originally mine but someone else's). In the first place, the code gets the portname using libudev library and function udev_device_get_devnode(struct udev_device), which returns ttyUSB0. But I don't want the absolute name, just the symlink name. Unfortunately, I can't seem to find some similar function in the libudev library to get the symlink name i just set with udev rules.
Is there another library or way to do that? I'm a bit lost..
Regards,
try QString QFile::symLinkTarget() const
#include <QFile>
...
QFile file('/sym/link/target');
qDebug() << file.symLinkTarget();
...

openDDS not generating typesupportc header

Im running into an issue with getting TAO_idl not generating typesupportc header. the dds_TAOv2_all.sln builds just fine and all the examples generate their respectful typesupport files including the typesupportc.h file that is necessary for the typesupport_var in my IDL file.
module X {
#pragma DCPS_DATA_TYPE "X::packet"
#pragma DCPS_DATA_KEY "X::packet from"
typedef sequence<octet> binary;
struct packet {
string from;
long packet_id;
long count;
long timer;
binary mydata;
};
};
the Xtypesupportc.h was genereated before, but ever since I had to reload DDS(DDS is compiled configured etc) when I run tao_idl and openDDS_idl with the x.idl file the xtypesupportc and xtypesupports don't get created and thus I can't register the type. any obvious thing that I am doing wrong? thank you.

QObject::tr() not translating language properly

QLocale systemLocale;
LOG_ERROR() << "SYSTEM LANGUAGE:" << systemLocale.languageToString(systemLocale.language());
LOG_ERROR() << QObject::tr("Welcome");
The second line prints the correct language, when I change the language from the phone settings, however, "Welcome" doesn't get translated to the current system language. What could be the issue with this?
The translation is probably not loaded. Here's how you can load a file:
QTranslator translator;
QString locale_string = QLocale().name();
QString filename = QString("my_app_%1").arg(locale_string);
if (translator.load(filename, "app/native/qm")) {
app.installTranslator(&translator);
}
This would try to load translations from app/native/qm/my_app_fr.qm on a french device, for example.
Note that by default, you'll have to restart the application after changing the device language. You can use a LocaleHandler to update the translation when the phone language changes. Listen to onSystemLanguageChanged() signal, remove the old translator, then load the new one (same code as above).

Qt QFileSystemWatcher on Windows

I have the following issue: I create a QFileSystemWatcher and it runs and works nicely on Linux, but no way on Windows 7. Can you spot anything in the code that might make it not to work?
Thx.
Here is the code to initialize it:
mConfigChangeWatcher = new QFileSystemWatcher();
mConfigChangeWatcher->addPath(config_file_name);
QObject::connect(mConfigChangeWatcher,
SIGNAL(fileChanged(QString)),
this,
SLOT(configFileChanged(QString)));
and this is supposed to be the slot getting the work done:
void MyClass::configFileChanged(const QString &file)
{
qDebug() << "Changed: " << file ;
}
When you check if the file is added to the watcher using QFileSystemWatcher::files() method after the first modification in the file do you get the correct list?
I was with the issue that some applications, when modifing a file, delete the old file from the system and write it again.
Note that QFileSystemWatcher stops monitoring files once they have been renamed or removed from disk, and directories once they have been removed from disk.
I was using QFileSystemWatcher to watch an image file edited by Photoshop. Somehow the file gets removed from the list of files being watched.
I had the same problem and solved it very fast.
Within the slot that manages the fileChanged signal I noted the path disappears from files(). I simply make a check and re-add it if necessary
if (! watcher_.files().contains(path))
{
watcher_.addPath(path);
}
I hope this helps
Fabio

Start a process using QProcess

I'm trying to start Microsoft word using QProcess as following:
QString program = "WINWORD.EXE";
process->start(program);
but nothing happens.
winword.exe is on path (so when i type winword.exe word is openning up).
Is it the right way to do so ?
may be code below will help you:
QProcess *process = new QProcess(this);
QString program = "explorer.exe";
QString folder = "C:\\";
process->start(program, QStringList() << folder);
I think you are trying to execute program that doesn't consists in global $PATH windows variable, that's why winword.exe doesn't executes.
Also you may need to define absolute path to program, e.g.:
QString wordPath = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
process->start(wordPath, QStringList() << "");
For me, I need to add " characteres :
m_process->start("\"C:\\Program Files (x86)\\Notepad++\\notepad++.exe\"");
From Qt documentation:
Note: Processes are started
asynchronously, which means the
started() and error() signals may be
delayed. Call waitForStarted() to make
sure the process has started (or has
failed to start) and those signals
have been emitted.
Connect the signals mentioned in doc to some GUI control or debug output and see what happens. If there is an error, you should check the error type using QProcess::error().
If the method, where you're trying to launch external process, is finished right after your code, e.g.:
void foo() {
...
QString program = "WINWORD.EXE";
process->start(program);
}
and variable
process
was declared as local variable, it will be destroyed at the end of method and no external process will be started - or correctly you won't see it because it will be destroyed right after start.
It was the reason for similar issue in my case. Hope it helps.
You can just set the working directory:
myProcess = new QProcess();
myProcess->setWorkingDirectory("C:\\Z-Programming_Source\\Java-workspace\\Encrypt1\\bin\\");
Or do it at start:
myProcess->start("dir \"My Documents\"");
At start() you can enter a command for the console... read the manual.
I prefer the first option. More readable.
QProcess *pro = new QProcess;
QString s = "\"C:\Users\xyz\Desktop\Example.exe";
pro ->start(s);

Resources