本片咱们话接上篇《Qt通用属性工具:随心定义,随时可见(一)》,讲讲自定义的对象属性如何绑定通用属性编辑工具。
同样,我们先准备一个比较简单的demo,先看看效果。
这里我们自定义了一个Student
类型,为它咱们定义了三个属性,
id
: 只读。在上图的演示中,我们也可以看到,我尝试对id进行修改,实际并没有生效。这里实际可以进一步优化通用属性编辑器,当只读属性时,直接灰显,不允许修改可能更好(后续优化)
name
: 可读、可写,当name发生变化时,我们还让他发出一个nameChanged
的信号。从上图我们修改名字,对应绑定的槽函数的打印输出信息可以看到。喔嚯,MVC,真是爽歪歪。
age
: 可读可写。修改值,自然也可以生效
#include "widget.h"
#include <QApplication>
#include <QObject>
#include "QtPropertyEditor.h"
#include <QHBoxLayout>
#include <QDebug>
class Student : public QObject
{
Q_OBJECT
Q_PROPERTY(QString id READ id)
Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
Q_PROPERTY(int age READ age WRITE setAge)
public:
Student():_id("xxoo"){}
Student(const QString id) : _id(id){}
Student(const QString id,const QString name)
: _id(id),_name(name){}
Student(const QString id,const QString name,const int age)
: _id(id),_name(name),_age(age){}
Student(const Student& that){
this->_name = that.name();
this->_age = that.age();
}
const QString id() const { return _id;}
const QString name() const {return _name;}
void setName(const QString& name){ _name = name; nameChanged(name);}
void setName(const QString&& name){_name = name;}
const int age() const { return _age;}
void setAge(const int& age){ _age = age;}
void setAge(const int&& age){ _age = age;}
signals:
void nameChanged(QString name);
private:
const QString _id;
QString _name;
int _age;
};
Q_DECLARE_METATYPE(Student)
class StudentList: public QObject
{
Q_OBJECT
Q_PROPERTY(QList<Student> students READ students WRITE setStudents)
Q_PROPERTY(QString desc READ desc WRITE setDesc)
public:
StudentList(){}
StudentList(const QList<Student> lstu ):_students(lstu){}
StudentList(const StudentList& that){
this->_students = that.students();
}
const QList<Student> students() const { return _students;}
void setStudents(const QList<Student>& lstu){ _students = lstu;}
void setStudents(const QList<Student>&& lstu){ _students = lstu;}
const QString desc() const { return _desc;}
void setDesc(const QString& desc ){ _desc = desc;}
void setDesc(const QString && desc){ _desc = desc;}
private:
QList<Student> _students;
QString _desc;
};
Q_DECLARE_METATYPE(StudentList)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qRegisterMetaType<Student>("Student");
qRegisterMetaType<StudentList>("StudentList");
// Ojbect
Student stu0("1001","zhaolei",22);
stu0.setObjectName(QStringLiteral("学生0"));
Student stu1("1002","cangjingkong",18); // 苍老师用永远18,没人能有意见吧
stu1.setObjectName(QStringLiteral("学生1"));
QList<Student> l;
l.append(stu0);
l.append(stu1);
StudentList lstu(l);
lstu.setDesc(QStringLiteral("三年一班学生信息"));
for(const auto& stu : lstu.students()){
QObject::connect(&stu,&Student::nameChanged,[](QString name){
qDebug() << __FUNCTION__ << __LINE__ << " namechanged : " << name;
});
}
// UI bind Object
QtPropertyEditor::QtPropertyTreeEditor tree_editor;
tree_editor.treeModel.setMaxDepth(4);
tree_editor.treeModel.propertyNames = QtPropertyEditor::getPropertyNames(&lstu);
tree_editor.treeModel.setObject(&lstu);
tree_editor.resizeColumnsToContents();
tree_editor.show();
return a.exec();
}
#include "main.moc"
这里属性节点名称有显示,可惜不如我所愿,关于list节点却没有将子节点一一展开,这是此通用属性编辑器需要进一步优化的issue,敬请期待续集吧~~