desktop: fix saving of column-widths of device and site tables

Qt's memory management scheme is completely broken and messes
with common expectations.

QObjects are organized as a tree. The children are destroyed
in the destructor of QObject. This means that they are destructed
after the destructor of the parent object has run and its
sub-object were destructed. Obviously, this makes no sense as
the child objects should be able to access their parent at
any time.

To restore the commonly expected deterministic order of
construction and destruction, one might simply do away with
Qt's silly object tree and organise things using classical
subobjects. However, that breaks with the Qt-generated UI
classes: The objects generated by these classes are *not*
destructed with the UI class. Instead, they are attached
to the widget's QObject tree. Thus these are again destructed
*after* the widget! Who comes up with such a scheme?

In our case this means that we cannot have models used for
TableViews as subobjects, because the TableView needs the
model to save the column widths in the destructor. Which,
as detailed above is called *after* the desctructor of the
widget! Thus, turn these models into heap-allocated objects
and add them to the QObject tree.

Funilly, this exposes another insanity of Qt's QObject tree:
Children are destructed in order of construction! One would
expect that if objects are constructed in the sequence
A, B, C one can expect that C can, at any time, access B and A.
Not so in Qt: The destruction order is likewise A, B, C!

Thus, take care to init the widgets before the model. Jeez.

Finally, print a warning in the column-saving code of
TableWidget, so that these kind of subtleties are caught
in the future.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2020-11-07 13:20:46 +01:00 committed by Dirk Hohndel
parent 291768b63c
commit 396598430b
7 changed files with 29 additions and 18 deletions

View file

@ -1,12 +1,17 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
#include "TabDiveComputer.h" #include "TabDiveComputer.h"
#include "ui_TabDiveExtraInfo.h" #include "ui_TabDiveExtraInfo.h"
#include "qt-models/divecomputermodel.h"
TabDiveComputer::TabDiveComputer(QWidget *parent) : TabBase(parent) TabDiveComputer::TabDiveComputer(QWidget *parent) : TabBase(parent)
{ {
ui.setupUi(this); ui.setupUi(this);
sortedModel.setSourceModel(&model);
ui.devices->setModel(&sortedModel); DiveComputerModel *model = new DiveComputerModel(this);
sortedModel = new DiveComputerSortedModel(this);
sortedModel->setSourceModel(model);
ui.devices->setModel(sortedModel);
ui.devices->view()->setSelectionBehavior(QAbstractItemView::SelectRows); ui.devices->view()->setSelectionBehavior(QAbstractItemView::SelectRows);
ui.devices->view()->setSelectionMode(QAbstractItemView::SingleSelection); ui.devices->view()->setSelectionMode(QAbstractItemView::SingleSelection);
ui.devices->view()->setSortingEnabled(true); ui.devices->view()->setSortingEnabled(true);
@ -29,5 +34,5 @@ void TabDiveComputer::tableClicked(const QModelIndex &index)
return; return;
if (index.column() == DiveComputerModel::REMOVE) if (index.column() == DiveComputerModel::REMOVE)
sortedModel.remove(index); sortedModel->remove(index);
} }

View file

@ -4,7 +4,8 @@
#include "TabBase.h" #include "TabBase.h"
#include "ui_TabDiveComputer.h" #include "ui_TabDiveComputer.h"
#include "qt-models/divecomputermodel.h"
class DiveComputerSortedModel;
class TabDiveComputer : public TabBase { class TabDiveComputer : public TabBase {
Q_OBJECT Q_OBJECT
@ -16,8 +17,7 @@ public slots:
void tableClicked(const QModelIndex &index); void tableClicked(const QModelIndex &index);
private: private:
Ui::TabDiveComputer ui; Ui::TabDiveComputer ui;
DiveComputerModel model; DiveComputerSortedModel *sortedModel;
DiveComputerSortedModel sortedModel;
}; };
#endif #endif

View file

