model = new QSqlQueryModel(); model->setQuery("SELECT a.id, a.item_id, i.item_type AS item_type FROM actions a " "INNER JOIN items i ON i.id = a.item_id "); MyDelegate *delegate = new MyDelegate(ui->actionsTV); ui->actionsTV->setModel(model); ui->actionsTV->setItemDelegateForColumn(2,delegate); ui->actionsTV->resizeColumnsToContents();
void AnswerDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ int id = index.data(Qt::DisplayRole).toInt(); if (option.state & QStyle::State_Selected) painter->fillRect(option.rect, option.palette.highlight()); if (option.state & QStyle::State_Selected) painter->setBrush(option.palette.highlightedText()); else painter->setBrush(QBrush(colors.at(id))); painter->fillRect(option.rect, painter->brush());}
model = new QSqlTableModel(); model->setTable("actions"); model->setSort(model->fieldIndex("id"),Qt::AscendingOrder); model->select(); int row(0); QSqlQuery q; q.exec(QString("SELECT i.item_type FROM items i " "INNER JOIN actions a ON i.id = a.item_id ORDER BY a.id")); while(q.next()) { int item_type = q.value(0).toInt(); qDebug() << model->setData(model->index(row++,1),QBrush(colors.at(item_type)), Qt::BackgroundRole); qDebug() << model->lastError() << row << colors.at(item_type); }
C++ (Qt)qDebug() << model->rowCount() << model->columnCount()
26 6 QModelIndex(0,4,0x0,QSqlTableModel(0x11798d0) ) QModelIndex(1,4,0x0,QSqlTableModel(0x11798d0) ) QModelIndex(2,4,0x0,QSqlTableModel(0x11798d0) ) ...
qDebug() << model->setData(model->index(0,1), 123, Qt::EditRole);
qDebug() << model->setData(model->index(0,1),QBrush(Qt::green, Qt::SolidPattern), Qt::BackgroundRole);