Qt5.9 c++开发指南 3.1.3
Qt 帮助文档 The Property System
获取类的属性和方法
属性系统介绍
Qt5.9 c++开发指南 3.1.3
Qt 帮助文档:
属性的使用
为控件自定义属性:
enum CusType
{
TYPE_ONE = 1,
TYPE_TWO = 2,
TYPE_THREE = 3
};
Q_DECLARE_METATYPE(CusType)
void MainWindow::dealProperty()
{
QPushButton *button = new QPushButton;
QObject *object = button;
button->setDown(true); //QPushButton 内置属性,是否 pressed down
button->setProperty("color", "red");
qDebug() << "button setDown value:" << button->isDown();
qDebug() << "button color value:" << object->property("color");
object->setProperty("down", true); //自定义属性
QColor color(Qt::red);
object->setProperty("bgColor", color); //自定义属性
object->setProperty("cusType", QVariant::fromValue(CusType::TYPE_ONE)); //自定义属性
qDebug() << "object bgColor value:" << object->property("bgColor");
qDebug() << "object cusType value:" << object->property("cusType").toInt();
}
输出: