0

I have custom model inherited from QAbstractItemModel and custom view inherited from QAbstractItemView. Model is a wrap on data organized as a tree. When model is changed it emits neccessary signals to notify view about changes. View has default item delegate.

And now I want to craete a custom widget for every item in a view and set it with QAbstractItemView::setIndexWidget(). How can I catch and handle every item creation in the view to make that?

GLaz
  • 295
  • 1
  • 11
  • What about `QAbstractItemModel::rowsInserted()` signal? – vahancho Jul 09 '14 at 10:59
  • @vahancho There are at least two signals which can be emitter after change a model: rowsInserted and layoutChanged. I can listen both of them, and in first case create custom widget for specific rows and in second case create all of them. But is this a good way or there is a better one? Also I need to get that signals after my base view class. If I connect to model after base class, will I get these signals assured after base class? – GLaz Jul 09 '14 at 11:10
  • 1
    you are better off using a custom [itemdelegate](http://qt-project.org/doc/qt-5/qabstractitemview.html#itemDelegate-2)? which will only ask the editors as needed – ratchet freak Jul 09 '14 at 11:50
  • @ratchetfreak I don't use editors at all. I want to use widgets to display items. – GLaz Jul 09 '14 at 11:55
  • then use its [paint](http://qt-project.org/doc/qt-5/qabstractitemdelegate.html#paint) function to "rubberstamp" it using your custom widget's `render` function – ratchet freak Jul 09 '14 at 12:05

1 Answers1

0

you are better off using an itemdelegate.

class MyItemDelegate: public QAbstractItemDelegate
{
Q_OBJECT

  QWidget *widget;

public:
  MyItemDelegate(QObject *p):QAbstractItemDelegate(p)
  {
    //create widget
  }


  void paint(QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index) const
  {
      //initialize painting widget
      widget->render(painter);
  }

}
ratchet freak
  • 47,288
  • 5
  • 68
  • 106