Results 1 to 8 of 8

Thread: QItemDelegate for painting QComboBox in QTreeWidget

  1. #1
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default QItemDelegate for painting QComboBox in QTreeWidget

    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
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QItemDelegate for painting QComboBox in QTreeWidget

    paint() is supposed to paint and not create widgets.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QItemDelegate for painting QComboBox in QTreeWidget

    yes that make sense but why is it not painting it on TreeWidget column rect, why is it painting it on 0,0 location?
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QItemDelegate for painting QComboBox in QTreeWidget

    Because you create a widget as a child of the main window so it is shown on there. What did you want to obtain by calling createEditor() there?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QItemDelegate for painting QComboBox in QTreeWidget

    i wanted to create combobox so user can select one of yes or no from that combo box for certain qtreewidgetitem
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  6. #6
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    44
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: QItemDelegate for painting QComboBox in QTreeWidget

    whats up guys? no good delegate programmer around the world?
    Life is like a dream, sometimes it is good and somtimes it is bad, but in the end it is over

  7. #7
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QItemDelegate for painting QComboBox in QTreeWidget

    You can try and take look at this http://doc.qt.nokia.com/4.7/itemview...xdelegate.html and replace the spinbox with a QComboBox.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QItemDelegate for painting QComboBox in QTreeWidget

    Quote Originally Posted by naturalpsychic View Post
    i wanted to create combobox so user can select one of yes or no from that combo box for certain qtreewidgetitem
    This doesn't require reimplementing paint(). If you want to mimic the looks of a combobox, do it the same way you mimic the looks of progress bar.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QComboBox in QTreeWidget
    By yes756 in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2009, 11:22
  2. QItemDelegate, QDataWidgetMapper and QComboBox
    By cydside in forum Qt Programming
    Replies: 7
    Last Post: 8th April 2009, 18:44
  3. Editable QComboBox with QItemDelegate
    By Jmgr in forum Qt Programming
    Replies: 11
    Last Post: 10th December 2008, 09:21
  4. QComboBox QitemDelegate size
    By aekilic in forum Qt Programming
    Replies: 1
    Last Post: 6th December 2008, 16:43
  5. QComboBox+QItemDelegate+QDateTimeEdit Problem
    By bangqianchen in forum Qt Programming
    Replies: 2
    Last Post: 15th September 2008, 10:14

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.