Unable to translate GUI in QML - c++

I have written following code to load .ts file
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTranslator>
#include <QDebug>
#include <QDir>
int main(int argc, char *argv[])
{
#if defined(Q_OS_WIN)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
QTranslator translator;
if(translator.load("<absolute-path>/translating-qml_ge.qm"))
{
qDebug() << "Loaded";
app.installTranslator(&translator);
}
else
qDebug() << "failed";
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
I have checked return value of 'installTranslator' which is true.
but still UI is not getting translated.

Related

How to play a wma audio file using Qt or any other cpp framework on linux?

To make the code below work:
#include <QApplication>
#include <QPushButton>
#include <QMediaPlayer>
#include <iostream>
int main(int argc, char **argv) {
QApplication application(argc, argv);
QPushButton button("Click Me");
button.show();
auto player = new QMediaPlayer;
player->setMedia(QUrl::fromLocalFile("/home/bj/m/y.wma"));
player->setVolume(50);
QObject::connect(&button, &QPushButton::clicked, [player](bool) {
std::cout << "xxx" << std::endl;
player->play();
});
return application.exec();
}
Now this code can play mp3 by installing gst-libav. But how to play wma?
I solved. ugly plugin is required. sudo pacman -S gst-plugins-ugly

Why messages printed after closing main window in Qt C++?

With this code
#include "mainwindow.h"
#include <QApplication>
#include <iostream>
#include <QDir>
#include <QTextStream>
int main(int argc, char *argv[]){
QApplication a(argc, argv);
QTextStream out(stdout);
out << QDir::currentPath();
std::cout << "Why is that?";
MainWindow mainWindow;
mainWindow.show();
return a.exec();
}
Both messages printed only after closing Main Window of my app, why is this?
I tried to debug, debugger thinks that he done with this line, but I see no messages.
extern std::ostream cout; is buffered, so it may choose when to flush its buffer to stdout. In your case, it is doing it when your program is terminating.
You can tell std::ostream to flush using std::flush, as such:
std::cout << "Why is that?" << std::flush;

QApplication changing value of std::to_string

It seems that Qt changes the 'interpretation' of the returned string of std::to_string(double).
Example:
#include <iostream>
#include <QApplication>
#define VERSION 1.0
#define VSIZE 3
int main(int argc, char** argv) {
std::string version = std::to_string(VERSION).substr(0, VSIZE);
std::cout << version << std::endl; // everything fine
QApplication app(argc, argv);
std::cout << std::to_string(VERSION).substr(0, VSIZE); // no good
return 0;
}
The output is:
1.0
1,0
I think it's Qt because this is the thinnest example where this happens, and removing the QApplication declaration makes it work again. How can I fix this?

Checking for existing .exe file Qt

why when i want to check .exe file this program will print false but when i add .txt file and check prints True?
i mean its only recognize .txt file not .exe
#include <QCoreApplication>
#include <QFile>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QFile read("C:\\Users\\Ramin\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\Time.exe");
if(read.exists()){
qDebug()<<"True";
}
else{
qDebug()<<"False";
}
return a.exec();
}
thanks in advance

Error: SIGABRT Signal meaning : Aborted" when trying to connect to the database by QT on UBUNTU

Error:
The inferior stopped because it received a signal from the Operating System.
Signal name :
SIGABRT
Signal meaning :
Aborted
Code Snippet:
#include <QCoreApplication>
#include <QDebug>
#include <QDir>
#include <QtSql>
#include <QSqlDriver>
#include <qsqldatabase.h>
#include <QSqlError>
#include <QPluginLoader>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("NNF");
db.setUserName("root");
db.setPassword("123456");
if( !db.open() )
{
qDebug() << db.lastError();
qFatal( "Failed to connect." );
}
qDebug( "Connected!" );
return a.exec();
}