Here are the two classes I have. They are instantiated from a myclassplugin class as part of a Qt custom designer widget. They are very basic. "test" shows up in the properties list but "Cell" doesn't. I guess I need to do something extra to the cell to get the value data out?
Cell.h
#ifndef CELL_H
#define CELL_H
#include <QObject>
#include <QMetaType>
{
Q_OBJECT
Q_PROPERTY( QString one READ one WRITE setOne
) Q_PROPERTY( int two READ two WRITE setTwo )
public:
explicit Cell
(QObject *parent
= 0);
Cell(const Cell& cell);
~Cell(){}
Cell& operator=( const Cell& cell );
void setOne( const QString& one );
int two() const;
void setTwo( const int value );
private:
int m_two;
};
Q_DECLARE_METATYPE(Cell);
#endif // CELL_H
#ifndef CELL_H
#define CELL_H
#include <QObject>
#include <QMetaType>
class Cell : public QObject
{
Q_OBJECT
Q_PROPERTY( QString one READ one WRITE setOne )
Q_PROPERTY( int two READ two WRITE setTwo )
public:
explicit Cell(QObject *parent = 0);
Cell(const Cell& cell);
~Cell(){}
Cell& operator=( const Cell& cell );
QString one() const;
void setOne( const QString& one );
int two() const;
void setTwo( const int value );
private:
QString m_one;
int m_two;
};
Q_DECLARE_METATYPE(Cell);
#endif // CELL_H
To copy to clipboard, switch view to plain text mode
Cell.cpp
#include "Cell.h"
, m_one( "" )
, m_two( 0 )
{
}
Cell::Cell(const Cell &cell)
: m_one( cell.m_one)
, m_two( cell.m_two )
{
}
{
return m_one;
}
void Cell
::setOne(const QString &one
) {
m_one = one;
}
int Cell::two() const
{
return m_two;
}
void Cell::setTwo(const int value)
{
m_two = value;
}
Cell& Cell::operator =( const Cell& cell )
{
m_one = cell.m_one;
m_two = cell.m_two;
return *this;
}
#include "Cell.h"
Cell::Cell(QObject *parent) :
QObject(parent)
, m_one( "" )
, m_two( 0 )
{
}
Cell::Cell(const Cell &cell)
: m_one( cell.m_one)
, m_two( cell.m_two )
{
}
QString Cell::one() const
{
return m_one;
}
void Cell::setOne(const QString &one)
{
m_one = one;
}
int Cell::two() const
{
return m_two;
}
void Cell::setTwo(const int value)
{
m_two = value;
}
Cell& Cell::operator =( const Cell& cell )
{
m_one = cell.m_one;
m_two = cell.m_two;
return *this;
}
To copy to clipboard, switch view to plain text mode
myclass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QtGui/QWidget>
#include "Cell.h"
{
Q_OBJECT
Q_PROPERTY(Cell cell READ cell WRITE setCell)
Q_PROPERTY(QString test READ test WRITE setTest
)
public:
~MyClass(){}
Cell cell();
void setCell(const Cell& cell);
void setTest(const QString& test);
private:
Cell m_Cell;
};
#endif
#ifndef MYCLASS_H
#define MYCLASS_H
#include <QtGui/QWidget>
#include "Cell.h"
class MyClass : public QWidget
{
Q_OBJECT
Q_PROPERTY(Cell cell READ cell WRITE setCell)
Q_PROPERTY(QString test READ test WRITE setTest)
public:
MyClass(QWidget *parent = 0);
~MyClass(){}
Cell cell();
void setCell(const Cell& cell);
QString test();
void setTest(const QString& test);
private:
Cell m_Cell;
QString m_test;
};
#endif
To copy to clipboard, switch view to plain text mode
myclass.cpp
#include "myclass.h"
MyClass
::MyClass(QWidget *parent
) :{
qRegisterMetaType<Cell>();
}
Cell
MyClass::cell()
{
return m_Cell;
}
void
MyClass::setCell(const Cell &cell)
{
m_Cell = cell;
}
MyClass::test()
{
return m_test;
}
void
MyClass
::setTest(const QString &test
){
m_test = test;
}
#include "myclass.h"
MyClass::MyClass(QWidget *parent) :
QWidget(parent)
{
qRegisterMetaType<Cell>();
}
Cell
MyClass::cell()
{
return m_Cell;
}
void
MyClass::setCell(const Cell &cell)
{
m_Cell = cell;
}
QString
MyClass::test()
{
return m_test;
}
void
MyClass::setTest(const QString &test)
{
m_test = test;
}
To copy to clipboard, switch view to plain text mode
I thought I could use the Cell's metaobject to get the property fields of Cell, but no success there yet.
ps. I can't seem to get attachment to work right now. I will try later from another computer if it's helpful?
Added after 15 minutes:
I added the following to the MyClass::cell() method:
int count = metaobject->propertyCount();
for(int i = 0; i < count; i++)
{
const char *name = metaproperty.name();
this->setProperty(name, value);
tmp.
append(QString("| %1 : %2 ").
arg(name
).
arg(value.
toString()));
}
setTest(tmp);
const QMetaObject *metaobject = m_Cell.metaObject();
int count = metaobject->propertyCount();
QString tmp(QString("%1 ").arg(count));
for(int i = 0; i < count; i++)
{
QMetaProperty metaproperty = metaobject->property(i);
const char *name = metaproperty.name();
QVariant value = m_Cell.property(name);
this->setProperty(name, value);
tmp.append(QString("| %1 : %2 ").arg(name).arg(value.toString()));
}
setTest(tmp);
To copy to clipboard, switch view to plain text mode
And got the result:
3 | objectName : | one : | two : 0
So the information is there. It just does not show up.
Bookmarks