I use qt linguist to translate my program in different languages but it doesn't show he translated text in the program.
I set proper fonts and add .ts file to TRANSLATIONS.
I use lupdate and lrelease commands.
how can i create .ts file also? (I create text file and change the format to .ts is it correct way?)
Revie Qt Translation.
To translate the app dynamically:
1 - Open Qt command terminal and go to your project folder.
2 - Get all the translatable string from your project
lupdate -pro Example.pro -ts example.ts
3 - Translate all the string to the language you want using QLinguist
4 - Generate the .qm file with all translation executing:
lrelease example.ts
5 - Add this file, example.qm, as a resource to your project to include it with the executable file. Resource System
6 - Now, use QTranslator to translate the app:
QTranslator* translator = new QTranslator;
if(translator->load(":/"+example.qm)){
qApp->removeTranslator(translator); // Remove the translator if was used before
qApp->installTranslator(translator); // Install again the translator to force a new translation.
qDebug() << "Translation success!" ;
}else{
qDebug() << "Error file not found!";
}
7 - You can handle the translation event using changeEvent:
void MainWindow::changeEvent(QEvent* event)
{
if (event) {
switch(event->type()) {
// When the translator is loaded this event is send.
case QEvent::LanguageChange:
break;
// Whem the system language changes this event is send.
case QEvent::LocaleChange:
//retranslate the ui.
break;
default:
break;
}
}
QMainWindow::changeEvent(event);
}
That's all folks!
Related
Im using this function so far:
bool MSSQL::checkSettings()
{
QString settingsFile = "someDir/setup.ini";
QString fileName(settingsFile);
QFile file(fileName);
if(QFileInfo::exists(fileName)) {
return true;
}
else {
qDebug() << "Error: No INI File Found on path:" << settingsFile;
return false;
}
}
which works fine.
However when I wish to use an online ini file which is reachable (because can be opened via browser link), then its a false, for example if i use:
QString settingsFile = "http://localhost/something/setup.ini";
which QT should be able to read, then it doesnt work...
any ideas?
I dont see where you are using QSettings at all. You are using QFile, which is representation of a local file.
To your question:
QSettings does not able to read/write setting from URL. You have to download it manually before instantiating QSettings, See QNetworkAccessManager.
Or you may try to make your own settings provider, by using: QSettings::registerFormat() static function.
I would like to transfer a file from a certain directory to another, But when i use QDir. rename it seems to always fail.
void handler::moveToTempFolder(QString localFilePath)
{
qDebug()<<localFilePath; <--- "C:/Users/user1/Pictures/IMG_00000009.jpg"
qDebug()<<"/TempFiles/" + getFileNameFromPath(localFilePath); <---- "/TempFiles/IMG_00000009.jpg" a folder that is in the same location as the .pro
QDir dir;
if(dir.rename(localFilePath,"/TempFiles/" + getFileNameFromPath(localFilePath)))
qDebug()<<"Success";
else
qDebug()<<"Failed";
}
I am getting Failed on my terminal. can anybody help?
Qt does not have a method to access the project folder that you .pro file is in. Probably because once you deploy this, any device other than yours will not have that folder.
It can however access the folder where the executable is with QDir::currentPath().
Also, I'm not sure how you defined getFileNameFromPath - but you can do that with QFileInfo.
void MainWindow::moveToTempFolder(QString localFilePath)
{
QFileInfo fileInfo(localFilePath);
QDir dir;
QString tempFilePath = dir.currentPath() + "/TempFiles/" + fileInfo.fileName();
if(dir.rename(localFilePath, tempFilePath))
{
qDebug() << "Success";
} else {
qDebug() << "Failed";
}
}
Note: When you run this from Qt Creator, your /TempFiles/ folder will most likely be in the same folder as your /debug/ and /release/ folders (one level up from your executable). But once you deploy this (or run it manually from the .exe) - /TempFiles/ will be in the same folderas the .exe
The working directory while the program is running is the build folder, not the project folder. My guess would be that is your problem.
I have a problem with writing to file, the problem is :
If I write the directory ":/Files/Scores.txt" it won't write anything but it reads from same directory.
But if I used this directory "D:/TicTacToe/TicTacToe/Scores.txt" it writes and reads but i will give the game to my instructor and the path won't be same and the file won't open, any ideas ?!
My write code:
void Write ( QString file)
{
QFile sfile(file);
if(!sfile.open(QFile::ReadOnly |QFile::Text))
{
return;
}
QTextStream in(&sfile);
QString lscores =sfile.readAll() ;
sfile.close();
if(!sfile.open(QFile::WriteOnly |QFile::Text))
{
return;
}
lscores=" "+Xscore+"\t"+" "+Oscore+"\n"+lscores;
QTextStream out(&sfile);
out <<lscores;
sfile.close();
}
Resource files (the ones you put in .qrc file) are read only, as MrEricSir mentioned. If you want to have some configuration/score file with your app you can for example use QApplication::applicationDirPath()
QString settingsFile = QApplication::applicationDirPath() % QLatin1Literal("/scores.txt");
This way you will have always absolute path to the file inside your app directory.
You can also use QDesktopServices::storageLocation with QDesktopServices::DataLocation parameter to obtain data location for your application.
Does Qt's QML language provide any kind of "strict" mode? In particular, there are two features I'd like:
Application crash on reference to undefined or null (e.g. foo = bar when foo is an existing property but bar is not defined, and foo.bar when foo is null)
"hard" asserts (the console.assert feature does not crash the application).
1. Use qml lint
Run qmllint on all .qml and .js files in your build setup
find ./myproject -type f -regex ".*\.\(qml\|js\)" -exec "$QT_DIR/bin/qmllint" \{\} +
2. Crash app on QML error/warning
Write a custom QDebug message handler function static void handler(QtMsgType type, const QMessageLogContext& context, const QString &message); you register via qInstallMessageHandler(&MyQDebugMessageHandler::handler); that turns QML warnings into fatal logs:
if (type == QtWarningMsg)
{
auto fatalWarnings = std::vector<QString>{
QStringLiteral("ReferenceError:"),
QStringLiteral("is not a type"),
QStringLiteral("File not found"),
};
for (const auto &s : fatalWarnings)
{
if (message.contains(s))
{
type = QtFatalMsg;
break;
}
}
}
Then make sure that QDebug messages of type QtFatalMsg crash the app.
3. Crash on console.assert()
console.assert() creates errors but nothing specific to detect them. So adapt point 2. to crash the app on errors as well.
I'm developing an application for Symbian S60 phones using the Qt Nokia SDK, which sends requests and receives responses from a webservice in every view i have.
The problem with this, is that it always asks the user to choose a accesspoint.
So what i want is to choose an accesspoint when the application starts, and use that throughout the application.
So i found this example: http://wiki.forum.nokia.com/index.php/How_to_set_default_access_point_using_Qt_Mobility_APIs
but i got following error:
undefined reference to 'QtMobility::QNetworkConfigurationManager::QNetworkConfigurationManager(QObject*)
i'm also getting more of these errors from other classes from QMobillity, like:
undefined reference to 'QtMobility::QNetworkSession::open()
.pro file:
CONFIG += mobility
MOBILITY += bearer
header:
#include <qmobilityglobal.h>
#include <QtNetwork>
#include <QNetworkSession>
#include <QNetworkConfigurationManager>
QTM_USE_NAMESPACE;
cpp file:
QNetworkConfigurationManager manager;
const bool selectIap = (manager.capabilities()& QNetworkConfigurationManager::CanStartAndStopInterfaces);
QNetworkConfiguration defaultIap = manager.defaultConfiguration();
if(!defaultIap.isValid() && (!selectIap && defaultIap.state() != QNetworkConfiguration::Active))
{
qDebug() << "Network access point NOT found";
// let the user know that there is no access point available
msgBox->setText(tr("Error"));
msgBox->setInformativeText(tr("No default access point available"));
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setDefaultButton(QMessageBox::Ok);
msgBox->topLevelWidget();
msgBox->exec();
}
else
{
qDebug() << "Network access point found and chosen";
}
session = new QNetworkSession(defaultIap,this);
session->open();
Anyone got an idea of what could be wrong?
Have you tried adding this to the .PRO file?
CONFIG += network