Important: Please read the Qt Code of Conduct -

QItemDelegate for drawing progress bar working, but won't move off origin

  • I'm trying to display a QStyleOptionProgressBar in a cell in a QTreeWidget, like the tutorial found here for the Qt torrent client. The progress bar renders, but it is stuck on the very top left of my QTreeWidget item. My guess is that there is no margin for my QTreeWidget so the drawControl function has nothing to offset the coordinates from and so just draws on the origin, but I have no idea what steps to take to remedy this.

    Code below:

    // HEADER FILE (joblist.hpp)
    class JobInterfaceDelegate : public QItemDelegate {
        inline explicit JobInterfaceDelegate(QWidget* parent=nullptr) : QItemDelegate(parent) {}
        // Render the progress bar
        void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const override;
    // Interface on which jobs are displayed
    class JobInterface : public QTreeWidget {
        explicit JobInterface(QWidget* parent=nullptr);
    // SOURCE FILE (joblist.cpp)
    // Delegate render method for painting progress bars
    void JobInterfaceDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
                                     const QModelIndex &index) const {
        // Only work for fourth column
        if (index.column() != 3) {
            QItemDelegate::paint(painter, option, index);
        // Set up a QStyleOptionProgressBar to mimic a progress bar
        QStyleOptionProgressBar progressBarOption;
        progressBarOption.state = QStyle::State_Enabled;
        progressBarOption.direction = QApplication::layoutDirection();
        progressBarOption.rect = option.rect;
        progressBarOption.fontMetrics = QApplication::fontMetrics();
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.textAlignment = Qt::AlignCenter;
        progressBarOption.textVisible = true;
        // Set the progress and text values of the bar
        // TODO: Reference the job at the column and its progress
        int progress = 25;
        progressBarOption.progress = progress < 0 ? 0 : progress;
        progressBarOption.text = QString::asprintf("%d%%", progressBarOption.progress);
    //    progressBarOption.rect = QRect(150, 150, 100, 100);  <- this appears no different from the line below
    //    progressBarOption.rect = QRect(100, 100, 100, 100);
        // Draw the progress bar onto the view (THIS IS BROKEN)
        QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
    //    painter->fillRect(option.rect, option.palette.highlight()); <-THIS WORKS
    // Build an empty job list (tree widget)
    JobInterface::JobInterface(QWidget *parent) :
            QTreeWidget(parent) {
        // Build static strings for each column header
        QStringList headers;
        headers << tr("Direction") << tr("Job") << tr("Status") << tr("Progress") << tr("Size")
                << tr("Files") << tr("Transfer Rate") << tr("Elapsed Time");
        // Set all initialization settings
        setItemDelegate(new JobInterfaceDelegate(this));
        // Size hints for columns
        QFontMetrics fm = parent->fontMetrics();
        QHeaderView *hdr = header();
        hdr->resizeSection(0, fm.horizontalAdvance("Download      "));
        hdr->setSectionResizeMode(1, QHeaderView::ResizeMode::Stretch);
        hdr->resizeSection(2, fm.horizontalAdvance("Completed      "));
        hdr->resizeSection(3, qMax(fm.horizontalAdvance( + " "), fm.horizontalAdvance(" 1234.0 KB/s ")));
        hdr->resizeSection(4, fm.horizontalAdvance("100.0 MB / 100.0 MB"));
        hdr->resizeSection(5, fm.horizontalAdvance("100 / 100"));
        hdr->resizeSection(6, qMax(fm.horizontalAdvance( + " "), fm.horizontalAdvance(" 1234.0 KB/s ")));
        hdr->resizeSection(7, fm.horizontalAdvance( + " "));

    The outcome looks like this, where 3 progress bars are stacked on top of each other on the origin (one created for each row):


    I want them to be located in the progress bar, each on its own row (obviously).

    All help is appreciated!

  • Lifetime Qt Champion


    Can you check again using QStyledItemDelegate as base class of your delegate ?

  • @sgaist Yes, same issue unfortunately. :(

  • Lifetime Qt Champion

    Check the option rect value that you get. If the coordinates don't change, one thing you can do is to update the top left corner based on the row and column number.

  • Yup, that does it, although kind of a hack. Added painter->translate(option.rect.left(), before the drawControl and painter->resetTransform() after it, for anyone who may have this problem in the future. Also made sure to change the rect of the progress bar so its coordinates are (0, 0) just in case the issue isn't consistent across platforms.

    Thank you!!!

  • Lifetime Qt Champion

    Another option you have is to save the state of the painter before you modify it and then restore it.

    That way you don't complete reset the transformation if any other was applied before your translation.

Log in to reply