I write example, all works fine:
QT -= gui
TARGET = QSharedPointerQueue
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
HEADERS += main.cpp
SOURCES += main.cpp
QT -= gui
TARGET = QSharedPointerQueue
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
HEADERS += main.cpp
SOURCES += main.cpp
To copy to clipboard, switch view to plain text mode
#include <QtCore/QCoreApplication>
#include <QtCore/QMetaType>
#include <QtCore/QSharedPointer>
#include <QtCore/QTextStream>
struct MyStruct
{
};
Q_DECLARE_METATYPE(QSharedPointer<MyStruct>)
static void myStructDeleter(MyStruct* ms)
{
<< "Do delete MyStruct with message: "
<< ms->message
<< endl;
delete ms;
}
{
Q_OBJECT
public:
virtual ~MyObject() {}
void sendMessage(const QString& message)
{
MyStruct* ms = new MyStruct;
ms->message = message;
emit sendPointer(QSharedPointer<MyStruct>(ms, myStructDeleter));
}
signals:
void sendPointer(QSharedPointer<MyStruct>);
};
{
Q_OBJECT
public:
virtual ~MyObjectTwo() {}
public slots:
void pointerReceiver(QSharedPointer<MyStruct> ptr)
{
<< ptr->message
<< "(" << Q_FUNC_INFO << ")"
<< endl;
}
};
int main(int argc, char* argv[])
{
qRegisterMetaType<QSharedPointer<MyStruct> >();
MyObject mobj;
MyObjectTwo mobj2;
QObject::connect(&mobj,
SIGNAL(sendPointer
(QSharedPointer<MyStruct>
)),
&mobj2, SLOT(pointerReceiver(QSharedPointer<MyStruct>)),
Qt::QueuedConnection);
mobj.sendMessage("Goodbye, cruel world!");
return a.exec();
}
#include "moc_main.cpp"
#include <QtCore/QCoreApplication>
#include <QtCore/QMetaType>
#include <QtCore/QSharedPointer>
#include <QtCore/QTextStream>
struct MyStruct
{
QString message;
};
Q_DECLARE_METATYPE(QSharedPointer<MyStruct>)
static void myStructDeleter(MyStruct* ms)
{
QTextStream(stdout)
<< "Do delete MyStruct with message: "
<< ms->message
<< endl;
delete ms;
}
class MyObject : public QObject
{
Q_OBJECT
public:
explicit MyObject(QObject* parent = 0) : QObject(parent) {}
virtual ~MyObject() {}
void sendMessage(const QString& message)
{
MyStruct* ms = new MyStruct;
ms->message = message;
emit sendPointer(QSharedPointer<MyStruct>(ms, myStructDeleter));
}
signals:
void sendPointer(QSharedPointer<MyStruct>);
};
class MyObjectTwo : public QObject
{
Q_OBJECT
public:
explicit MyObjectTwo(QObject* parent = 0) : QObject(parent) {}
virtual ~MyObjectTwo() {}
public slots:
void pointerReceiver(QSharedPointer<MyStruct> ptr)
{
QTextStream(stdout)
<< ptr->message
<< "(" << Q_FUNC_INFO << ")"
<< endl;
QCoreApplication::exit(0);
}
};
int main(int argc, char* argv[])
{
QCoreApplication a(argc, argv);
qRegisterMetaType<QSharedPointer<MyStruct> >();
MyObject mobj;
MyObjectTwo mobj2;
QObject::connect(&mobj, SIGNAL(sendPointer(QSharedPointer<MyStruct>)),
&mobj2, SLOT(pointerReceiver(QSharedPointer<MyStruct>)),
Qt::QueuedConnection);
mobj.sendMessage("Goodbye, cruel world!");
return a.exec();
}
#include "moc_main.cpp"
To copy to clipboard, switch view to plain text mode
P.S.: Without Q_DECLARE_METATYPE compilator issue errors, so just try recompile project remove moc_* files first.
Bookmarks