0

What should I do to be able to send signals to slots with QVector of my custom class objects as an argument?

struct LicenseInfo
{
    QString company_name;
    QString server_name;
    QString product_name;
    int product_version;
    QString license_end;
    QString last_update;
    QString comment;
};

Usage

connect(_worker, SIGNAL(newLicensesActivated(QVector<LicenseInfo>)),
                 this, SLOT(newLicensesActivated(QVector<LicenseInfo>)));

It is ok to do the following?

#include "licenseinfo.h"

Q_DECLARE_METATYPE(LicenseInfo)

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    qRegisterMetaType<QVector<LicenseInfo>>();

    MainWindow w;
    w.show();

    return a.exec();
}

Should I use both Q_DECLARE_METATYPE macro and qRegisterMetaType function in this case?

FrozenHeart
  • 19,844
  • 33
  • 126
  • 242

1 Answers1

0

The biggest thing I would worry about when trying to pass Qt Containers of objects is to make sure that the objects in the containers don't go out of scope. If the contents are trivial or simple types, this is not necessary.

I prefer to make the items on the heap (using new), and pass around containers of pointers. Then I make sure I clean them up properly at the end of its lifecycle... often using qDeleteAll(). It requires a little more planning up front, but it reduces a lot of bugs later.

Also I like making sure I am using the right container for the job. Like it might make sense to use a QSet instead of a QVector, or even just use a QList instead. But then you may need to implement a comparison operator to make it so that the objects can be compared and sorted.

Another piece of advice, be careful about having >> in your qRegisterMetaType or in any declaration for that matter. Add a space inbetween so it isn't ambiguous. qRegisterMetaType< QVector<LicenseInfo> >(); (note the exaggerated space).

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • "be careful about having >> in your qRegisterMetaType or in any declaration for that matter" -- what's the reason behind it? It's perfectly legal since C++03 or C++11 – FrozenHeart Feb 04 '16 at 16:49
  • "The biggest thing I would worry about when trying to pass Qt Containers of objects is to make sure that the objects in the containers don't go out of scope" -- is it possible? I thought that it's guaranteed that the objects that I passed as an argument to the signal will be alive – FrozenHeart Feb 04 '16 at 16:51
  • Depending on the machine I am using, I don't always have the luxury of an up to date compiler, or a compiler that is fully compliant. I was unaware that it had been fixed in more recent C++ revisions. – phyatt Feb 04 '16 at 16:55
  • http://stackoverflow.com/a/1668276/999943 The copy-on-write idiom I think is the catch that has caused issues for me or for others I was helping debug their code with. – phyatt Feb 04 '16 at 17:01
  • Copy on write has nothing to do with this. Objects that you put in a container are copied and owned by the container. – peppe Feb 04 '16 at 20:14
  • @FrozenHeart: yes, everything is OK. – peppe Feb 04 '16 at 21:24