How to invoke QMetaMethod with an array of QStrings? - c++

QMetaObject system provides some basic reflection API. What I want to do is to call given slot or method that's available through this system.
However, the arguments and names of method are initially all QString values. The documentation shows an example:
QString retVal;
QByteArray normalizedSignature = QMetaObject::normalizedSignature("compute(QString, int, double)");
int methodIndex = obj->metaObject()->indexOfMethod(normalizedSignature);
QMetaMethod method = obj->metaObject()->method(methodIndex);
method.invoke(obj,
Qt::DirectConnection,
Q_RETURN_ARG(QString, retVal),
Q_ARG(QString, "sqrt"),
Q_ARG(int, 42),
Q_ARG(double, 9.7));
If the "compute" slot does not take exactly one QString, one int and
one double in the specified order, the call will fail.
Emphasis mine. Now with this example, I'm pretty clueless. It's clearly using templates, which are useless in dynamic context. I don't really see a purpose in using this API statically, when I could just as well call the method directly.
So let's say I have this function:
void invokeSlot(QObject* target, const QString& slotName, const QStringList& arguments) {
// This is how I can get list of overloads by given name
const QMetaObject* obj = target_->metaObject();
QList<QMetaMethod> overloads;
for (int i = 0, l = obj->methodCount(); i < l; ++i) {
const QMetaMethod method = obj->method(i);
if( method.name() == name )
overloads.append(method);
}
// Now how to convert the arguments?
}
You can assume that I have prepared this method for type conversion:
QVariant convertType(const QString& source, const QVariant::Type requiredType);
But how to pass the arguments even if I chose correct overload and convert strings to QVariants of required type?

You can prepare this function:
QGenericArgument convertType(const QString& source, const QVariant::Type requiredType);
By using switches and Q_ARG inside it.
Then a big switch on the argument count with invokes in it.

Related

QVariant conversion doesn't recognize std::string called by my template

I have a function in a class header ("frame.h") that's supposed to convert a QString to a generic type, initializing it to a default value if the QString is empty, shown below.
template <typename T>
static void setStat(T &val, QString &temp)
{
QVariant qv(temp);
if (temp == "")
val = T();
else
val = qv.value<T>();
}
When I call this (which has my only instance of a QVariant) I get the following two errors:
Type is not registered, please use the Q_DECLARE_METATYPE macro to make it
known to Qt's meta-object system (compiling source file item.cpp)
'qt_metatype_id': is not a member of 'QMetaTypeId<T>'
In the file mentioned in the first error ("item.cpp"), I call setStat() once and only once, in the code below, which is in the class constructor.
string temp1 = "";
Frame::setStat(temp1, vec[5]);
desc = temp1;
It's probably worth mentioning that this is in VS2017 with the Qt extension. As I understand it, the error is telling me that std::string is an unrecognized type. Is this normal behavior? How do I fix this?
Even with registration for QVariant, QString doesn't convert freely to QString.
You might add overload instead:
static void setStat(std::string &val, QString &temp)
{
val = temp.toStdString();
}

Static function to convert QList of different types to QString

I have a QList which contains string, int, double and date values. I want to convert the QList to a QString and return it within a QString returning function. Is there a static function I can use or what am I missing here? My current code:
QString Product::toString()
{
QStringList listString;
QString outString;
for(int i =0; i < size(); i++)
{
listString << at(i)->toString();
}
outString = listString.join("\n");
return outString;
}
Notes:
I am assuming Product::at(int)is returning a Transaction, given a previous question.
I am also assuming OP mean "built-in" when he or she wrote"static"
The for can be removed using built-in functions. Some (many?) will find the new syntax less understandable, though.
QString Product::toString()
{
QStringList aggregate;
std::transform(m_transactions.begin(),
m_transactions.end(),
std::back_inserter(aggregate),
std::bind(std::mem_fn(&Transactions::toString), std::placeholders::_1));
// or : [](const Transaction& transaction){ return transaction.toString(); });
return aggregate.join("\n");
}
std::transform will transform every m_transactions elements using Transaction::toString(), and place the results into aggregate.
std::back_inserter means "use QStringList::push_bask to record the results". If QStringList had a resize(int) like QVector does, we could have used aggregate.begin() instead.
The unary function is a bit tricky, as it needs to be converted into a unary function, which what std::bind/std::mem_fn is doing. If you are using C++11, you can use a lambda instead.
Also from the previous question, #SingerOfTheFall's remark is valid:
I also find it a little odd to save transactions inside of products. A
better design would be having a separate class that could store them.
If you keep this design, Transaction Product::at(int) and int Product::size() should be renamed to make the link with Transaction explicit, like getTransaction and getNumberOfTransactions.

Pass class method to const char * parameter

