diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2020-02-27 19:46:48 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-02-28 08:12:26 -0800 |
commit | cb80ff746b687a3ad29b53d9f633cbdc6b142c71 (patch) | |
tree | 50a22d892fffd1175dbba94b49d1b712a604039b /desktop-widgets/modeldelegates.cpp | |
parent | 9214bdb3c576c0e26745939a18ea75634aef8ce4 (diff) | |
download | subsurface-cb80ff746b687a3ad29b53d9f633cbdc6b142c71.tar.gz |
crash fix: Don't cast to CylindersModel or CylindersModelFiltered
The tank-info-delegate cast its model to CylindersModelFiltered,
since this is what the equipment-tab uses since implementing the
filtering of unused cylinders. However, the planner users the same
delegate and still uses the unfiltered CylindersModel. This means
that the (dynamic) cast returns a null pointer and crashes.
One possibility would be to derive CylindersModelFiltered and
CylindersModel from the same class that defines virtual functions
and cast to that class.
This is a different attempt: don't cast (i.e. stay with a
QAbstractItemModel and play it via Qt's model-view system. Firstly,
replace the passInData function by a role to setData(). Secondly,
read the working-pressure and size via new columns using data().
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'desktop-widgets/modeldelegates.cpp')
-rw-r--r-- | desktop-widgets/modeldelegates.cpp | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/desktop-widgets/modeldelegates.cpp b/desktop-widgets/modeldelegates.cpp index ed4833550..2717f37bc 100644 --- a/desktop-widgets/modeldelegates.cpp +++ b/desktop-widgets/modeldelegates.cpp @@ -238,7 +238,7 @@ static struct RevertCylinderData { void TankInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelIndex&) const { - CylindersModelFiltered *mymodel = qobject_cast<CylindersModelFiltered *>(currCombo.model); + QAbstractItemModel *mymodel = currCombo.model; TankInfoModel *tanks = TankInfoModel::instance(); QModelIndexList matches = tanks->match(tanks->index(0, 0), Qt::DisplayRole, currCombo.activeText); int row; @@ -255,8 +255,8 @@ void TankInfoDelegate::setModelData(QWidget*, QAbstractItemModel*, const QModelI int tankPressure = tanks->data(tanks->index(row, TankInfoModel::BAR)).toInt(); mymodel->setData(IDX(CylindersModel::TYPE), cylinderName, Qt::EditRole); - mymodel->passInData(IDX(CylindersModel::WORKINGPRESS), tankPressure); - mymodel->passInData(IDX(CylindersModel::SIZE), tankSize); + mymodel->setData(IDX(CylindersModel::WORKINGPRESS), tankPressure, CylindersModel::PASS_IN_ROLE); + mymodel->setData(IDX(CylindersModel::SIZE), tankSize, CylindersModel::PASS_IN_ROLE); } TankInfoDelegate::TankInfoDelegate(QObject *parent) : ComboBoxDelegate(TankInfoModel::instance(), parent, true) @@ -277,10 +277,10 @@ void TankInfoDelegate::editorClosed(QWidget*, QAbstractItemDelegate::EndEditHint { if (hint == QAbstractItemDelegate::NoHint || hint == QAbstractItemDelegate::RevertModelCache) { - CylindersModelFiltered *mymodel = qobject_cast<CylindersModelFiltered *>(currCombo.model); + QAbstractItemModel *mymodel = currCombo.model; mymodel->setData(IDX(CylindersModel::TYPE), currCylinderData.type, Qt::EditRole); - mymodel->passInData(IDX(CylindersModel::WORKINGPRESS), currCylinderData.pressure); - mymodel->passInData(IDX(CylindersModel::SIZE), currCylinderData.size); + mymodel->setData(IDX(CylindersModel::WORKINGPRESS), currCylinderData.pressure, CylindersModel::PASS_IN_ROLE); + mymodel->setData(IDX(CylindersModel::SIZE), currCylinderData.size, CylindersModel::PASS_IN_ROLE); } } @@ -289,11 +289,11 @@ QWidget *TankInfoDelegate::createEditor(QWidget *parent, const QStyleOptionViewI // ncreate editor needs to be called before because it will populate a few // things in the currCombo global var. QWidget *delegate = ComboBoxDelegate::createEditor(parent, option, index); - CylindersModelFiltered *mymodel = qobject_cast<CylindersModelFiltered *>(currCombo.model); - cylinder_t *cyl = mymodel->cylinderAt(index); - currCylinderData.type = cyl->type.description; - currCylinderData.pressure = cyl->type.workingpressure.mbar; - currCylinderData.size = cyl->type.size.mliter; + QAbstractItemModel *model = currCombo.model; + int row = index.row(); + currCylinderData.type = model->data(model->index(row, CylindersModel::TYPE)).value<QString>(); + currCylinderData.pressure = model->data(model->index(row, CylindersModel::WORKINGPRESS_INT)).value<int>(); + currCylinderData.size = model->data(model->index(row, CylindersModel::SIZE_INT)).value<int>(); MainWindow::instance()->graphics->setReplot(false); return delegate; } |