PDA

View Full Version : User defined property & compound properties



pan
24th November 2010, 15:19
I just need some direction on this one.

I want to create MyClass with some Q_PROPERTY fields then declare MyClass as a metatype so I can use it in QVarient and set it as a Q_PROPERTY of my main widget. Then MyClass properties will get added as compound properties (like geometry or font on QWidget).

I run a Q_DECLARE_METATYPE() at the end of MyClass and qRegisterMetaType<MyClass>(); in the constructor of the widget. Declaring:
Q_PROPERTY(MyClass myClass READ myClass WRITE setMyClass) in the header.

It runs the setMyClass method but doesn't display anything...

franz
24th November 2010, 20:37
Maybe a little compiling example code?

pan
25th November 2010, 07:54
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>

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

Cell.cpp
#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;
}


myclass.h
#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


myclass.cpp
#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;
}


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:

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);

And got the result:

3 | objectName : | one : | two : 0
So the information is there. It just does not show up.

pan
26th November 2010, 08:12
Just an update for anyone interested in the future...

It is out of the scope of the Qt designer for me to display the information that way.

Ashutosh2k1
18th May 2011, 09:50
Hi
have u achieved what r u talking about like properties on CustomWidget

wysota
18th May 2011, 10:29
There is no problems with properties on custom widgets. The problem is the OP wanted to violate the rule that QObjects are not to be copied and he wanted Designer to somehow automatically detect and handle that situation for him. It is possible but it requires much more work as you need to implement the QDesignerCustomPropertySheetExtension interface through a plugin.

Ashutosh2k1
2nd June 2011, 06:09
hi
Can u Show some Example regarding this .I have to implement this for my custom Widget

Ashutosh2k1
23rd June 2011, 12:16
Hi I have used the QDesignerPropertySheet Extension now i am creating the This as the base class and the CustomProperty Class as memeber of this class but the Property are not Coming Where i am missing

wysota
23rd June 2011, 13:29
You are probably missing in your code.

Ashutosh2k1
23rd June 2011, 13:58
Hi
Suppose i have created the QDesignerProperty Extension through the procedure .Now my CustomProperty class is in CustomClass and put the pointer in the Extension Class .But the Custom Property is not Coming am i missing something

Added after 17 minutes:

Hi ,
need help regarding QDesinerPropertySheetExtension .I have serached the google but found nothing regarding Custom Property and QDesinerPropertySheetExtension

pan
23rd June 2011, 15:13
What I originally wanted was a way to display properties like they do with geometry. The approach I took was obviously wrong. But support at Nokia told me it was not possible to create it the way I wanted (unless I rewrite some source I guess).

There is a help page for QDesignerPropertySheetExtension...

It is a bit hard to understand what you want. If you just want a property on your custom designer widget? or you want to create your own QVariant? Do you really want to mess with the properties or do you just want to add some custom ones?

Check out Q_PROPERTY if you just want to add some extras to the existing property sheet.

Ashutosh2k1
24th June 2011, 05:37
No Actually I want the Property Like Geometry where you can expand the fifferent memeber .For Just Showing the Property i can use Q_PROPERTY and it gives result.
But Suppose i have defined a struct Prop whose memeber are firstand seecond so i want the the struct memeber to be appear as the property of Custom Widget and i can modify the member of struct first and second
So my problem is this

pan
24th June 2011, 09:22
wysota says it is possible... I tried working with QDesignerPropertySheetExtension but didn't get it to work. If you figure out a solution it would be interesting to see what you did. Good luck.

But after talking with the Qt people at Nokia last year I was left with this conclusion: it is possible to have compound properties but it is not possible to show them in the designer's editor in a compound format.