4

I want-to put-a customed-widget into the cell of QTableView. The Widget can-be a QPushButton, a checkBox or something else. I've tried following methods, but no-one satisfies me:

  1. Use delegate. This strategy can paint widget, but cant-be a customed widget, and cant interact.
  2. Use QTableView::setIndexWidget(). Using this strategy, the customed-widget covers the cell wholly & absolutely, delegate binded to QTableView not working, which means a-double-click() wont make Edit operation on-the cell.

Note that the function is stand-alone, like a plugin. So I cant do following things.

  1. Inherit from QTableView. I can only get a-pointer-variable binded with QTableView----only an object of QTableView.
  2. Inherit from any-model. The model is control by other user, the programmer writing model shouldn't and can't just use my-customed model.

The problem is really complex and too-many constraints restrict the design.

trivelt
  • 1,913
  • 3
  • 22
  • 44
Nick.Rhan
  • 167
  • 2
  • 7
  • I think you will have to go the delegate way. You can use custom widgets and you can interact with them. See: https://stackoverflow.com/questions/16660292/qt-using-custom-qitemdelegate-for-qtableview – Taron Aug 04 '17 at 06:51
  • the QItemDelegate::createEditor() just paints when the cell is-clicked. The need is that the widget can be shown previously, If I click the cell, another delegate also can work.But QItemDelegate::paint() only draw, not gives a real Widget that can-be interact – Nick.Rhan Aug 04 '17 at 07:35
  • tableView->openPersistentEditor( index ). With that its always visible! – Taron Aug 04 '17 at 08:22
  • still dosent work – Nick.Rhan Aug 04 '17 at 09:48

2 Answers2

2

You can't do this without a delegate if you want to use model/view to contain the data of your widget. Even worse, if you try to do this anyway, you'll significantly slow down your program when the number of elements is very high, because every widget will be involved in the event loop. This is NOT what you want. You want to involve only the widgets that are visible to the user.

Considering reading this for more explanation.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
1

Thanks for all of you contributing to this question, I've found a way to solve this problem. It is solved as the "original thought", which is just to draw a QToolButton on the tableView, using the QModelIndex. Through QModelIndex, I can get the geomertry location where I can draw any QWidget. However, this method is really complex, for I have to maintain the changes of QModelIndex, like removeColumns and insertColumns.

Another problem is concerned that I have to distinguish between hide & delete columns or rows. And I think I should post an another new question for help. Thanks again.

Nick.Rhan
  • 167
  • 2
  • 7