QObject的setUserData和setProperty——Qt
作者:互联网
QObjectUserData* userData(uint id) const;
void QObject::setUserData(uint id, QObjectUserData* data)
{
Q_D(QObject);
if (!d->extraData)
d->extraData = new QObjectPrivate::ExtraData;
if (d->extraData->userData.size() <= (int) id)
d->extraData->userData.resize((int) id + 1);
d->extraData->userData[id] = data;
}
QVariant property(const char *name) const;
bool QObject::setProperty(const char *name, const QVariant &value)
{
Q_D(QObject);
const QMetaObject* meta = metaObject();
if (!name || !meta)
return false;
int id = meta->indexOfProperty(name);
if (id < 0) {
if (!d->extraData)
d->extraData = new QObjectPrivate::ExtraData;
const int idx = d->extraData->propertyNames.indexOf(name);
if (!value.isValid()) {
if (idx == -1)
return false;
d->extraData->propertyNames.removeAt(idx);
d->extraData->propertyValues.removeAt(idx);
} else {
if (idx == -1) {
d->extraData->propertyNames.append(name);
d->extraData->propertyValues.append(value);
} else {
if (value.userType() == d->extraData->propertyValues.at(idx).userType()
&& value == d->extraData->propertyValues.at(idx))
return false;
d->extraData->propertyValues[idx] = value;
}
}
QDynamicPropertyChangeEvent ev(name);
QCoreApplication::sendEvent(this, &ev);
return false;
}
QMetaProperty p = meta->property(id);
#ifndef QT_NO_DEBUG
if (!p.isWritable())
qWarning("%s::setProperty: Property \"%s\" invalid,"
" read-only or does not exist", metaObject()->className(), name);
#endif
return p.write(this, value);
}
标签:setUserData,const,Qt,idx,extraData,value,setProperty,id,name 来源: https://blog.csdn.net/Liuqz2009/article/details/120902999