Commit e28d6127 authored by Artur Grunau's avatar Artur Grunau
Browse files

DataNamePropertyWidget: fix null pointer deref

If a DataNamePropertyWidget is created for a DataNameProperty whose
AccessInfo == READ, it doesn't instantiate a QLineEdit to display the
property's value (QComboBox is used instead).

Previously, the updateWidgetFromProperty method didn't check AccessInfo
to see what display widget was in use, and simply always dereferenced
the _lineEdit pointer. If AccessInfo == READ, it was null and the
application crashed.

Now updateWidgetFromProperty accesses only the combo box if
AccessInfo == READ, and uses the line edit otherwise.

Fixes #99
parent 97a051c5
...@@ -53,14 +53,9 @@ namespace campvis { ...@@ -53,14 +53,9 @@ namespace campvis {
_combobox->addItems(sl); _combobox->addItems(sl);
dc->s_dataAdded.connect(this, &DataNamePropertyWidget::onDataAdded); dc->s_dataAdded.connect(this, &DataNamePropertyWidget::onDataAdded);
int idx = _combobox->findText(QString::fromStdString(property->getValue())); setCurrentComboBoxText(QString::fromStdString(property->getValue()));
if (idx != -1)
_combobox->setCurrentIndex(idx);
else
_combobox->setEditText(QString::fromStdString(property->getValue()));
} }
addWidget(_combobox); addWidget(_combobox);
connect(_combobox, SIGNAL(editTextChanged(const QString&)), this, SLOT(onTextChanged(const QString&))); connect(_combobox, SIGNAL(editTextChanged(const QString&)), this, SLOT(onTextChanged(const QString&)));
} }
...@@ -80,13 +75,28 @@ namespace campvis { ...@@ -80,13 +75,28 @@ namespace campvis {
void DataNamePropertyWidget::updateWidgetFromProperty() { void DataNamePropertyWidget::updateWidgetFromProperty() {
DataNameProperty* prop = static_cast<DataNameProperty*>(_property); DataNameProperty* prop = static_cast<DataNameProperty*>(_property);
QString qs = QString::fromStdString(prop->getValue()); QString qs = QString::fromStdString(prop->getValue());
if (_lineEdit->text() != qs) {
if (prop->getAccessInfo() == DataNameProperty::READ) {
_combobox->blockSignals(true);
setCurrentComboBoxText(qs);
_combobox->blockSignals(false);
}
else if (_lineEdit->text() != qs) {
_lineEdit->blockSignals(true); _lineEdit->blockSignals(true);
_lineEdit->setText(qs); _lineEdit->setText(qs);
_lineEdit->blockSignals(false); _lineEdit->blockSignals(false);
} }
} }
void DataNamePropertyWidget::setCurrentComboBoxText(const QString& text) {
int idx = _combobox->findText(text);
if (idx != -1)
_combobox->setCurrentIndex(idx);
else
_combobox->setEditText(text);
}
void DataNamePropertyWidget::onTextChanged(const QString& text) { void DataNamePropertyWidget::onTextChanged(const QString& text) {
DataNameProperty* prop = static_cast<DataNameProperty*>(_property); DataNameProperty* prop = static_cast<DataNameProperty*>(_property);
++_ignorePropertyUpdates; ++_ignorePropertyUpdates;
...@@ -101,4 +111,4 @@ namespace campvis { ...@@ -101,4 +111,4 @@ namespace campvis {
} }
} }
\ No newline at end of file
...@@ -45,7 +45,7 @@ namespace campvis { ...@@ -45,7 +45,7 @@ namespace campvis {
* Widget for a DataNameProperty * Widget for a DataNameProperty
*/ */
class DataNamePropertyWidget : public AbstractPropertyWidget { class DataNamePropertyWidget : public AbstractPropertyWidget {
Q_OBJECT; Q_OBJECT
public: public:
/** /**
...@@ -72,6 +72,16 @@ namespace campvis { ...@@ -72,6 +72,16 @@ namespace campvis {
void onTextChanged(const QString& text); void onTextChanged(const QString& text);
private: private:
/**
* Set the combo box's current text to the provided value.
*
* If \c text can already be found in the combo box, the corresponding item is made active. Otherwise, \c text
* is set as the contents of the combo box's text edit.
*
* \param text text to be set in the combo box
*/
void setCurrentComboBoxText(const QString& text);
QLineEdit* _lineEdit; QLineEdit* _lineEdit;
QComboBox* _combobox; QComboBox* _combobox;
DataContainer* _dc; DataContainer* _dc;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment