Search code examples
c++qtpropertygrid

How to use this Qproperty grid library?


I was looking for an equivalent of .NET's PropertyGrid and after a lot of search I found this one that actually compiled. But I can't figure out how this works for my own object. There some examples in the sample project but this didn't really help me how I use this with my custom object.

I tried created a class that inheried from QObject and tested both using properties and fields but neither worked. What I get is my class name empty, without any values listed.

Here's my code:

class Foo : public QObject
{
    Q_OBJECT

public:
    explicit Foo(QObject *parent = 0);
    ~Foo();

    int val;
};

I also tried definitng Foo class like this (this version crashs):

class Foo : public QObject
{
    Q_OBJECT

public:
    explicit Foo(QObject *parent = 0);
    ~Foo();

    Q_PROPERTY(int a READ getA WRITE setA)

    int a;

    int getA() const;

    void setA(int a);

};

And the code that create the property grid:

Property

Model *model = new PropertyModel();
    Foo *f = new Foo();
    PropertyItemFromQObject *conv=new PropertyItemFromQObject(&PropertyItemDefaultFactory::instance());
    PropertyItem *it=conv->importFrom(f,true,0);
    model->add( new PropertyItemColor("une couleur",QColor(0,255,0)));
    model->add( new PropertyItemColor("test color",QColor(255,0,0)));
    model->add(it);

    tree.setModel(model);
    tree.setItemDelegate(new PropertyDelegate());
    tree.show();

    delete f;

Solution

  • Better look for the Qt Property Browser. It's a Qt solution and BSD licensed. You can find multiple examples in the subfolder as well as by searching the net. This property browser is also used by Qt designer for instance (it just adds coloring on top of it).