nilhcraiv
13th March 2013, 16:22
Hello,
I want to show in QTreeView a lot of QProgressBars as child of each item to indicate a processes, i.e. each item show a text in treeView, and they have QProgressBars as childs. I try it with QItemDelegate, but don't know how I individualy update each QProgressBar. I do it as follows: (I found this code searching on Internet)
CustomItemDelegate::CustomItemDelegate(QObject *parent) :
QItemDelegate(parent)
{
_state = QStyle::State_Enabled;
}
void CustomItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.parent().isValid())
{
QStyleOptionProgressBarV2 progressBarOption;
QRect rect = option.rect;
QSize size(rect.width()*3/4,rect.height()*3/4);
rect.setSize(size);
progressBarOption.state = QStyle::State_Enabled;
progressBarOption.direction = QApplication::layoutDirection();
progressBarOption.rect =rect;
progressBarOption.fontMetrics = QApplication::fontMetrics();
QPalette pal = progressBarOption.palette;
QColor col;
col.setNamedColor("#05B8CC");
pal.setColor(QPalette::Highlight,col);
progressBarOption.palette = pal;
progressBarOption.type = QStyleOption::SO_ProgressBar;
progressBarOption.version = 2 ;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
int progress = index.data(Qt::DisplayRole).toInt();//TCP client or server must changes this value emitting signal bytesWritten(qint64)
progressBarOption.progress = progress;
progressBarOption.text = QString("%1%").arg(progressBarOption.progress);
// Draw the progress bar onto the view.
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter,0);
}
else
{
QItemDelegate::paint(painter, option, index);
}
}
...
...
and in QTreeView
CustomItemDelegate* itemDelegate = new CustomItemDelegate(this);
setItemDelegate( itemDelegate );//To set progressbar in childs of each item.
setModel( "a QAbstractItemModel to build the hierarchy of item");
In this code the progress of QProgressBar is set by text on QModelIndex index. How I can change only a progress in one ProgressBar? My application creates a TCP connection to send a file and I wish show the progress in each progressbar but I don't want update all QAbstractItemModel by setModel()
I have searched a lot, but have not found the solution!
I want to show in QTreeView a lot of QProgressBars as child of each item to indicate a processes, i.e. each item show a text in treeView, and they have QProgressBars as childs. I try it with QItemDelegate, but don't know how I individualy update each QProgressBar. I do it as follows: (I found this code searching on Internet)
CustomItemDelegate::CustomItemDelegate(QObject *parent) :
QItemDelegate(parent)
{
_state = QStyle::State_Enabled;
}
void CustomItemDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if(index.parent().isValid())
{
QStyleOptionProgressBarV2 progressBarOption;
QRect rect = option.rect;
QSize size(rect.width()*3/4,rect.height()*3/4);
rect.setSize(size);
progressBarOption.state = QStyle::State_Enabled;
progressBarOption.direction = QApplication::layoutDirection();
progressBarOption.rect =rect;
progressBarOption.fontMetrics = QApplication::fontMetrics();
QPalette pal = progressBarOption.palette;
QColor col;
col.setNamedColor("#05B8CC");
pal.setColor(QPalette::Highlight,col);
progressBarOption.palette = pal;
progressBarOption.type = QStyleOption::SO_ProgressBar;
progressBarOption.version = 2 ;
progressBarOption.minimum = 0;
progressBarOption.maximum = 100;
progressBarOption.textAlignment = Qt::AlignCenter;
progressBarOption.textVisible = true;
int progress = index.data(Qt::DisplayRole).toInt();//TCP client or server must changes this value emitting signal bytesWritten(qint64)
progressBarOption.progress = progress;
progressBarOption.text = QString("%1%").arg(progressBarOption.progress);
// Draw the progress bar onto the view.
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter,0);
}
else
{
QItemDelegate::paint(painter, option, index);
}
}
...
...
and in QTreeView
CustomItemDelegate* itemDelegate = new CustomItemDelegate(this);
setItemDelegate( itemDelegate );//To set progressbar in childs of each item.
setModel( "a QAbstractItemModel to build the hierarchy of item");
In this code the progress of QProgressBar is set by text on QModelIndex index. How I can change only a progress in one ProgressBar? My application creates a TCP connection to send a file and I wish show the progress in each progressbar but I don't want update all QAbstractItemModel by setModel()
I have searched a lot, but have not found the solution!