I have a TreeWidget and i have managed to paint progress bar on one of column in QTreeWidget but i cannot get my head around painting combo box in (a different) column. Following is my QItemDelegate derived class:
Qt Code:
  1. #include "listviewdelegate.h"
  2. #include "mainwindow.h"
  3.  
  4.  
  5.  
  6. ListViewDelegate::ListViewDelegate(MainWindow *parent):QItemDelegate(parent) {}
  7.  
  8.  
  9.  
  10. void ListViewDelegate::paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index) const
  11. {
  12.  
  13. MainWindow* mw=static_cast<MainWindow*>(parent());
  14.  
  15. if (index.column() == 7)
  16. {
  17.  
  18. createEditor(mw,option,index);
  19. //this prints at wrong place
  20.  
  21. return;
  22. }
  23. else if (index.column()==4) //progress
  24. {
  25. QStyleOptionProgressBarV2 progressBarOption;
  26. progressBarOption.state=QStyle::State_Enabled;
  27. progressBarOption.direction = QApplication::layoutDirection();
  28. progressBarOption.rect = option.rect;
  29. progressBarOption.fontMetrics = QApplication::fontMetrics();
  30. progressBarOption.minimum = 0;
  31.  
  32. try{
  33. progressBarOption.maximum=mw->totalSize[index.row()];
  34. }
  35. catch(...){
  36. }
  37.  
  38. progressBarOption.textAlignment = Qt::AlignCenter;
  39. progressBarOption.textVisible = true;
  40. if (index.row() == mw->currIdx){
  41.  
  42. int progress=mw->progress;
  43.  
  44. try{
  45. progressBarOption.progress = progress < 0 ? 0 : progress;
  46. }
  47. catch(...){
  48. }
  49.  
  50. progressBarOption.text = CMFile::getShortValue(progressBarOption.progress)+" / " +CMFile::getShortValue(progressBarOption.maximum);
  51. }
  52. else
  53. {
  54. progressBarOption.text=(index.row() < mw->currIdx)?"Completed":"Queued";
  55.  
  56. }
  57. QApplication::style()->drawControl(QStyle::CE_ProgressBar,&progressBarOption,painter);
  58. return;
  59.  
  60. }
  61.  
  62.  
  63.  
  64. QItemDelegate::paint(painter,option,index);
  65.  
  66. return;
  67.  
  68.  
  69.  
  70.  
  71.  
  72. }
  73.  
  74. QWidget* ListViewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  75. {
  76. QComboBox *cmbBox=new QComboBox(parent);
  77. cmbBox->insertItem(0,"YES");
  78. cmbBox->insertItem(1,"NO");
  79.  
  80.  
  81. cmbBox->setVisible(true);
  82.  
  83. return cmbBox;
  84. }
  85. void ListViewDelegate::setEditorData(QWidget *editor,
  86. const QModelIndex &index) const
  87. {
  88. int value = index.model()->data(index, Qt::EditRole).toInt();
  89.  
  90. QComboBox *cmbBox = static_cast<QComboBox*>(editor);
  91. cmbBox->setCurrentIndex(value);
  92. }
  93.  
  94. void ListViewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
  95. const QModelIndex &index) const
  96. {
  97. QComboBox *cmbBox = static_cast<QComboBox*>(editor);
  98.  
  99. int value = cmbBox->itemText(0).toInt();
  100.  
  101. model->setData(index, value, Qt::EditRole);
  102. }
  103.  
  104. void ListViewDelegate::updateEditorGeometry(QWidget *editor,
  105. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
  106. {
  107. editor->setGeometry(option.rect);
  108. }
To copy to clipboard, switch view to plain text mode 


It does paint the combobox but it does it at 0,0 location on form. when i have set geometry to option's rect object already.

any idea? thanks