Created
February 23, 2016 18:59
-
-
Save mcleary/33f1551db62a66a96a81 to your computer and use it in GitHub Desktop.
This functions show how to connect to a signal defined by a QMetaProperty. This is usefull to create editor for an object without knowing the object itself.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QWidget *ProjectWidget::_createBoolTreeEntry(QObject *cloudData, QMetaProperty property) | |
{ | |
auto checkbox = new QCheckBox(this); | |
checkbox->setChecked(property.read(cloudData).toBool()); | |
connect(checkbox, &QCheckBox::clicked, [=](bool checked) | |
{ | |
property.write(cloudData, checked); | |
}); | |
auto checkboxMetaObject = checkbox->metaObject(); | |
int setCheckedMetaMethodIndex = checkboxMetaObject->indexOfMethod("setChecked(bool)"); | |
auto setCheckedMetaMethod = checkboxMetaObject->method(setCheckedMetaMethodIndex); | |
connect(cloudData, property.notifySignal(), checkbox, setCheckedMetaMethod); | |
return checkbox; | |
} | |
QWidget *ProjectWidget::_createIntTreeEntry(PointCloudData *cloudData, QMetaProperty property) | |
{ | |
QWidget* itemWidget = nullptr; | |
if(property.isWritable()) | |
{ | |
auto slider = new QSlider(Qt::Horizontal, this); | |
slider->setValue(property.read(cloudData).toInt()); | |
auto propertyInfo = cloudData->propertyInfo(property.name()); | |
slider->setMinimum(propertyInfo.MinValue); | |
slider->setMaximum(propertyInfo.MaxValue); | |
connect(slider, &QSlider::valueChanged, [=](int value) | |
{ | |
property.write(cloudData, value); | |
}); | |
itemWidget = slider; | |
} | |
else | |
{ | |
auto label = new IntLabel(this); | |
label->setValue(property.read(cloudData).toInt()); | |
itemWidget = label; | |
} | |
auto widgetMetaObject = itemWidget->metaObject(); | |
int setValueMetaMethodIndex = widgetMetaObject->indexOfMethod("setValue(int)"); | |
auto setValueMetaMethod = widgetMetaObject->method(setValueMetaMethodIndex); | |
connect(cloudData, property.notifySignal(), itemWidget, setValueMetaMethod); | |
return itemWidget; | |
} | |
QWidget *ProjectWidget::_createColorTreeEntry(QObject *cloudData, QMetaProperty property) | |
{ | |
auto colorBtn = new QPushButton(this); | |
auto color = property.read(cloudData).value<QColor>(); | |
ColorUtils::setButtonColor(colorBtn, color); | |
auto changeColorCallbackWrapper = [=](QColor newColor) | |
{ | |
property.write(cloudData, newColor); | |
ColorUtils::setButtonColor(colorBtn, newColor); | |
}; | |
connect(colorBtn, &QPushButton::clicked, [=] | |
{ | |
auto previousColor = property.read(cloudData).value<QColor>(); | |
QColorDialog colorDialog(color); | |
connect(&colorDialog, &QColorDialog::currentColorChanged, changeColorCallbackWrapper); | |
connect(&colorDialog, &QColorDialog::colorSelected, changeColorCallbackWrapper); | |
if(!colorDialog.exec()) | |
{ | |
changeColorCallbackWrapper(previousColor); | |
} | |
}); | |
return colorBtn; | |
} | |
QWidget *ProjectWidget::_createTextEntry(QObject *cloudData, QMetaProperty property) | |
{ | |
auto label = new QLabel(this); | |
label->setText(property.read(cloudData).toString()); | |
if(property.hasNotifySignal()) | |
{ | |
auto labelMetaObject = label->metaObject(); | |
int setTextMetaMethodIndex = labelMetaObject->indexOfMethod("setText(QString)"); | |
auto setTextMetaMethod = labelMetaObject->method(setTextMetaMethodIndex); | |
connect(cloudData, property.notifySignal(), label, setTextMetaMethod); | |
} | |
return label; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment