Fix crash and wrong behavior caused by qt model/view changes

* Crash caused by list varibles getting initialized after the models
using them. Previously Qt didn't try to access them so early. Move them
to the models as there is no need to share them betwen view and models.
* Fix empty color list by using begin/endResetModel instead of
dataChanged to signal changes in data.
This commit is contained in:
Kārlis Seņko 2021-04-13 21:28:38 +03:00 committed by karliss
parent da17c6e411
commit 820aa98b7b
5 changed files with 38 additions and 34 deletions

View File

@ -372,6 +372,7 @@ bool ColorSettingsModel::setData(const QModelIndex &index, const QVariant &value
void ColorSettingsModel::updateTheme()
{
beginResetModel();
theme.clear();
QJsonObject obj = ThemeWorker().getTheme(Config()->getColorTheme()).object();
@ -391,9 +392,7 @@ void ColorSettingsModel::updateTheme()
int r = s1.compare(s2, Qt::CaseSensitivity::CaseInsensitive);
return r < 0;
});
if (!theme.isEmpty()) {
dataChanged(index(0), index(theme.size() - 1));
}
endResetModel();
}
QJsonDocument ColorSettingsModel::getTheme() const

View File

@ -9,14 +9,11 @@
#include <QShortcut>
#include <QTreeWidget>
ImportsModel::ImportsModel(QList<ImportDescription> *imports, QObject *parent)
: AddressableItemModel(parent), imports(imports)
{
}
ImportsModel::ImportsModel(QObject *parent) : AddressableItemModel(parent) {}
int ImportsModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : imports->count();
return parent.isValid() ? 0 : imports.count();
}
int ImportsModel::columnCount(const QModelIndex &) const
@ -26,7 +23,7 @@ int ImportsModel::columnCount(const QModelIndex &) const
QVariant ImportsModel::data(const QModelIndex &index, int role) const
{
const ImportDescription &import = imports->at(index.row());
const ImportDescription &import = imports.at(index.row());
switch (role) {
case Qt::ForegroundRole:
if (index.column() < ImportsModel::ColumnCount) {
@ -91,22 +88,29 @@ QVariant ImportsModel::headerData(int section, Qt::Orientation, int role) const
RVA ImportsModel::address(const QModelIndex &index) const
{
const ImportDescription &import = imports->at(index.row());
const ImportDescription &import = imports.at(index.row());
return import.plt;
}
QString ImportsModel::name(const QModelIndex &index) const
{
const ImportDescription &import = imports->at(index.row());
const ImportDescription &import = imports.at(index.row());
return import.name;
}
QString ImportsModel::libname(const QModelIndex &index) const
{
const ImportDescription &import = imports->at(index.row());
const ImportDescription &import = imports.at(index.row());
return import.libname;
}
void ImportsModel::reload()
{
beginResetModel();
imports = Core()->getAllImports();
endResetModel();
}
ImportsProxyModel::ImportsProxyModel(ImportsModel *sourceModel, QObject *parent)
: AddressableFilterProxyModel(sourceModel, parent)
{
@ -162,7 +166,7 @@ bool ImportsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &rig
ImportsWidget::ImportsWidget(MainWindow *main)
: ListDockWidget(main),
importsModel(new ImportsModel(&imports, this)),
importsModel(new ImportsModel(this)),
importsProxyModel(new ImportsProxyModel(importsModel, this))
{
setWindowTitle(tr("Imports"));
@ -184,8 +188,6 @@ ImportsWidget::~ImportsWidget() {}
void ImportsWidget::refreshImports()
{
importsModel->beginResetModel();
imports = Core()->getAllImports();
importsModel->endResetModel();
importsModel->reload();
qhelpers::adjustColumns(ui->treeView, 4, 0);
}

View File

@ -52,7 +52,7 @@ private:
"OemToChar|OemToCharA|OemToCharW|CharToOemBuffA|CharToOemBuffW|alloca|_"
"alloca|strlen|wcslen|_mbslen|_mbstrlen|StrLen|lstrlen|"
"ChangeWindowMessageFilter)\\z"));
QList<ImportDescription> *imports;
QList<ImportDescription> imports;
public:
enum Column {
@ -66,7 +66,7 @@ public:
};
enum Role { ImportDescriptionRole = Qt::UserRole, AddressRole };
ImportsModel(QList<ImportDescription> *imports, QObject *parent = nullptr);
ImportsModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
@ -77,6 +77,7 @@ public:
RVA address(const QModelIndex &index) const override;
QString name(const QModelIndex &index) const override;
QString libname(const QModelIndex &index) const;
void reload();
};
class ImportsProxyModel : public AddressableFilterProxyModel
@ -105,7 +106,6 @@ private slots:
private:
ImportsModel *importsModel;
ImportsProxyModel *importsProxyModel;
QList<ImportDescription> imports;
void highlightUnsafe();
};

View File

@ -6,14 +6,11 @@
#include <QShortcut>
#include <QTreeWidget>
RelocsModel::RelocsModel(QList<RelocDescription> *relocs, QObject *parent)
: AddressableItemModel<QAbstractTableModel>(parent), relocs(relocs)
{
}
RelocsModel::RelocsModel(QObject *parent) : AddressableItemModel<QAbstractTableModel>(parent) {}
int RelocsModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : relocs->count();
return parent.isValid() ? 0 : relocs.count();
}
int RelocsModel::columnCount(const QModelIndex &) const
@ -23,7 +20,7 @@ int RelocsModel::columnCount(const QModelIndex &) const
QVariant RelocsModel::data(const QModelIndex &index, int role) const
{
const RelocDescription &reloc = relocs->at(index.row());
const RelocDescription &reloc = relocs.at(index.row());
switch (role) {
case Qt::DisplayRole:
switch (index.column()) {
@ -67,16 +64,23 @@ QVariant RelocsModel::headerData(int section, Qt::Orientation, int role) const
RVA RelocsModel::address(const QModelIndex &index) const
{
const RelocDescription &reloc = relocs->at(index.row());
const RelocDescription &reloc = relocs.at(index.row());
return reloc.vaddr;
}
QString RelocsModel::name(const QModelIndex &index) const
{
const RelocDescription &reloc = relocs->at(index.row());
const RelocDescription &reloc = relocs.at(index.row());
return reloc.name;
}
void RelocsModel::reload()
{
beginResetModel();
relocs = Core()->getAllRelocs();
endResetModel();
}
RelocsProxyModel::RelocsProxyModel(RelocsModel *sourceModel, QObject *parent)
: AddressableFilterProxyModel(sourceModel, parent)
{
@ -121,7 +125,7 @@ bool RelocsProxyModel::lessThan(const QModelIndex &left, const QModelIndex &righ
RelocsWidget::RelocsWidget(MainWindow *main)
: ListDockWidget(main),
relocsModel(new RelocsModel(&relocs, this)),
relocsModel(new RelocsModel(this)),
relocsProxyModel(new RelocsProxyModel(relocsModel, this))
{
setWindowTitle(tr("Relocs"));
@ -140,8 +144,6 @@ RelocsWidget::~RelocsWidget() {}
void RelocsWidget::refreshRelocs()
{
relocsModel->beginResetModel();
relocs = Core()->getAllRelocs();
relocsModel->endResetModel();
relocsModel->reload();
qhelpers::adjustColumns(ui->treeView, 3, 0);
}

View File

@ -19,13 +19,13 @@ class RelocsModel : public AddressableItemModel<QAbstractTableModel>
friend RelocsWidget;
private:
QList<RelocDescription> *relocs;
QList<RelocDescription> relocs;
public:
enum Column { VAddrColumn = 0, TypeColumn, NameColumn, CommentColumn, ColumnCount };
enum Role { RelocDescriptionRole = Qt::UserRole, AddressRole };
RelocsModel(QList<RelocDescription> *relocs, QObject *parent = nullptr);
RelocsModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
@ -35,6 +35,8 @@ public:
RVA address(const QModelIndex &index) const override;
QString name(const QModelIndex &index) const override;
void reload();
};
class RelocsProxyModel : public AddressableFilterProxyModel
@ -63,7 +65,6 @@ private slots:
private:
RelocsModel *relocsModel;
RelocsProxyModel *relocsProxyModel;
QList<RelocDescription> relocs;
};
#endif // RELOCSWIDGET_H