qRegisterMetaType usage - qt

#include<QMetaType>
typedef QList<int> IntList;
qRegisterMetaType<IntList>("IntList");
error C2909: 'qRegisterMetaType': explicit instantiation of function template requires return type
C2909 says I need to define
template int qRegisterMetaType<IntList>("IntList");
If I define like I mentioned above then I get the below error
error C2059: syntax error : 'string'
warning C4667: 'int qRegisterMetaType(void)' : no function template defined that matches forced instantiation
why do I get this error ?

"qRegisterMetaType" is a function. It must appear in a code block.

int metatype_id = qRegisterMetaType<IntList>("IntList");

You need to add Q_DECLARE_METATYPE(IntList) before you can register it.

Related

How to retrieve QPair from QVariant?

I'm doing auto data = combobox->currentData().value<QPair>(); but the compiler complains with:
[ 48%] Building CXX object src/CMakeFiles/mudlet.dir/dlgProfilePreferences.cpp.o
/home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp: In lambda function:
/home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp:420:81: error: no matching function for call to ‘QVariant::value()’
auto data = script_preview_combobox->currentData().value<QPair>();
^
In file included from /home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/QVariant:1:0,
from /home/vadi/Programs/Mudlet/mudlet/cmake-build-debug/src/ui_profile_preferences.h:12,
from /home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.h:27,
from /home/vadi/Programs/Mudlet/mudlet/src/dlgProfilePreferences.cpp:25:
/home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/qvariant.h:351:14: note: candidate: template<class T> T QVariant::value() const
inline T value() const
^
/home/vadi/Programs/Qt/5.9/gcc_64/include/QtCore/qvariant.h:351:14: note: template argument deduction/substitution failed:
src/CMakeFiles/mudlet.dir/build.make:806: recipe for target 'src/CMakeFiles/mudlet.dir/dlgProfilePreferences.cpp.o' failed
As far as I see, my call is lining up with template<class T> T QVariant::value() - what's wrong?
QPair is a template class and your code for getting the value from the variant does not fully describe the type.
First you need to know what two types your QPair describes. Then you must use the following code to extract it (changing the QString and int to your pairs data types):
auto pair = combobox->currentData().value<QPair<QString, int> >();

cannot convert 'QScopedPointer<T>' to 'QStandardItem *'

I use this code without any error
QStandardItem *newRow;
newRow = new QStandardItem(hostname);
model2->setItem(index, 2, newRow);
I want to change the above code to the below:
QScopedPointer<QStandardItem> newRow(new QStandardItem);
model2->setItem(index, 2, newRow);
But I get this error:
C:\...\mainwindow.cpp:352: error: C2664: 'void QStandardItemModel::setItem(int,int,QStandardItem *)' : cannot convert parameter 3 from 'QScopedPointer<T>' to 'QStandardItem *'
with
[
T=QStandardItem
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
How can I solve the problem?
Try this, use take() method to get the pointer.
On my computer
QStandardItem *item2 = new QStandardItem("foo");
model->setItem(4,0,item2);//works
QScopedPointer<QStandardItem> newRow(new QStandardItem("foo"));
model->setItem(4,0,newRow.take());//works too
Instead of QScopedPointer<T>::take() which releases the stored pointer of the scoped pointer container i suggest to use QScopedPointer<T>::data() which returns a the pointer but does not reset the scoped pointer
But on the other hand, why would you like to use QScopedPointer to store a pointer to the QStandardItem when the model will take ownership of it and will handle its lifetime?

Unexpected reply signature: got "oa{sv}", expected "(oa{sv})"

Using C++/QtDBus.
I'm trying to get a reply from DBus call to function described as:
object, dict PullAll(string targetfile, dict filters).
I registered (qDBusRegisterMetaType) a type defined as: typedef QPair< QDBusObjectPath, QVariantMap > Transfer;
In QDBusPendingCallWatcher handler I'm doing:
QDBusPendingReply<Transfer> reply = *pwatcher;
I get an error:
Unexpected reply signature: got "oa{sv}", expected "(oa{sv})"
What's wrong? What is parentheses in "(oa{sv})"?
I think the whole message needs to be wrapped in a struct. At least you have the proper signature otherwise and are getting a response.
arrays: []
dict entries: {}
structs: ()
I'm not that familiar with QtDbus, but looking at the page for the QDbusArgument Class, you might have to do something like this:
argument.beginStructure();
argument << mystruct.objectpath << mystruct.array;
argument.endStructure();

How do I correct a "const mismatch in out variable"?

So I'm currently writing code to access a player's uniqueNetId using:
Class'GameEngine'.static.GetOnlineSubsystem().UniqueNetIdToString(
OnlineSubsystemSteamworks(Class'GameEngine'.static.GetOnlineSubsystem()).LoggedInPlayerId.Uid);
But that leads to this:
Error, Call to 'UniqueNetIdToString', parameter 1: Const mismatch in Out variable
Does anybody have any idea what I'm doing wrong?
It's not actually a const mismatch. The function is expecting a struct and you are passing in a member of the struct instead. Try removing the .Uid, i.e.:
Class'GameEngine'.static.GetOnlineSubsystem().UniqueNetIdToString(
OnlineSubsystemSteamworks(Class'GameEngine'.static.GetOnlineSubsystem()).LoggedInPlayerId);

2 arguments in push_back

I am trying to put 2 arguments inside a vector using push_back but its giving me an error since the function is allowed to take only one argument. How can I pass 2 arguments??
Vertex Class:
template <class VertexType, class EdgeType> class Vertex{
public:
std::vector<std::pair<int, EdgeType>> VertexList;
};
Outside Vertex Class inside Main():
project3::Vertex<string, string> v1("v1");
v1.VertexList.push_back(1,"e1");
Error is :
error C2661: 'std::vector<_Ty>::push_back' : no overloaded function takes 2 arguments
IntelliSense: too many arguments in function call
You need to do
v1.VertexList.push_back(std::pair<int, EdgeType>(1,"e1"));
Try push_back(make_pair(1, string("e1")));

Resources