Qt linguist doesn't translate the strings in ubuntu14.04 - c++

There is a simple code for translating. It runs in Ubuntu 14.04.
#include <QtGui>
#include <QApplication>
#include <QLabel>
#include <QTextCodec>
int main (int argc,char *argv[])
{
QApplication app(argc,argv);
QTranslator qtr;
qtr.load("hello.qm");
app.installTranslator(&qtr);
//QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF-8"));
QLabel *label=new QLabel(QObject::tr("hello"));
label->show();
retrun app.exec();
}
I had followed the below steps to translate hello to the chinese 你好 by Qt linguist:
add TRANSLATIONS +=hell0.ts to hello.pro and lupdate.
open the Qt linguist and find that the interface is
It is not like the normal Qt linguist interface. This is my first problem. How can i fix it?
Then i print 你好 to translate 'hello', but nothing is displayed in the blank.
I still push the 'done' button.
I use gedit to open the hello.ts and see that it has been tranlated to 你好.
The picture is
(3) I use 'lrelease' command and get the hello.qm
Last, i run the code as aboved. But the result is just to print 'hello' out.
It doesn't translate 'hello' to 你好. This is my second problem.
I also had used other methods like QtTextCodec::setCodecForTr instead of Qt linguist,but it still doesn't work.

Try to call retranslateUI(this) so the Translation will be updated

Related

Is it possible to add background image on QWidget without using QtCreator?

Having trouble adding background image on the widget, even though I referenced the recent codes online.
This is my current code for main.cpp:
#include <QApplication>
#include <QWidget>
int main (int argc, char **argv){
QApplication app (argc,argv);
QWidget *w=new QWidget();
w->setStyleSheet("background-image: url(:/cover.jpg);");
w->setWindowTitle("Test");
w->show();
return app.exec();
}
After executing the code, how come the widget remains blank? Thanks in advance!
QtCreator is an IDE designed by Qt. It's just an interface.
I checked your implementation and I don't see anything wrong. It also work well on my pc. Can you check your image url or try with another image ?
Btw, if you're on linux, try removing : character after url ;
w->setStyleSheet("background-image: url(/cover.jpg);");
EDİT:
If jpg is in the same directory with your application, it should be ;
w->setStyleSheet("background-image: url(./cover.jpg);");
You can give a full path to avoid this kind of errors.
"Is it possible to add background image on QWidget without using QtCreator?"
Yes, of course it is.
QtCreator is just an IDE. You don't need to use it at all to write code using the Qt library.
Just as you can use it to write code that does not use Qt at all.

Using QPainter with QCoreApplication

We have an application (QCoreApplication) that takes some images as input, does something to them, and exports them again. We now need to add some text to the images, and tried to do this with the QPainter class. It all worked well when using it in one of our other apps (using QApplication), but not in our main QCoreApplication app.
Here is the code:
void drawTextOnImage(QImage* image, const QString& text, const QFont& font)
{
QPainter p;
if (!p.begin(image)) return;
p.setFont(font);
p.drawText(image->rect(), Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text);
p.end();
}
The application crashes on the drawText line.
Any ideas?
It is a very simple text, so suggestions without using Qt will also be appreaciated.
When using classes from "Qt Gui" like QPainter, you are supposed to use a QGuiApplication, not a QCoreApplication.
You might get lucky and be able to make some GUI stuff works while using only a QCoreApplication. But as you have discovered, it makes your application very brittle. Some classes like QPixmap will print an error message, but others will just crash.
The same is applicable with "Qt Widget": if you use a widget related class, you must use a QApplication.
Note that since QApplication inherits QGuiApplication, if you have a QApplication you can use "Qt Gui".
In case you need to run a non-GUI application on something without a windowing system, you need, aside from creating an instance of QGuiApplication, to also choose an appropriate Qt platform.
For me, offscreen platform worked fine. I was generating images with textual elements and saving them to files on a headless Raspberry Pi. My code then was like the example below. Note that setenv is a POSIX function and may need a replacement on Windows, though I'm not sure whether windowless Windows is a thing at all.
#include <stdlib.h>
#include <QImage>
#include <QPainter>
#include <QGuiApplication>
int main(int argc, char** argv)
{
setenv("QT_QPA_PLATFORM","offscreen",1);
QGuiApplication app(argc,argv);
QImage img(128,128, QImage::Format_RGB888);
img.fill(Qt::white);
QPainter p(&img);
p.drawText(QPoint(0,64), "Works!");
img.save("/tmp/test.png");
}

Using pdf.js with Qt5.8

