panthro
panthro

Reputation: 24099

Signals and Slots - Passing Data

Im trying to connect a signal to a slot and pass through a vector but im not having much luck.

res = QObject::connect(storePayments, SIGNAL(existingPurchasesResponseSuccess(std::vector<QString>)), this, SLOT(RefreshPurchasesSuccess(std::vector<QString>)));

Slot:

void RefreshPurchasesSuccess(std::vector<QString>);

void Store::RefreshPurchasesSuccess(std::vector<QString> previousPurchasesArray)
{
 //do something
}

Signal:

void existingPurchasesResponseSuccess(std::vector<QString>);


vector<QString> previousPurchasesArray;
emit existingPurchasesResponseSuccess(previousPurchasesArray);

It says the signal/slot is not defined, but when I take out the vector it works, so it must be something wrong with that. Am I defining it wrong?

Thanks

Upvotes: 1

Views: 5950

Answers (1)

mip
mip

Reputation: 8733

If you use custom structure like std::vector<QString> you must declare and register metatype

 Q_DECLARE_METATYPE(std::vector<QString>)

"Ideally, this macro should be placed below the declaration of the class or struct. If that is not possible, it can be put in a private header file which has to be included every time that type is used in a QVariant." -- Qt documentation on Q_DECLARE_METATYPE

For queued connections you may need qRegisterMetatype

 qRegisterMetaType<std::vector<QString> >();

qRegisterMetaType can be called for example in main() even before QApplication::exec().

Also remember that you must use Q_OBJECT macro if your class declares any signals or slots.

"The Q_OBJECT macro must appear in the private section of a class definition that declares its own signals and slots or that uses other services provided by Qt's meta-object system."

If you have no reason to use std::vector<QString> then it would be much simpler to use QStringList, which is already known to Qt's meta-object system, provides many convenient methods to manipulate its content and as a standard Qt type will fit to non-yours slot definitions.

Upvotes: 9

Related Questions