QCheckBox in a QTableView
-
Hello -
I've searched for a solution to my problem, and I've been finding that either they're rather complex (and hard to follow) or the result that they say indicate will happen doesn't happen for me, so I figured I'd put what I have out there and see if someone can help me understand why it's not working.
Here's what I'm trying to do: I'm using a QSqlRelationalTableModel with a QTableView to pull data from a SQLite db I created. One of the record fields is Boolean, so I'm trying to make a QCheckBox in that column to handle the boolean variable.
Output I'm getting: That column is displaying the checkbox on the left (not the center), and it has the either word "true" or "false" in text behind it. Clicking on the checkbox doesn't change the word behind, as (I believe) the model is set to update the SQL database after the user clicks accept.
Attempted Solution: The most common response on forums I saw was to make a delegate to pull in a QCheckBox. In order to center it, I'd also need to create a new QWidget with a QHBoxLayout to center the checkbox. Another solution I saw to center the checkbox was to create a new widget, which painted just the checkbox and didn't have a text portion like the QCheckBox does. However, I saw on https://bugreports.qt-project.org/browse/QTBUG-4924?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel that the SizeHints were fixed for when there's no text, so I figured it should center by just using a normal QCheckBox widget.
Here's the code I'm using (Hopefully the .cpp's are straight-forward enough, so I'll leave out the header files, unless someone feels they're needed):
BooleanWidget.cpp:
@
#include "booleanwidget.h"BooleanWidget::BooleanWidget(QWidget *parent) : QWidget(parent) {
QHBoxLayout *layout = new QHBoxLayout(this);
box = new QCheckBox(this);
layout->addWidget(box);
setLayout(layout);
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
}bool BooleanWidget::isChecked() { return box->isChecked(); }
void BooleanWidget::setChecked(bool checked) {
if(checked)
box->setCheckState(Qt::Checked);
else
box->setCheckState(Qt::Unchecked);
}
@BooleanDelegate:
@
#include "booleandelegate.h"BooleanDelegate::BooleanDelegate(QObject *parent) : QStyledItemDelegate(parent) {
}QWidget* BooleanDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
Q_UNUSED(option)
Q_UNUSED(index)
return new BooleanWidget(parent);
}void BooleanDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const {
BooleanWidget check = static_cast<BooleanWidget>(editor);
check->setChecked(index.model()->data(index, Qt::EditRole).toBool());
}void BooleanDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const {
BooleanWidget check = static_cast<BooleanWidget>(editor);
model->setData(index, check->isChecked(), Qt::EditRole);
}void BooleanDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const {
Q_UNUSED(index)
editor->setGeometry(option.rect);
}
@plugindialog.cpp (.ui file contains the QTableView):
@
#include "plugindialog.h"
#include "ui_plugindialog.h"PluginDialog::PluginDialog(QWidget *parent) : QDialog(parent), ui(new Ui::PluginDialog) {
ui->setupUi(this);
ui->pluginView->setModel(SQLModel::Instance()->getModel(SQLModel::Plugins));
ui->pluginView->setItemDelegateForColumn(2, new BooleanDelegate(parent));
for(int i=0; i<ui->pluginView->model()->rowCount(); i++)
ui->pluginView->openPersistentEditor(ui->pluginView->model()->index(i, 2));
ui->pluginView->hideColumn(0);
}PluginDialog::~PluginDialog() {
delete ui;
}void PluginDialog::onAccept() {
QSqlRelationalTableModel m = (QSqlRelationalTableModel)ui->pluginView->model();
m->submitAll();
close();
}void PluginDialog::onReject() {
close();
}
@Any ideas? Thanks!!