Hello, apologies for my English, I speak French, C++, then English.

I begin with Qt, currently using multiple views of a tree model.
I tried to add widgets (buttons) in a filtered view but the buttons are disapearing when I add a row to the model.

In the doc, it is said to use a delegate for dynamic data. It seems to me that delegates are fit for custom paint or data editors. Am I wrong ? setIndexWidget looked quite adequate ...

Shall I use a delegate ? If yes, is there somewhere a delegate example for the equivalent of setIndexWidget? The paint method doesn't look appropriate, is it possible to paint a button in it ?
On the other side, shall I re-add the buttons in the rows each time I change the model ?
I tried to re-add a button, but it seams impossible to reuse a disappeared button. Is it destroyed ?

Here is an example code, thanks in advance you for your answers, Derf.

Qt Code:
  1. int main(int argc, char**argv)
  2. {
  3. //------ test proxy and setIndexWidget
  4. QApplication application(argc,argv);
  5. //-- model
  6. QModelIndex parent;
  7. for (int i=0; i<4; ++i) {
  8. parent = testModel.index(0,0,parent);
  9. testModel.insertRows(0,1,parent);
  10. testModel.insertColumns(0,1,parent);
  11. QModelIndex index = testModel.index(0,0,parent);
  12. testModel.setData(index,i);
  13. }
  14.  
  15. //-- tree and filtered tree
  16. QTreeView tree;
  17. tree.setModel(&testModel);
  18. tree.setWindowTitle("direct");
  19. tree.show();
  20.  
  21. filter.setSourceModel(&testModel);
  22.  
  23. QTreeView filteredTree;
  24. filteredTree.setModel(&filter);
  25. filteredTree.setWindowTitle("filtered");
  26. filteredTree.show();
  27.  
  28. //-- add buttons
  29. QModelIndex modelIndex = testModel.index(0,0,QModelIndex());
  30. QToolButton * button1 = new QToolButton(&tree);
  31. button1->setText("button1");
  32. tree.setIndexWidget(modelIndex,button1);
  33.  
  34. QModelIndex filterIndex = filter.mapFromSource(modelIndex);
  35. QToolButton * button2 = new QToolButton(&filteredTree);
  36. button2->setText("button2");
  37. filteredTree.setIndexWidget(filter.mapFromSource(modelIndex),button2);
  38.  
  39. //-- change model
  40. testModel.insertRows(0,1,QModelIndex());
  41. testModel.setData(testModel.index(0,0,QModelIndex()),42);
  42.  
  43. //-- add buttons
  44. modelIndex = testModel.index(0,0,QModelIndex());
  45. QToolButton * button3 =new QToolButton(&tree);
  46. button3->setText("button3");
  47. tree.setIndexWidget(modelIndex,button3);
  48.  
  49. filterIndex = filter.mapFromSource(modelIndex);
  50. QToolButton * button4 = new QToolButton(&filteredTree);
  51. button4->setText("button4");
  52. filteredTree.setIndexWidget(filterIndex,button4);
  53.  
  54. //-- add again button, normal ?
  55. //-- doesn't work with the same (button2) ? is it destroyed when the model changes ?
  56. //modelIndex = testModel.index(1,0,QModelIndex());
  57. //filterIndex = filter.mapFromSource(modelIndex);
  58. //QToolButton * button5 = new QToolButton(&filteredTree);
  59. //button5->setText("button5");
  60. //filteredTree.setIndexWidget(filter.mapFromSource(modelIndex),button5);
  61.  
  62. //-- run
  63. application.exec();
  64. return 0;
  65. }
To copy to clipboard, switch view to plain text mode