QT QML Translation retranslate - c++

I'm running QT 5.10.1 on Windows, the app is only made in QML. I'm trying to use the new retranslate() to change language during runtime. The current code is working fine with texts that use the getEmptyString() appended to it. But the rest of the text within qsTr() does not. TranslationHandler.cpp is empty and I haven't cleaned up the includes.
So I'm able to set a language from the QML using the context property. Also I checked so the engine is the same instance. Any ideas why the retranslate function is not working?
Thanks for the help!
main.cpp :
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QTranslator>
#include <QtGui>
#include <QQmlContext>
#include <QDebug>
#include <QQmlEngine>
#include "translationhandler.h"
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
TranslationHandler transHndl(&engine);
engine.rootContext()->setContextProperty("translateHandler", &transHndl);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;
return app.exec();
}
TranslationHandler.h :
#ifndef TRANSLATIONHANDLER_H
#define TRANSLATIONHANDLER_H
#include <QTranslator>
#include <QString>
#include <QGuiApplication>
#include <QObject>
#include <QDebug>
#include <QQmlEngine>
class TranslationHandler : public QObject
{
Q_OBJECT
Q_PROPERTY(QString emptyString READ getEmptyString NOTIFY languageChanged)
public:
explicit TranslationHandler(QQmlEngine *engine)
{
m_translator1 = new QTranslator(this);
m_currentLanguage = "en";
m_engine = engine;
}
QString getEmptyString()
{
return "";
}
Q_INVOKABLE QString getCurrentLanguage()
{
return m_currentLanguage;
}
Q_INVOKABLE void selectLanguage(QString language)
{
if(language == QString("jp"))
{
m_currentLanguage = language;
m_translator1->load(":/translation/qml_jp.qm");
qGuiApp->installTranslator(m_translator1);
m_engine->retranslate();
}
if(language == QString("en"))
{
m_currentLanguage = language;
qGuiApp->removeTranslator(m_translator1);
m_engine->retranslate();
}
emit languageChanged();
}
signals:
void languageChanged();
private:
QTranslator *m_translator1;
QString m_currentLanguage;
QQmlEngine *m_engine;
public slots:
};
#endif // TRANSLATIONHANDLER_H

This was confirmed as a bug and will be fixed in version 5.12. If you want to compile it yourself please check the bug report
https://bugreports.qt.io/browse/QTBUG-68350

Related

Dynamic c++ object in QJSEngine

I can not figure out how to organize the dynamic creation of the c++ objects in QJSEngine
I try to do so :
main.cpp
#include <QCoreApplication>
#include <QQmlEngine>
#include <QObject>
#include <QQmlComponent>
#include <QtQml>
#include <QQmlEngine>
#include "js_object.h"
Q_DECLARE_METATYPE(Js_Object*)
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QJSEngine myEngine;
qmlRegisterType<Js_Object>("Js_Object", 1, 0, "Js_Object");
QJSValue v=myEngine.evaluate("import Js_Object 1.0;\n var t= new Js_Object() ;");
qDebug()<<v.toString();
return a.exec();
}
JsObject.h
#ifndef JS_OBJECT_H
#define JS_OBJECT_H
#include <QObject>
class Js_Object : public QObject
{
Q_OBJECT
public:
explicit Js_Object(QObject *parent = 0) : QObject(parent) {
qDebug()<<"Js_Object::Js_Object(QObject *parent) : QObject(parent)";
}
};
#endif // JS_OBJECT_H
this example compile, bun console print :
"SyntaxError: Syntax error"
What I do wrong ?
QScriptEngine allow to do this, but in QT5.5 QScriptEngine is deprecated,
try to work with QJSEngine

How to get your personalized library widget in QT design window?

