Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QItemDelegate for drawing progress bar working, but won't move off origin
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 915 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    evankirkiles
    wrote on last edited by evankirkiles
    #1

    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 {
        Q_OBJECT
    public:
        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 {
        Q_OBJECT
    public:
        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);
            return;
        }
    
        // 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));
        setHeaderLabels(headers);
        setSelectionBehavior(QAbstractItemView::SelectRows);
        setAlternatingRowColors(true);
        setRootIsDecorated(false);
        
        // Size hints for columns
        QFontMetrics fm = parent->fontMetrics();
        QHeaderView *hdr = header();
        hdr->setStretchLastSection(false);
        hdr->resizeSection(0, fm.horizontalAdvance("Download      "));
        hdr->setSectionResizeMode(1, QHeaderView::ResizeMode::Stretch);
        hdr->resizeSection(2, fm.horizontalAdvance("Completed      "));
        hdr->resizeSection(3, qMax(fm.horizontalAdvance(headers.at(3) + " "), 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(headers.at(3) + " "), fm.horizontalAdvance(" 1234.0 KB/s ")));
        hdr->resizeSection(7, fm.horizontalAdvance(headers.at(7) + " "));
    }
    

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

    Screenshot

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

    All help is appreciated!
    Evan

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      E 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

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

        E Offline
        E Offline
        evankirkiles
        wrote on last edited by
        #3

        @sgaist Yes, same issue unfortunately. :(

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • E Offline
            E Offline
            evankirkiles
            wrote on last edited by
            #5

            Yup, that does it, although kind of a hack. Added painter->translate(option.rect.left(), option.rect.top()) 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!!!

            1 Reply Last reply
            1
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved