Fix usage of Qt::CheckStateRole in preview model

Qt::CheckStateRole expects an enum, not a bool

Also set the flag that the item it user checkable, otherwise it can't be changed

BUG: 471999
This commit is contained in:
Nicolas Fella 2023-07-08 22:34:08 +02:00
parent 88ebcd42db
commit 31a8866ac0
5 changed files with 16 additions and 10 deletions

View file

@ -175,7 +175,7 @@ void ContextMenuSettingsPage::applySettings()
for (int i = 0; i < model->rowCount(); ++i) {
const QModelIndex index = model->index(i, 0);
const QString service = model->data(index, ServiceModel::DesktopEntryNameRole).toString();
const bool checked = model->data(index, Qt::CheckStateRole).toBool();
const bool checked = model->data(index, Qt::CheckStateRole).value<Qt::CheckState>() == Qt::Checked;
if (service.startsWith(VersionControlServicePrefix)) {
if (checked) {
@ -240,7 +240,7 @@ void ContextMenuSettingsPage::restoreDefaults()
const bool checked =
!service.startsWith(VersionControlServicePrefix) && service != QLatin1String(DeleteService) && service != QLatin1String(CopyToMoveToService);
model->setData(index, checked, Qt::CheckStateRole);
model->setData(index, checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
}
}
@ -357,7 +357,7 @@ void ContextMenuSettingsPage::addRow(const QString &icon, const QString &text, c
m_serviceModel->setData(index, icon, Qt::DecorationRole);
m_serviceModel->setData(index, text, Qt::DisplayRole);
m_serviceModel->setData(index, value, ServiceModel::DesktopEntryNameRole);
m_serviceModel->setData(index, checked, Qt::CheckStateRole);
m_serviceModel->setData(index, checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
}
#include "moc_contextmenusettingspage.cpp"

View file

@ -104,7 +104,7 @@ void PreviewsSettingsPage::applySettings()
m_enabledPreviewPlugins.clear();
for (int i = 0; i < rowCount; ++i) {
const QModelIndex index = model->index(i, 0);
const bool checked = model->data(index, Qt::CheckStateRole).toBool();
const bool checked = model->data(index, Qt::CheckStateRole).value<Qt::CheckState>() == Qt::Checked;
if (checked) {
const QString enabledPlugin = model->data(index, Qt::UserRole).toString();
m_enabledPreviewPlugins.append(enabledPlugin);
@ -153,7 +153,7 @@ void PreviewsSettingsPage::loadPreviewPlugins()
model->insertRow(0);
const QModelIndex index = model->index(0, 0);
model->setData(index, show, Qt::CheckStateRole);
model->setData(index, show ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
model->setData(index, plugin.name(), Qt::DisplayRole);
model->setData(index, plugin.pluginId(), ServiceModel::DesktopEntryNameRole);
}

View file

@ -74,7 +74,7 @@ void ServiceItemDelegate::updateItemWidgets(const QList<QWidget *> &widgets, con
if (!iconName.isEmpty()) {
checkBox->setIcon(QIcon::fromTheme(iconName));
}
checkBox->setChecked(model->data(index, Qt::CheckStateRole).toBool());
checkBox->setChecked(model->data(index, Qt::CheckStateRole).value<Qt::CheckState>() == Qt::Checked);
const bool configurable = model->data(index, ServiceModel::ConfigurableRole).toBool();
@ -98,7 +98,7 @@ void ServiceItemDelegate::updateItemWidgets(const QList<QWidget *> &widgets, con
void ServiceItemDelegate::slotCheckBoxClicked(bool checked)
{
QAbstractItemModel *model = const_cast<QAbstractItemModel *>(focusedIndex().model());
model->setData(focusedIndex(), checked, Qt::CheckStateRole);
model->setData(focusedIndex(), checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
}
void ServiceItemDelegate::slotConfigureButtonClicked()

View file

@ -29,7 +29,7 @@ bool ServiceModel::insertRows(int row, int count, const QModelIndex &parent)
beginInsertRows(parent, row, row + count - 1);
for (int i = 0; i < count; ++i) {
ServiceItem item;
item.checked = false;
item.checked = Qt::Unchecked;
item.configurable = false;
m_items.insert(row, item);
}
@ -47,7 +47,7 @@ bool ServiceModel::setData(const QModelIndex &index, const QVariant &value, int
switch (role) {
case Qt::CheckStateRole:
m_items[row].checked = value.toBool();
m_items[row].checked = value.value<Qt::CheckState>();
break;
case ConfigurableRole:
m_items[row].configurable = value.toBool();
@ -105,4 +105,9 @@ void ServiceModel::clear()
endRemoveRows();
}
Qt::ItemFlags ServiceModel::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index) | Qt::ItemIsUserCheckable;
}
#include "moc_servicemodel.cpp"

View file

@ -35,10 +35,11 @@ public:
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
void clear();
Qt::ItemFlags flags(const QModelIndex &index) const override;
private:
struct ServiceItem {
bool checked;
Qt::CheckState checked;
bool configurable;
QString icon;
QString text;