@ -12,8 +12,10 @@
TabDiveSite::TabDiveSite(QWidget *parent) : TabBase(parent) TabDiveSite::TabDiveSite(QWidget *parent) : TabBase(parent)
{ {
ui.setupUi(this); ui.setupUi(this);
model = new DiveSiteSortedModel(this);
ui.diveSites->setTitle(tr("Dive sites")); ui.diveSites->setTitle(tr("Dive sites"));
ui.diveSites->setModel(&model); ui.diveSites->setModel(model);
// Default: sort by name // Default: sort by name
ui.diveSites->view()->sortByColumn(LocationInformationModel::NAME, Qt::AscendingOrder); ui.diveSites->view()->sortByColumn(LocationInformationModel::NAME, Qt::AscendingOrder);
ui.diveSites->view()->setSortingEnabled(true); ui.diveSites->view()->setSortingEnabled(true);
@ -44,7 +46,7 @@ void TabDiveSite::clear()
void TabDiveSite::diveSiteClicked(const QModelIndex &index) void TabDiveSite::diveSiteClicked(const QModelIndex &index)
{ {
struct dive_site *ds = model.getDiveSite(index); struct dive_site *ds = model->getDiveSite(index);
if (!ds) if (!ds)
return; return;
switch (index.column()) { switch (index.column()) {
@ -80,7 +82,7 @@ void TabDiveSite::diveSiteAdded(struct dive_site *, int idx)
if (idx < 0) if (idx < 0)
return; return;
QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, LocationInformationModel::NAME); QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, LocationInformationModel::NAME);
QModelIndex localIdx = model.mapFromSource(globalIdx); QModelIndex localIdx = model->mapFromSource(globalIdx);
ui.diveSites->view()->setCurrentIndex(localIdx); ui.diveSites->view()->setCurrentIndex(localIdx);
ui.diveSites->view()->edit(localIdx); ui.diveSites->view()->edit(localIdx);
} }
@ -91,7 +93,7 @@ void TabDiveSite::diveSiteChanged(struct dive_site *ds, int field)
if (idx < 0) if (idx < 0)
return; return;
QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, field); QModelIndex globalIdx = LocationInformationModel::instance()->index(idx, field);
QModelIndex localIdx = model.mapFromSource(globalIdx); QModelIndex localIdx = model->mapFromSource(globalIdx);
ui.diveSites->view()->scrollTo(localIdx); ui.diveSites->view()->scrollTo(localIdx);
} }
@ -102,7 +104,7 @@ void TabDiveSite::on_purgeUnused_clicked()
void TabDiveSite::on_filterText_textChanged(const QString &text) void TabDiveSite::on_filterText_textChanged(const QString &text)
{ {
model.setFilter(text); model->setFilter(text);
} }
QVector<dive_site *> TabDiveSite::selectedDiveSites() QVector<dive_site *> TabDiveSite::selectedDiveSites()
@ -111,7 +113,7 @@ QVector<dive_site *> TabDiveSite::selectedDiveSites()
QVector<dive_site *> sites; QVector<dive_site *> sites;
sites.reserve(indices.size()); sites.reserve(indices.size());
for (const QModelIndex &idx: indices) { for (const QModelIndex &idx: indices) {
struct dive_site *ds = model.getDiveSite(idx); struct dive_site *ds = model->getDiveSite(idx);
sites.append(ds); sites.append(ds);
} }
return sites; return sites;

View file

@ -4,7 +4,8 @@
#include "TabBase.h" #include "TabBase.h"
#include "ui_TabDiveSite.h" #include "ui_TabDiveSite.h"
#include "qt-models/divelocationmodel.h"
class DiveSiteSortedModel;
class TabDiveSite : public TabBase { class TabDiveSite : public TabBase {
Q_OBJECT Q_OBJECT
@ -22,7 +23,7 @@ private slots:
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected); void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected);
private: private:
Ui::TabDiveSite ui; Ui::TabDiveSite ui;
DiveSiteSortedModel model; DiveSiteSortedModel *model;
QVector<dive_site *> selectedDiveSites(); QVector<dive_site *> selectedDiveSites();
void hideEvent(QHideEvent *) override; void hideEvent(QHideEvent *) override;
void showEvent(QShowEvent *) override; void showEvent(QShowEvent *) override;

View file

@ -81,12 +81,15 @@ TableView::~TableView()
s.remove(""); s.remove("");
} else if (ui.tableView->model()) { } else if (ui.tableView->model()) {
for (int i = 0; i < ui.tableView->model()->columnCount(); i++) { for (int i = 0; i < ui.tableView->model()->columnCount(); i++) {
if (ui.tableView->columnWidth(i) == defaultColumnWidth(i)) if (ui.tableView->columnWidth(i) == defaultColumnWidth(i)) {
s.remove(QString("colwidth%1").arg(i)); s.remove(QString("colwidth%1").arg(i));
else } else {
s.setValue(QString("colwidth%1").arg(i), ui.tableView->columnWidth(i)); s.setValue(QString("colwidth%1").arg(i), ui.tableView->columnWidth(i));
} }
} }
} else {
qWarning("TableView %s without model", qPrintable(objectName()));
}
s.endGroup(); s.endGroup();
} }

View file

@ -215,7 +215,7 @@ bool DiveSiteSortedModel::lessThan(const QModelIndex &i1, const QModelIndex &i2)
} }
} }
DiveSiteSortedModel::DiveSiteSortedModel() DiveSiteSortedModel::DiveSiteSortedModel(QObject *parent) : QSortFilterProxyModel(parent)
{ {
setSourceModel(LocationInformationModel::instance()); setSourceModel(LocationInformationModel::instance());
} }

View file

@ -49,7 +49,7 @@ private:
bool setData(const QModelIndex &index, const QVariant &value, int role) override; bool setData(const QModelIndex &index, const QVariant &value, int role) override;
#endif // SUBSURFACE_MOBILE #endif // SUBSURFACE_MOBILE
public: public:
DiveSiteSortedModel(); DiveSiteSortedModel(QObject *parent = nullptr);
QStringList allSiteNames() const; QStringList allSiteNames() const;
void setFilter(const QString &text); void setFilter(const QString &text);
struct dive_site *getDiveSite(const QModelIndex &idx); struct dive_site *getDiveSite(const QModelIndex &idx);