I am a newbie to QT.
My goal is to access my personalized widget(created in my DLL) from the Design window of application in QT.
I was able to create the DLL successfully and call it from my application.
Kindly guide me as to how to access the DLL widget which I created in the design window along with other library QML types.
Thanks in advance,
Sam
Please find my files below :
myCppLib.h
#ifndef MYCPPLIB_H
#define MYCPPLIB_H
#include "mycpplib_global.h"
#include <QDebug>
#include <QWidget>
class MYCPPLIBSHARED_EXPORT MyCppLib : public QWidget
{
private:
Q_OBJECT
public:
MyCppLib();
void Test();
//void resize(int, int);
//void show();
};
#endif // MYCPPLIB_H
Application
main.cpp
#include <QGuiApplication>
#include <QApplication>
#include <QQmlApplicationEngine>
#include <mycpplib.h>
int main(int argc, char *argv[])
{
//QGuiApplication app(argc, argv);
QApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
MyCppLib testLib;
//testLib.resize(200,200);
testLib.setProperty("height", 250);
testLib.setProperty("width", 250);
testLib.Test();
testLib.show();
return app.exec();
}

QT connect crash using signal accepted

I have a problem with my Qt code. I wanted to write an program which are taking a coordinates of point and then that point is drawing in point with that coordinates. My program doesn't have any errors or warnings when i built it but it's crashing at start(MainWindow doesn't show). This is my code:
main.cpp
#include < QApplication >
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow win;
win.show();
return app.exec();
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include <QAction>
#include <QToolBar>
#include <QTextCodec>
#include <QObject>
#include <QDialog>
#include <QLineEdit>
#include <QPushButton>
#include <QString>
#include "addpoint.h"
class MainWindow: public QMainWindow
{
Q_OBJECT
private:
QToolBar *AddToolbar;
QAction *AddPointAction;
AddPoint *AddPointDialog;
QLineEdit *x;
public:
MainWindow();
void createToolbar();
void createActionAdd();
signals:
public slots:
void PointClicked();
void DialogAccepted();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow()
{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
createActionAdd();
createToolbar();
connect(AddPointAction, SIGNAL(triggered(bool)), this, SLOT(PointClicked()));
connect(AddPointDialog, SIGNAL(accepted()), this, SLOT(DialogAccepted()));
setMinimumSize(480, 320);
}
/**/
void MainWindow::createToolbar()
{
AddToolbar = new QToolBar;
AddToolbar->addAction(AddPointAction);
addToolBar(AddToolbar);
}
/**/
void MainWindow::createActionAdd()
{
AddPointAction = new QAction("Add Road", this);
x = new QLineEdit(this);
x->setFixedSize(100, 30);
x->move(100, 100);
}
/**/
void MainWindow::PointClicked()
{
AddPointDialog = new AddPoint(this);
AddPointDialog->setModal(true);
AddPointDialog->exec();
}
/**/
void MainWindow::DialogAccepted()
{
x->setText("abc");
}
addpoint.h
#ifndef ADDPOINT_H
#define ADDPOINT_H
#include <QWidget>
#include <QTextCodec>
#include <QDialog>
#include <QObject>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QString>
class AddPoint : public QDialog
{
Q_OBJECT
private:
QLabel *XpointL;
QLineEdit *XPoint;
QPushButton *OKButton;
public:
AddPoint(QWidget *parent);
void createButton();
signals:
public slots:
};
#endif // ADDPOINT_H
addpoint.cpp
#include "addpoint.h"
AddPoint::AddPoint(QWidget *parent) :QDialog(parent)
{
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
createButton();
connect(OKButton, SIGNAL(clicked(bool)), this, SLOT(accept()));
setMinimumSize(320, 240);
}
/**/
void AddPoint::createButton()
{
XpointL = new QLabel("Point X:", this);
XpointL->setFixedSize(100, 30);
XpointL->move(10, 10);
XPoint = new QLineEdit(this);
XPoint->setFixedSize(180, 30);
XPoint->move(120, 10);
OKButton = new QPushButton("OK", this);
OKButton->setFixedSize(100, 30);
OKButton->move(200, 150);
}
After running the program i see in aplication output lap:
"The program has unexpectedly finished."
"C:\QT\build-xxx-Desktop_Qt_5_4_2_MSVC2013_64bit-Debug\debug\xx.exe crashed"
I note that i made some experiments with this code and i saw that i have problem with signal accpeted() at mainwindow.cpp. I don't know what can i do with this problem. I hope you will help me.
AddPointDialog is uninitialized pointer, it does not yet point to valid AddPoint in MainWindow's constructor. You cannot call connect on that. Its value will change later, when you do AddPointDialog = new AddPoint(this); and only then will you be able to call connect.
Simply, you should move your connect call to void MainWindow::PointClicked() after you've initialized your pointer. I'd also make AddPointDialog local to that function and store it on the stack (you don't use it anywhere else and you're leaking memory).
The code would be:
void MainWindow::PointClicked()
{
AddPoint AddPointDialog(this);
AddPointDialog.setModal(true);
connect(&AddPointDialog, SIGNAL(accepted()), this, SLOT(DialogAccepted()));
AddPointDialog.exec();
}

Get QTextEdit changes when textChanged() signal is emited

I have a QTextEdit and I connected the textChanged() slot to a signal. How can I find the changes when the signal is emitted. For example, I want to save the cursor position and the character written when I write something.
In the slot that gets called when the signal is emitted you can get the text with QString str = textEdit->toplainText();. Also you can store the previous version of the string and compare to get the character that was added and its position.
Regarding the cursor position you can us QTextCurosr class as in this example:
widget.h file:
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
#include <QTextCursor>
#include <QVBoxLayout>
#include <QLabel>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private slots:
void onTextChanged();
void onCursorPositionChanged();
private:
QTextCursor m_cursor;
QVBoxLayout m_layout;
QTextEdit m_textEdit;
QLabel m_label;
};
#endif // WIDGET_H
widget.cpp file:
#include "widget.h"
#include <QString>
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
connect(&m_textEdit, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
connect(&m_textEdit, SIGNAL(cursorPositionChanged()), this, SLOT(onCursorPositionChanged()));
m_layout.addWidget(&m_textEdit);
m_layout.addWidget(&m_label);
setLayout(&m_layout);
}
Widget::~Widget()
{
}
void Widget::onTextChanged()
{
// Code that executes on text change here
}
void Widget::onCursorPositionChanged()
{
// Code that executes on cursor change here
m_cursor = m_textEdit.textCursor();
m_label.setText(QString("Position: %1").arg(m_cursor.positionInBlock()));
}
main.cpp file:
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}

