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. Problems with QSytyledItemDelegate and QStyleOptionProgressBar on macOS
Forum Updated to NodeBB v4.3 + New Features

Problems with QSytyledItemDelegate and QStyleOptionProgressBar on macOS

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 5 Posters 701 Views 2 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.
  • B Offline
    B Offline
    bsomervi
    wrote on last edited by bsomervi
    #1

    Hi All,

    the following short application demonstrates the problem (occurs on at least Qt v5.12.10):

    #include <QtWidgets>
    #include <QTimer>
    
    class Delegate final
      : public QStyledItemDelegate
    {
    public:
      explicit Delegate (QObject * parent = nullptr)
        : QStyledItemDelegate {parent}
      {
      }
    
      void paint (QPainter * painter, QStyleOptionViewItem const& option
                  , QModelIndex const& index) const
      {
        QStyleOptionProgressBar progress_bar_option;
        progress_bar_option.rect = option.rect;
        progress_bar_option.state = QStyle::State_Enabled;
        progress_bar_option.direction = QApplication::layoutDirection ();
        progress_bar_option.fontMetrics = QApplication::fontMetrics ();
        progress_bar_option.minimum = 0;
        progress_bar_option.maximum = 100;
        auto progress = index.data ().toLongLong ();
        auto percent = int (progress * 100 / index.data (Qt::UserRole).toLongLong ());
        progress_bar_option.progress = percent;
        progress_bar_option.text = QString::number (percent) + '%';
        progress_bar_option.textVisible = true;
        progress_bar_option.textAlignment = Qt::AlignCenter;
        QApplication::style ()->drawControl (QStyle::CE_ProgressBar, &progress_bar_option, painter);
      }
    };
    
    int main (int argc, char * argv[])
    {
      QApplication a {argc, argv};
      QListWidget w;
      Delegate d;
      w.setItemDelegate (&d);
      QListWidgetItem i1 {"Item 1"};
      i1.setData (Qt::UserRole, 100);
      w.addItem (&i1);
      QListWidgetItem i2 {"Item 2"};
      w.addItem (&i2);
      i2.setData (Qt::UserRole, 100);
      w.show ();
      QTimer t;
      qint64 n1 {0};
      qint64 n2 {0};
      t.callOnTimeout ([&] {
                         i1.setData (Qt::DisplayRole, (n1 += 2));
                         i2.setData (Qt::DisplayRole, (n2 += 10));
                        });
      t.start (1000);
      QObject::connect (&a, &QApplication::lastWindowClosed, &a, &QApplication::quit);
      return a.exec ();
    }
    

    On Windows (MinGW) an Linux this works as intended with QProgressBars being used as QListWidgetitem delegates, but on macOS the progress bars get piled on top of each other at the top left of the list widget.

    I am looking for a way use progress bar item delegates that look similar on all platforms.

    TIA
    Bill.

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

      Hi,

      Please try with a more recent version, I remember a fix for drawing in item views.

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

      B 1 Reply Last reply
      0
      • B Offline
        B Offline
        bsomervi
        wrote on last edited by
        #3

        Thanks for the suggestion, but isn't 5.12.10 an LTS release, I would expect bug fixes to be back ported. I tried with 5.15.2 and it is the same.

        Regards
        Bill.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @bsomervi said in Problems with QSytyledItemDelegate and QStyleOptionProgressBar on macOS:

          I would expect bug fixes to be back ported

          It depends on it's priority and a drawing bug in a style delegate for macos is not very critical I would guess.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Please try with a more recent version, I remember a fix for drawing in item views.

            B Offline
            B Offline
            bsomervi
            wrote on last edited by
            #5

            @SGaist did you mean I should try Qt 6?

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

              No, rather the latest 5.15.

              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
              0
              • E Offline
                E Offline
                equeim
                wrote on last edited by
                #7

                BTW I have the same issue with Qt 6.5.1

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

                  While waiting for a better solution, you can negate the y value of the progress_bar_option.rect.

                  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
                  0
                  • I Offline
                    I Offline
                    Icy-Joker
                    wrote on last edited by
                    #9

                    Good News!!!
                    You can just call "QPainter::translate(const QPoint&)" to make it work!

                    if(index.isValid())
                      {
                        int progress = 50;// 从模型中获取进度值
                    
                        QStyleOptionProgressBar progress_bar_option;
                        progress_bar_option.rect = option.rect;
                        progress_bar_option.direction = QApplication::layoutDirection();
                        progress_bar_option.minimum = 0;
                        progress_bar_option.maximum = 100;
                        progress_bar_option.progress = progress;
                        progress_bar_option.text = QString::number(progress) + "%";
                        progress_bar_option.textAlignment = Qt::AlignCenter;
                        progress_bar_option.textVisible = true;
                    
                        painter->save();
                        painter->translate(option.rect.topLeft());//change the coordinate system here
                        qobject_cast<QWidget*>(option.styleObject)->style()->drawControl(QStyle::CE_ProgressBar,&progress_bar_option,painter);
                        painter->restore();
                      }
                    
                    1 Reply Last reply
                    0

                    • Login

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