
Originally Posted by
andersin
can this be done at all?
To some extent:
#include <QtDebug>
#include <QCoreApplication>
#include <QObject>
#include <QVariant>
class BInterface
{
public:
virtual int foo() = 0;
};
class B
: public QObject,
public BInterface
{
Q_OBJECT
Q_PROPERTY( int b READ b WRITE setB )
public:
virtual int foo() { return mv_b; }
const int b() const { return mv_b; }
void setB( int b ) { mv_b = b; }
private:
int mv_b;
};
{
Q_OBJECT
Q_PROPERTY( QObject* B READ B WRITE setB
) public:
virtual ~A() { if(mv_B) delete mv_B; }
void setB
( QObject* B
) { if (mv_B
) delete mv_B; mv_B
= B;
}
private:
};
int main( int argc, char **argv )
{
// QObject *a = new A();
// a->setProperty( "B", 0 ); // works
// QObject *a = new A();
// a->setProperty( "B", new B() ); // crashes
if( b1 != 0 ) {
b1->setProperty( "b", 1234 );
}
else {
qDebug() << "b1 is null";
}
if( b2 != 0 ) {
qDebug() << b2->property( "b" );
}
else {
qDebug() << "b2 is null";
}
return 0;
}
#include "main.moc"
#include <QtDebug>
#include <QCoreApplication>
#include <QObject>
#include <QVariant>
class BInterface
{
public:
virtual int foo() = 0;
};
class B : public QObject, public BInterface
{
Q_OBJECT
Q_PROPERTY( int b READ b WRITE setB )
public:
virtual int foo() { return mv_b; }
const int b() const { return mv_b; }
void setB( int b ) { mv_b = b; }
private:
int mv_b;
};
class A : public QObject
{
Q_OBJECT
Q_PROPERTY( QObject* B READ B WRITE setB )
public:
A( QObject *b = 0 ) : mv_B( b ) {}
virtual ~A() { if(mv_B) delete mv_B; }
QObject * B() { return mv_B; }
void setB( QObject* B ) { if (mv_B) delete mv_B; mv_B = B; }
private:
QObject* mv_B;
};
int main( int argc, char **argv )
{
QCoreApplication app( argc, argv );
QObject *a = new A( new B() );
// QObject *a = new A();
// a->setProperty( "B", 0 ); // works
// QObject *a = new A();
// a->setProperty( "B", new B() ); // crashes
QObject* b1 = a->property( "B" ).value< QObject *>();
if( b1 != 0 ) {
b1->setProperty( "b", 1234 );
}
else {
qDebug() << "b1 is null";
}
QObject* b2 = a->property( "B" ).value< QObject *>();
if( b2 != 0 ) {
qDebug() << b2->property( "b" );
}
else {
qDebug() << "b2 is null";
}
return 0;
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Output:
$ ./properties
QVariant(int, 1234)
Bookmarks