In Qt QShortcut has such constructor
QShortcut(const QKeySequence& key, QWidget *parent,
const char *member = 0, const char *ambiguousMember = 0,
Qt::ShortcutContext context = Qt::WindowShortcut);
As far as I understand from documentation I can pass a method to the third parameter instead of using connect to signal activated. But how can I pass method there? Using
QShortcut* sh = new QShortcut(QKeySequence(Qt::Key_Alt, Qt::Key_1), this, &OnShortcut);
or
QShortcut* sh = new QShortcut(QKeySequence(Qt::Key_Alt, Qt::Key_1), this, &MyClass::OnShortcut);
gives errors
error: no matching function for call to 'QShortcut::QShortcut(QKeySequence, KeyPressProcessing* const, void (*)())'
You have to use SLOT(OnShortcut()) instead of &OnShortcut because the QShortcut class constructor requires const char *as the second and third arguments and not the pointer to a function. Thus, Qt has dedicated conversion macro SLOT that accepts the function signature as an argument and converts it in the form that suitable for establishing connection.

Qt 5: const char * QString cast

In Qt 4 it is possible to automatically cast a QString to a "const char *", e.g. I could pass a QString to a function that expected a "const char *".
void myFunction(const char *parameter);
QString myString;
myFunction(myString); //works in QT4
In Qt 5 I would however get an "error C2440: 'type cast' : cannot convert from 'QString' to 'const char *'" (that is the Visual C++ 2008 compiler, other compilers would throw something similar). If I understand the documentation correctly, it is because the Qt 3 compatibility layer is not included anymore in QT5.
Of course, I could change the function call from
myFunction(myString);
to
myFunction(myString.toLatin1().data());
However, I have a huge code base that compiles fine with Qt 4 and I would really like to have my old code compile with Qt 5 without modifying it. Is there any way to achieve this?
You could create a macro or inline function for your purpose to minimize the changes, but since that would also require a grep alike operation, there is not much difference.
#define QString2charp(myString) myString.toLatin1().data()
or
inline char* QString2charp (const QString &myString)
{
return myString.toLatin1().data();
}
and then:
myFunction(QString2charp(myString));
BUT
of course, in an ideal world, it would be nice if your "myFunction" could get an overload expecting a QString argument.
void myFunction(const char *parameter);
void myFunction(QString parameter);
and then the overload would be implemented like this:
void myFunction(const QString &parameter)
{
myFunction(myString.toLatin1().data());
}
of course, this would require the constructor being explicit so that no implicit conversion can happen, otherwise the compiler will complain about ambiguity in presence of both when trying to pass const char*, but if you always use QString, it should just work with Qt 5.
This is somewhat equal to rewriting the original function to a different signature expecting QString though because unfortunately the corresponding constructor is not explicit. I imagine that was not changed in Qt 5 for compatibility as it would have broken too much code.
As you can see, you do not need to change anything in your code this way, other than adding a one-liner overload. It is neat, isn't it?
You could either
Change the signature of your function to MyFunction( const QString & parameter ) (of course that works only if you don't need the one that takes char * any more)
Overload MyFunction:
void myFunction(const char *parameter); //this is your normal function
void myFunction( const QString & parameter )
{
char * c_str = nullptr;
<convert parameter to char * in any way that suits you>
myFunction( c_str );
}
If the above is impossible for some reason, the best approach is probably to write a similar wrapper:
void MyFunctionWrapper( const QString & parameter )
{
<same as above>
}
It is no longer possible to cast QString to const char* in >= Qt 5, because QString is UTF-16 and no longer has an entry for caching the result of converting to ASCII.
The way to achieve your aim is
a) Add overloads to the functions expecting const char* which can handle QStrings,
b) Add the explicit conversion code to the call-site, or
c) Go to some char-based string, like std::string.
None of those fulfill your aim of nearly no changes though.
I found this on QT forum
const QByteArray byteArray = mytext = textBox.text().toUtf8();
const char *mytext = byteArray.constData();
This resolved my issue

how to implement the macros SIGNAL and SLOT as functions QT

Well, what i want is to implement functions that works like the macros SIGNAL and SLOT but with my own implementation, so the goal is that the function (or my own macro if i can create it) receive a parameter like an String "mycustomsignal" and he return a const char* "2mycustomsignal(double,int)" so i do that using a QMap for store this asociation QMap<"signal name","signal signature">, i fill it in another function.
in this case my QMap is this->signals so i search the signature "mycustomsignal(something)" with the key "mycustomsignal" and prepend the code that QObject::connect recognize for signals and i get "2mycustomsignal(something)" so i convert it in const char* because QObject::connect have this parameters in this format and i want to use also in conjuntion with SIGNAL and SLOT MACROS like this:
QObject::connect(customwidget, customwidget->getSignal("somesignal"),
somewidget, SLOT(someslot()));
the function that i use is (only for undertand what i do):
const char* SomeClass::getSignal(QString signalName) {
QString signalsignature = this->signals.value(signalName);
signalsignature.prepend(QString::number(QSIGNAL_CODE));
QByteArray ba = signalsignature.toLatin1();
return signalformated; //Here is the lost of data because QByteArray only exist in the function
}
but this return a pointer to local and the source of the data is destroyed when the function ends, so how i could do this with a function or creating my own MACRO?
Thanks for any help or suggestion.
You have to return QByteArray from your method, return ba;, then get the const char* from the return value:
QObject::connect(customwidget, customwidget->getSignal("somesignal").constData(),
somewidget, SLOT(someslot()));
If you really want to return char pointer, then you have to keep the QByteArray around, for example by adding it to a QList member variable of the same object, so it will get destructed when the instance gets destructed.