Unsolved tr() with binding: how to get the calling classname to a function,
-
@Christian-Ehrlicher ahhh, ok. That makes sense.
So the best approach would be to put it in a macro. -
Hi,
Out of curiosity, why do you want to translate a class name ?
-
I don’t want to translate the classname.
I want to connect a translation to a slot.
Normally I have situations like:
object.setName(tr(„super english text“))Problem is that in this situations a retranslate wouldn’t trigger a new „setName“.
So my idea is to have a function
trExt("super english text", &object, &Class::setName);That way it would call setName and create a connection to a languageChanged event which resets the parameter to the new translation every time
-
@SGaist said in tr() with binding: how to get the calling classname to a function,:
why do you want to translate a class name ?
No, he wants not translate the class name but mimic tr() which in fact calls QCoreApplication::translate() where the context is the class name.
-
one additional question:
how do I invoke a methode without the name? Or how do I get the nameI have this:
#define N_TRANSLATE\ template <typename Func>\ void trExt(const QString &text, typename QtPrivate::FunctionPointer<Func>::Object *receiver, Func slot) {\ QMetaObject::invokeMethod(receiver, "HERE I NEED A NAME", Q_ARG(QString, text));\
To use "invokeMethod" I need the slotname.
I would like to pass the slot as shown. Can I get the name somehow? Or is there another way of invoking this slot? -
@themts said in tr() with binding: how to get the calling classname to a function,:
how do I invoke a methode without the name?
I don't understand what you mean - when you want to call a function you should know it's name, or?
-
That is my function:
template <typename Func> void trExt(const char *text, typename QtPrivate::FunctionPointer<Func>::Object *receiver, Func slot) { const char* className = metaObject()->className(); //A) CALL "Func slot" HERE AND PASS tr(text) as parameter to "Func slot" connect(&_langLoader(), &NLanguageLoader::languageChanged, [className, text, slot]() { QString newText = QCoreApplication::translate(className, text); //B) INVOKE METHOD "Func slot" HERE AND PASS newText as parameter to "Func slot" like QMetaObject::invokeMethod to be sure it is threadsafe }); }
I' missing two things now:
A) how do i call the slot (parameter 3) of the receiver? I would have exptected to use only slot(tr(text)) but compiler tells me "there is no function that takes 1 argument". Just from c++ side I would have expected that it works.B) as the thread-context might be different I would like to use the QMetaObject::invokeMethod function as it will send it to the event loop if the target is in another context.
-
I just checked to code of QTimer::singleShot(), which does exactly what I need.
I just have to pass 0ms.EDIT: not completely. A singleShot does not accept arguments. It's using QMetaObject::InvokeMethodImpl() which does not take arguments...
-
ok, I will give up...
I will do it like this.
Instead of callingtrExt("super english text", &object, &Class::setName);"
I will call
trExt("super english text", &object, "setName");
I would have prefered the first way but I'm not that deep in the Qt internals.
void trExt(const char *text, QObject *receiver, const char *member) { QMetaObject::invokeMethod(receiver, member, Q_ARG(QString, tr(text))); const char* className = metaObject()->className(); connect(&_langLoader(), &NLanguageLoader::languageChanged, [className, text, receiver, member]() { QString newText = QCoreApplication::translate(className, text); QMetaObject::invokeMethod(receiver, member, Q_ARG(QString, newText)); }); }
-
What about implementing the changeEvent method to handle your object's translatable strings like shown in the dynamic translation part of the internationalization documentation ?