I am trying to make a pdf viewer in Qt5.8 .I know that poppler is a choice for Qt but I want to do this using pdf.js .I dont know how to embed pdf.js with Qt5.8. I have seen the hello world documentation of pdf.js but it didn't helped. Please help me.
Thanks in advance .
The basic idea would be to have some widget to display HTML if you want to make use of pdf.js - it seems that QWebEngineView (makes use of Chromium) could do the job as it takes a minimum of code to get your first implementation done.
Having an installation of pdf.js on your computer and a minimalistic gui application (QT Widgets project) prepared with your QT Creator, you could use the following code to have the basics:
#include "mainwindow.h"
#include <QApplication>
#include <QWebEngineView>
static QString pathToPDFjs = QString("file:///path-to/pdfjs-1.8.170-dist/web/viewer.html");
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow win;
QWebEngineView *view;
QString pdfFileURL;
//you could parse a widget to get the file name
pdfFileURL = QString("file:///path-to-your/file.pdf");
//init the view and attach it to the ui
view = new QWebEngineView();
win.setCentralWidget(view);
win.show();
//auto-load feature in pdf.js, pass file=filename as parameter
view->load(QUrl::fromUserInput(pathToPDFjs + QString("?file=") + pdfFileURL));
view->show();
return app.exec();
}
From there on you can add extra functionality to your user interface.
You could even add modifications to your installation of pdf.js (if needed).
If you'd need to call JavaScript on your pdf.js, you can make use of the view's page (a QWebEnginePage) which in turn can runJavaScript.
No idea why you want to use pdf.js, but you might want to have a look at QtLabs PDF module. It seems pretty recent and well integrated with current Qt. (and I guess it's more efficient than JavaScript code)
If you want to try it out, here’s how to get started:
git clone git://code.qt.io/qt-labs/qtpdf
cd qtpdf
git submodule update --init --recursive
qmake
make
cd examples/pdf/pdfviewer
qmake
make
./pdfviewer /path/to/my/file.pdf

How to print version of a Qt GUI application to console

I have a GUI application written using Qt Widgets. I've added versioning and I'm planning to write an update manager too. In order this to work the update manager must be able to determine the version of my app. I thought of implementing this by running my app with a version switch then parsing it's output. I did a research and I found out that Qt has some kind of built in solution for this.
Here is an example:
#include "mainwindow.h"
#include <QApplication>
#include <QCommandLineParser>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QApplication::setApplicationVersion("1.0.0");
QCommandLineParser parser;
auto versionOption = parser.addVersionOption();
parser.process(app);
if (parser.isSet(versionOption))
{
MainWindow w;
w.show();
return app.exec();
}
return 0;
}
If I launch this app with a -v or --version command line switch, I get a message box containing the version information.
I need to achieve the same, only the information should be printed to standard output. If the app is launched with the version switch it should only display the version in the console then close.
How could I print the version information to the standard console output with a GUI app?
As we cleared some points in comments let's move on. ;)
Take a look at the documentation (http://doc.qt.io/qt-5/qapplication.html#details). In the detail section you see a sane way how to properly parse and handle command line options.
And here (https://stackoverflow.com/a/3886128/6385043) you can see a possibility for writing to standard output. Notice the QDebug caveat.
In my opinion, stick to the text file. You may generate it during build with qmake using the variable VERSION, which you can also use with QApplication::setApplicationVersion(QString).

QT HTML5 with Menu Bar like real program

I'm working on an QT HTML5 application and I was wondering how i could add an top menu just like normal program ( with the default file, tools, help... options ).
I think that I've to change something in the html5applicationviewer.cpp, but I've got 0 knowledge of that ( I'm learning this... )
Even if you could point me a little bit in the right direction, I'm grateful. I've searched around, but found absolutely nothing about this topic ( but maybe i didn't search right... )
If you need more info, please ask.
Simplest way to add normal "desktop"-style menus to a Qt app is to use QMainWindow which has good support for menus.
Here's something to get you started. First I created the default HTML5 Qt application with Qt Creator (SDK version 5.2.1). Then I edited the main.cpp and added some lines. Result is below, original lines without comment and all added lines with comment.
#include <QApplication>
#include <QMainWindow> // added
#include <QMenuBar> // added
#include <QMenu> // added
#include <QAction> // added
#include "html5applicationviewer.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QMainWindow w; // important, viewer is in stack so w must be before it!
Html5ApplicationViewer viewer;
w.setCentralWidget(&viewer); // set viewer as the central widget
QMenu *fileMenu = w.menuBar()->addMenu("&File"); // create file menu
QAction *exitAction = fileMenu->addAction("&Exit"); // create exit action
QObject::connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit())); // make the action do something
viewer.setOrientation(Html5ApplicationViewer::ScreenOrientationAuto);
//viewer.showExpanded(); // will be shown by main window
viewer.loadFile(QLatin1String("html/index.html"));
w.show(); // show main window
return app.exec();
}