我怎么知道Qt属性浏览器的值发生了变化?

时间:2015-11-18 02:42:15

标签: c++ qt qt-creator

我正在使用Qt property browser小部件。我正在使用moc自动创建属性网格的所有值,但是我没有找到如何在属性网格中自动设置在属性网格中输入的值?我有什么可以知道的吗?

我像这样创建属性网格:

    QWidget *w = new QWidget();
    Foo *o = new Foo();
    QtBoolPropertyManager *boolManager = new QtBoolPropertyManager(w);
    QtIntPropertyManager *intManager = new QtIntPropertyManager(w);
    QtStringPropertyManager *stringManager = new QtStringPropertyManager(w);
    QtGroupPropertyManager *groupManager = new QtGroupPropertyManager(w);

    const QMetaObject *meta = o->metaObject();
    const QString className = meta->className();
    QMap<QString, PropManager*> dic;
    QtProperty *root = groupManager->addProperty(className);

    for(int i = meta->propertyOffset(); i < meta->propertyCount(); ++i)
    {
        QMetaProperty metaProp = meta->property(i);
        const QString name = metaProp.name();
        QVariant value = metaProp.read(o);
        QVariant::Type t = metaProp.type();
        QtProperty *prop = NULL;
        PropManager *propMan = NULL;

        switch(t)
        {
            case QVariant::Int:
            {
                prop = intManager->addProperty(name);
                root->addSubProperty(prop);
                intManager->setValue(prop, value.toInt());
                propMan = new PropManager(prop, intManager, t);
                break;
            }
            case QVariant::String:
            {
                prop = stringManager->addProperty(name);
                root->addSubProperty(prop);
                stringManager->setValue(prop, value.toString());
                propMan = new PropManager(prop, stringManager, t);
                break;
            }
            default:
                throw std::invalid_argument("unknow type");
        }

        if(prop != NULL)
            dic.insert(name, propMan);
    }

    QtCheckBoxFactory *checkBoxFactory = new QtCheckBoxFactory(w);
    QtSpinBoxFactory *spinBoxFactory = new QtSpinBoxFactory(w);
    QtLineEditFactory *lineEditFactory = new QtLineEditFactory(w);

    QtAbstractPropertyBrowser *editor = new QtTreePropertyBrowser();
    editor->setFactoryForManager(boolManager, checkBoxFactory);
    editor->setFactoryForManager(intManager, spinBoxFactory);
    editor->setFactoryForManager(stringManager, lineEditFactory);
    editor->addProperty(root);

    QGridLayout *layout = new QGridLayout(w);
    layout->addWidget(editor, 1, 0);
    w->show();

Foo.h 的定义如下:

class Foo : public QObject
{
    Q_OBJECT

public:
    explicit Foo() { }
    ~Foo() { }

    Q_PROPERTY(QString name READ getA WRITE setA)
    Q_PROPERTY(int age READ getNum WRITE setNum)

    QString name;
    int age;

    QString getA() const { return name; }
    int getNum() const { return age; }
    void setA(QString a) { this->name = a; }
    void setNum(int n) { this->age = n; }
};

1 个答案:

答案 0 :(得分:0)

部分解决方案:

我从propertyChanged()找到QtAbstractPropertyManager,但每次编辑一个字符时都会提高sed ':1;N;s/\n/,/;0~3b;t1' file 。我希望有一个插槽,当用户完成编辑时会触发它。