QSqlQueryModel and QTableView, with a custom ItemDelegate
I have http://doc.qt.io/qt-4.8/QTableView connected with http://doc.qt.io/qt-4.8/QSqlQueryModel, the first column I inserted it after populating data from database. The first column will be checkbox, checked is the second column have a matched value from a QStringList variable I pass to the delegate. My problem is the delegate never draws http://doc.qt.io/qt-4.8/QCheckBox on the first column.
Here is how I set the delegate:
Code:
sqlQuery = "SELECT ProCode, ProName, ProPrice FROM Promotions";
model->setQuery(sqlQuery);
model->insertColumns(0, 1);
ui->promotionsList->setModel(model);
QStringList promotions
= this
->workerMod
->promotionsList
->text
().
split(',');
PromotionDelegate *checkDelegatePromos = new PromotionDelegate(this);
checkDelegatePromos->setPromotions(promotions);
ui->promotionsList->setItemDelegate(checkDelegatePromos);
The delegate I implement:
Code:
{
if(index.isValid() && index.column() == 0)
{
//QMessageBox::critical(0, QString::number(index.row()), "1"); Also, this message never fired up!
check->setCheckState(Qt::Unchecked);
return check;
}
else
}
{
if(index.isValid() && index.column() == 0)
{
QCheckBox *check
= static_cast<QCheckBox
*>
(editor
);
QModelIndex inx
= index.
model()->index
(index.
row(),
1);
QString prom
= index.
model()->data
(inx,
0).
toString();
for(int i = 0; i< this->m_promotions.length(); ++i)
{
if(this->m_promotions.at(i) == prom)
{
check->setCheckState(Qt::Checked);
break;
}
}
}
else
}
{
}
{
if(index.isValid() && index.column() == 0)
{
editor->setGeometry(option.rect);
}
else
}
void PromotionDelegate
::setPromotions(QStringList &promotions
) {
this->m_promotions = promotions;
}