fix: resolve Q_CLASSINFO metadata for child properties by matching all path segments

resloveMetaData() only matched Q_CLASSINFO against the first segment of
  the property path (e.g. "ptPosition" from "ptPosition.x"), so child
  handles like x/y under a QPointF tree never picked up Min/Max/Step
  metadata declared with Q_CLASSINFO("x", ...). This caused the x/y
  NumberBox editors to fall back to numeric_limits min/max, clamping
  negative values to ~0.

  Now iterates path segments from most specific (last) to least specific
  (first), so child property names correctly match their Q_CLASSINFO
  annotations.

  Also add Q_CLASSINFO("x"/"y", "Min=-1000,Max=1000,Step=1") on
  GraphicsTextItem to constrain position editing to a reasonable range.
This commit is contained in:
jessequ 2026-07-31 16:39:24 +08:00
parent e63fae92c8
commit e1671fdbe3
2 changed files with 25 additions and 15 deletions

View File

@ -42,23 +42,29 @@ QPropertyHandle::QPropertyHandle(QObject* inParent, QMetaType inType, QString in
void QPropertyHandle::resloveMetaData() void QPropertyHandle::resloveMetaData()
{ {
auto metaObj = parent()->metaObject(); auto metaObj = parent()->metaObject();
auto firstField = getPropertyPath().split(".").first(); const QStringList pathSegments = getPropertyPath().split(".");
for (int i = 0; i < metaObj->classInfoCount(); i++) {
auto metaClassInfo = metaObj->classInfo(i); // Try each segment from most specific (last) to least specific (first)
if (metaClassInfo.name() == firstField) { for (int seg = pathSegments.size() - 1; seg >= 0; --seg) {
QStringList fields = QString(metaClassInfo.value()).split(",", Qt::SplitBehaviorFlags::SkipEmptyParts); const QString& fieldName = pathSegments[seg];
for (auto field : fields) { if (fieldName.isEmpty()) continue;
QStringList pair = field.split("="); for (int i = 0; i < metaObj->classInfoCount(); i++) {
QString key, value; auto metaClassInfo = metaObj->classInfo(i);
if (pair.size() > 0) { if (metaClassInfo.name() == fieldName) {
key = pair.first().trimmed(); QStringList fields = QString(metaClassInfo.value()).split(",", Qt::SplitBehaviorFlags::SkipEmptyParts);
for (auto field : fields) {
QStringList pair = field.split("=");
QString key, value;
if (pair.size() > 0) {
key = pair.first().trimmed();
}
if (pair.size() > 1) {
value = pair[1].trimmed();
}
mMetaData[key] = value;
} }
if (pair.size() > 1) { return;
value = pair[1].trimmed();
}
mMetaData[key] = value;
} }
return;
} }
} }
} }

View File

@ -12,6 +12,10 @@ class GraphicsTextItem : public AbstractShapeType<QGraphicsTextItem>,
public IPropertyGroupProvider public IPropertyGroupProvider
{ {
Q_OBJECT Q_OBJECT
Q_CLASSINFO("x", "Min=-1000,Max=1000,Step=1,Group=ptPosition")
Q_CLASSINFO("y", "Min=-1000,Max=1000,Step=1,Group=ptPosition")
Q_PROPERTY(QString text READ toPlainText WRITE setPlainText) Q_PROPERTY(QString text READ toPlainText WRITE setPlainText)
Q_PROPERTY(QPointF ptPosition READ pos WRITE setPos FINAL) Q_PROPERTY(QPointF ptPosition READ pos WRITE setPos FINAL)
Q_PROPERTY(QFont textFont READ textFont WRITE setTextFont) Q_PROPERTY(QFont textFont READ textFont WRITE setTextFont)