Hi,
I have table in Sqlite database which I display using QTableview and QSqlQueryModel. The first column needs to have a header which is a checkbox and all the items in the column needs to be checkbox too. Since the checkbox needs to be centered, I need to use a delegate to paint it.
I have painted checkbox using the following code. This creates checkboxes, but they cannot be checked or unchecked.
&check_box_style_option);
QPoint check_box_point
(view_item_style_options.
rect.
x() + view_item_style_options.rect.width() / 2 -
check_box_rect.width() / 2,
view_item_style_options.rect.y() +
view_item_style_options.rect.height() / 2 -
check_box_rect.height() / 2);
return QRect(check_box_point, check_box_rect.
size());
}
CheckBoxDelegate
::CheckBoxDelegate(QObject *parent
) : QStyledItemDelegate(parent)
{
}
void CheckBoxDelegate
::paint(QPainter *painter,
bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
check_box_style_option.
state |
= QStyle::State_Enabled;
if (checked) {
check_box_style_option.
state |
= QStyle::State_On;
} else {
check_box_style_option.
state |
= QStyle::State_Off;
}
check_box_style_option.rect = CheckBoxRect(option);
&check_box_style_option,
painter);
}
static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) {
QStyleOptionButton check_box_style_option;
QRect check_box_rect = QApplication::style()->subElementRect(
QStyle::SE_CheckBoxIndicator,
&check_box_style_option);
QPoint check_box_point(view_item_style_options.rect.x() +
view_item_style_options.rect.width() / 2 -
check_box_rect.width() / 2,
view_item_style_options.rect.y() +
view_item_style_options.rect.height() / 2 -
check_box_rect.height() / 2);
return QRect(check_box_point, check_box_rect.size());
}
CheckBoxDelegate::CheckBoxDelegate(QObject *parent) :
QStyledItemDelegate(parent)
{
}
void CheckBoxDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
QStyleOptionButton check_box_style_option;
check_box_style_option.state |= QStyle::State_Enabled;
if (checked) {
check_box_style_option.state |= QStyle::State_On;
} else {
check_box_style_option.state |= QStyle::State_Off;
}
check_box_style_option.rect = CheckBoxRect(option);
QApplication::style()->drawControl(QStyle::CE_CheckBox,
&check_box_style_option,
painter);
}
To copy to clipboard, switch view to plain text mode
I still do not have a clear idea how to go about it. I read somewhere that I need to re-implement data(), setdata() and flags() of QSqlQueryModel. The following code shows how I use the QSqlQueryModel for QTableView. Could you please tell me how to go about it? Any help is appreciated.
//Load the tableview with the database table
//Initializaton of the query
query->prepare("SELECT * FROM UserData");
if(query->exec())
{
model->setQuery(*query);
ui->tableView->setModel(model);
//The header delegate to paint a checkbox on the header
HeaderDelegate *myHeader = new HeaderDelegate(Qt::Horizontal, ui->tableView);
ui->tableView->setHorizontalHeader(myHeader);
int RowCount = model->rowCount();
qDebug() << RowCount;
CheckBoxDelegate *myCheckBoxDelegate = new CheckBoxDelegate();
for(int i = 0; i <model->rowCount(); i++)
{
ui->tableView->setItemDelegateForColumn(0,myCheckBoxDelegate);
}
ui->tableView->horizontalHeader()->setClickable(true);
ui->tableView->setSortingEnabled(true);
}
//Load the tableview with the database table
QSqlQueryModel model = new QSqlQueryModel();
//Initializaton of the query
QSqlQuery *query = new QSqlQuery(dbm->db);
query->prepare("SELECT * FROM UserData");
if(query->exec())
{
model->setQuery(*query);
ui->tableView->setModel(model);
//The header delegate to paint a checkbox on the header
HeaderDelegate *myHeader = new HeaderDelegate(Qt::Horizontal, ui->tableView);
ui->tableView->setHorizontalHeader(myHeader);
int RowCount = model->rowCount();
qDebug() << RowCount;
CheckBoxDelegate *myCheckBoxDelegate = new CheckBoxDelegate();
for(int i = 0; i <model->rowCount(); i++)
{
ui->tableView->setItemDelegateForColumn(0,myCheckBoxDelegate);
}
ui->tableView->horizontalHeader()->setClickable(true);
ui->tableView->setSortingEnabled(true);
}
To copy to clipboard, switch view to plain text mode
Bookmarks