Open external links from flash player hosted on QWebKit

i have flash player running on QWebkit , and on the flash player there are some web links
that needs to be open in external browser , what i did is :
m_webView->page()->setLinkDelegationPolicy(QWebPage::LinkDelegationPolicy::DelegateAllLinks);
connect(m_webView->page(),SIGNAL(linkClicked(const QUrl&)),
this,
SLOT(linkClickedHandler(const QUrl&)),Qt::DirectConnection);
void WebBroswerDeleget::linkClickedHandler(const QUrl& url)
{
QDesktopServices::openUrl(QUrl(url.toString(), QUrl::TolerantMode));
}
but its never triggered even of i change in the connect from m_webView->page() to m_webView
i overrided the QWebview::createWindow like this:
QWebView* MyAdWebview::createWindow (QWebPage::WebWindowType type)
{
QWebView* p = new QWebView(0);
connect(p->page()->networkAccessManager(), SIGNAL(finished(QNetworkReply*)), this, SLOT(newWindowLoadFinished(QNetworkReply*)), Qt::UniqueConnection);
return p;
}
void MyAdWebview::newWindowLoadFinished(QNetworkReply* reply) {
QDesktopServices::openUrl(reply->url().toString());
}
QDesktopServices::openUrl is a cutom function which opens the default system browser with this url
This is working for me on both 4.7.2 QtEmbedded and 4.8.1 on mac. What I don't understand is this:
m_webView->page()->setLinkDelegationPolicy(QWebPage::LinkDelegationPolicy::DelegateAllLinks);
Just do:
m_webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
Something like this works for me:
#include <QWebPage>
#include <QWebView>
#include <QApplication>
#include "sigrec.h"
int main(int argc, char** argv)
{
QApplication a(argc, argv);
SigRec rec;
QWebView view;
view.page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
QObject::connect(view.page(), SIGNAL(linkClicked(const QUrl&)), &rec, SLOT(onLinkClicked(const QUrl&)),
Qt::DirectConnection);
view.show();
view.setUrl(QUrl("http://www.google.com"));
return a.exec();
}
Where SigRec is something like this:
#ifndef SIGREC_H
#define SIGREC_H
#include <QObject>
#include <QUrl>
class SigRec : public QObject
{
Q_OBJECT
public:
explicit SigRec(QObject *parent = 0);
public slots:
void onLinkClicked(const QUrl &url);
};
#endif // SIGREC_H