When 2 views share the same model, how can the views get different values for some of the Qt::ItemDataRole roles?

The model does not have any information about the views, so your item delegate will have to handle this. If you want one of your views to have icons and not the other one, then you can reimplement QItemDelegate::drawDecoration() [qt.nokia.com] and set the delegate on that view, e.g:

  1. #include <QtGui>
  2.  
  3. class ItemDelegate : public QItemDelegate
  4. {
  5. public:
  6.     ItemDelegate() : QItemDelegate()
  7.     {
  8.     }
  9.  
  10.     void drawDecoration(QPainter *painter, const QStyleOptionViewItem &option,
  11.                         const QRect &rect, const QPixmap &pixmap) const
  12.     {
  13.         QPixmap pix(22, 22);
  14.         pix.fill(Qt::red);
  15.         QItemDelegate::drawDecoration(painter, option, option.rect, pix);
  16.     }
  17. };
  18.  
  19. int main(int argc, char **argv)
  20. {
  21.     QApplication app(argc, argv);
  22.     QWidget widget;
  23.     QHBoxLayout *layout = new QHBoxLayout(&widget);
  24.     QTableView *tableView = new QTableView(&widget);
  25.     tableView->setItemDelegate(new ItemDelegate());
  26.     QTableView *secondtableView = new QTableView(&widget);
  27.     QStandardItemModel *model = new QStandardItemModel;
  28.     model->insertRows(0, 4);
  29.     model->insertColumns(0, 4);
  30.     for (int row = 0; row < 4; row++) {
  31.         for (int col = 0; col < 4; col++) {
  32.             QModelIndex index = model->index(row, col);
  33.             model->setData(index, QVariant((row+1) * (col+1)).toString());
  34.         }
  35.     }
  36.     secondtableView->setModel(model);
  37.     tableView->setModel(model);
  38.  
  39.     layout->addWidget(tableView);
  40.     layout->addWidget(secondtableView);
  41.  
  42.     widget.show();
  43.     return app.exec();
  44. }

Alternatively, you can reimplement QItemDelegate::paint() [qt.nokia.com] and draw the pixmap and the other details there.

No comments

Write a comment

Sorry, you must be logged in to post a comment.