QProgressBar inside QTableView column (Size problem)
-
Hi `~ I Have A Size problem on progressbar inside QTableView (Table is from Right To left)
the progressbar takes the size of it's column 1 and other column 0 which is not a linked with it- it contains headers TEXT.. i just clean it with paint
- when i click to column 0 (from the right to left) it remove the progressbar and when i click something else it comes back as the picture
here's my code
expiredatesDelegate::expiredatesDelegate(QObject *parent) : QStyledItemDelegate{parent} { } void expiredatesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.column() == 1) { int progress = index.data().toInt(); QStyleOptionProgressBar progressBarOption; progressBarOption.rect = option.rect; progressBarOption.state = QStyle::StateFlag::State_Horizontal; progressBarOption.minimum = 0; progressBarOption.maximum = 100; progressBarOption.progress = progress; progressBarOption.text = QString::number(progress) + "%"; progressBarOption.textVisible = true; progressBarOption.textAlignment =Qt::AlignCenter; painter->setFont(QFont("Arial",10)); QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); } else QStyledItemDelegate::paint(painter, option, index); }
ui->tableView->setModel(expiredatesmodel); ui->tableView->setColumnWidth(0,260); ui->tableView->setColumnWidth(1,260); ui->tableView->setColumnWidth(2,170); ui->tableView->setColumnWidth(3,170); /****/* ui->tableView->model()->setData(index,productname[i],1); ui->tableView->setItemDelegate(new expiredatesDelegate());
-
Try this:
- instead of
ui->tableView->setItemDelegate(new expiredatesDelegate());
callui->tableView->setItemDelegateForColumn(1,new expiredatesDelegate());
- change
paint
to:
void expiredatesDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyleOptionViewItem opt = option; initStyleOption(&opt, index); const int progress = index.data().toInt(); QStyleOptionProgressBar progressBarOption; progressBarOption.rect = opt.rect; progressBarOption.direction = opt.direction; progressBarOption.fontMetrics= opt.fontMetrics; progressBarOption.palette= opt.palette; progressBarOption.state = opt.state | QStyle::State_Horizontal; progressBarOption.styleObject = opt.styleObject; progressBarOption.minimum = 0; progressBarOption.maximum = 100; progressBarOption.progress = progress; progressBarOption.text = tr("%1%").arg(progress); progressBarOption.textVisible = true; progressBarOption.textAlignment =Qt::AlignCenter; painter->save(); painter->setFont(QFont("Arial",10)); (opt.widget ? opt.widget.style : QApplication::style())->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); painter->restore(); }
- instead of
-
@VRonin said in QProgressBar inside QTableView column (Size problem):
widget
Hi Thank you so much bro
i tried it but almost the same
only it changes it's directionusing ui->tableView->setItemDelegateForColumn(1,new expiredatesDelegate());
it looks like this
using
ui->tableView->setItemDelegate(new expiredatesDelegate());
but it got it's right size but i lost other columns ^^ i am sure there's a very close solution now ~~ Where it is ^^? -
@VRonin
you knew what bro :D ^^ when the model gets data there's a values higher than the max of the progressbar that's why it takes the size of other columns ... when i set higher progressBarOption.maximum = 100;
problem solved
Thank you so much
without you i coudn't find it even if it was that easy