naturalpsychic
22nd May 2011, 16:44
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:
#include "listviewdelegate.h"
#include "mainwindow.h"
ListViewDelegate::ListViewDelegate(MainWindow *parent):QItemDelegate(parent) {}
void ListViewDelegate::paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
MainWindow* mw=static_cast<MainWindow*>(parent());
if (index.column() == 7)
{
createEditor(mw,option,index);
//this prints at wrong place
return;
}
else if (index.column()==4) //progress
{
QStyleOptionProgressBarV2 progressBarOption;
progressBarOption.state=QStyle::State_Enabled;
progressBarOption.direction = QApplication::layoutDirection();
progressBarOption.rect = option.rect;
progressBarOption.fontMetrics = QApplication::fontMetrics();
progressBarOption.minimum = 0;
try{
progressBarOption.maximum=mw->totalSize[index.row()];
}
catch(...){
}
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
if (index.row() == mw->currIdx){
int progress=mw->progress;
try{
progressBarOption.progress = progress < 0 ? 0 : progress;
}
catch(...){
}
progressBarOption.text = CMFile::getShortValue(progressBarOption.progress)+" / " +CMFile::getShortValue(progressBarOption.maximum);
}
else
{
progressBarOption.text=(index.row() < mw->currIdx)?"Completed":"Queued";
}
QApplication::style()->drawControl(QStyle::CE_ProgressBar,&progressBarOption,painter);
return;
}
QItemDelegate::paint(painter,option,index);
return;
}
QWidget* ListViewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *cmbBox=new QComboBox(parent);
cmbBox->insertItem(0,"YES");
cmbBox->insertItem(1,"NO");
cmbBox->setVisible(true);
return cmbBox;
}
void ListViewDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::EditRole).toInt();
QComboBox *cmbBox = static_cast<QComboBox*>(editor);
cmbBox->setCurrentIndex(value);
}
void ListViewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *cmbBox = static_cast<QComboBox*>(editor);
int value = cmbBox->itemText(0).toInt();
model->setData(index, value, Qt::EditRole);
}
void ListViewDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
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
#include "listviewdelegate.h"
#include "mainwindow.h"
ListViewDelegate::ListViewDelegate(MainWindow *parent):QItemDelegate(parent) {}
void ListViewDelegate::paint(QPainter *painter,const QStyleOptionViewItem &option,const QModelIndex &index) const
{
MainWindow* mw=static_cast<MainWindow*>(parent());
if (index.column() == 7)
{
createEditor(mw,option,index);
//this prints at wrong place
return;
}
else if (index.column()==4) //progress
{
QStyleOptionProgressBarV2 progressBarOption;
progressBarOption.state=QStyle::State_Enabled;
progressBarOption.direction = QApplication::layoutDirection();
progressBarOption.rect = option.rect;
progressBarOption.fontMetrics = QApplication::fontMetrics();
progressBarOption.minimum = 0;
try{
progressBarOption.maximum=mw->totalSize[index.row()];
}
catch(...){
}
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
if (index.row() == mw->currIdx){
int progress=mw->progress;
try{
progressBarOption.progress = progress < 0 ? 0 : progress;
}
catch(...){
}
progressBarOption.text = CMFile::getShortValue(progressBarOption.progress)+" / " +CMFile::getShortValue(progressBarOption.maximum);
}
else
{
progressBarOption.text=(index.row() < mw->currIdx)?"Completed":"Queued";
}
QApplication::style()->drawControl(QStyle::CE_ProgressBar,&progressBarOption,painter);
return;
}
QItemDelegate::paint(painter,option,index);
return;
}
QWidget* ListViewDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QComboBox *cmbBox=new QComboBox(parent);
cmbBox->insertItem(0,"YES");
cmbBox->insertItem(1,"NO");
cmbBox->setVisible(true);
return cmbBox;
}
void ListViewDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::EditRole).toInt();
QComboBox *cmbBox = static_cast<QComboBox*>(editor);
cmbBox->setCurrentIndex(value);
}
void ListViewDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *cmbBox = static_cast<QComboBox*>(editor);
int value = cmbBox->itemText(0).toInt();
model->setData(index, value, Qt::EditRole);
}
void ListViewDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
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