Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-94122

when QTableview inserts a progressbar, it doesn't display in correct position

    XMLWordPrintable

    Details

    • Platform/s:
      macOS

      Description

       hi guys. when i try to insert a progressbar into tableview, it not works well on macos(11.3.1 big sur (20E241)), but works well on windows. I have set a delegate for column 1. there is a same question https://bugreports.qt.io/browse/PYSIDE-1464.

       

       

      here is my code 

      //
      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QDebug>
      MainWindow::MainWindow( QWidget* parent )
      
      {
          initUI();
      
          initModel();
      
          initDelegate();
      
          timer = new QTimer();
      
          connect( timer, &QTimer::timeout, this, &MainWindow::download );
      
          timer->setInterval( 1000 );
          timer->start();
      }
      
      MainWindow::~MainWindow() {}
      
      void MainWindow::initUI()
      {
          this->setMinimumSize( QSize( 400, 200 ) );
      
          tableView = new QTableView();
      
          tableView->setEditTriggers( QAbstractItemView::NoEditTriggers );
      
          this->setCentralWidget( tableView );
      }
      
      void MainWindow::initModel()
      {
          model = new QStandardItemModel();
      
          model->setColumnCount( 2 );
      
          model->setHeaderData( 0, Qt::Horizontal, "name" );
          model->setHeaderData( 1, Qt::Horizontal, "progress" );
      
          model->setItem( 0, 0, new QStandardItem( "test-0" ) );
          model->setItem( 0, 1, new QStandardItem( "18" ) );
      
          model->setItem( 1, 0, new QStandardItem( "test-1" ) );
          model->setItem( 1, 1, new QStandardItem( "3" ) );
      
          model->setItem( 2, 0, new QStandardItem( "test-2" ) );
          model->setItem( 2, 1, new QStandardItem( "69" ) );
      
          tableView->setModel( model );
      }
      
      void MainWindow::initDelegate()
      {
          delegate = new ProgressBarDelegate();
      
          tableView->setItemDelegateForColumn( 1, delegate );
      }
      
      void MainWindow::download()
      {
          for ( auto i = 0; i < model->rowCount(); i++ )
          {
              int value = model->index( i, 1 ).data().toInt();
              value++;
      
              model->setData( model->index( i, 1 ), value );
          }
      }
      
      

       

       

      delegate.h

       

      //
      #ifndef PROGRESSBARDELEGATE_H
      #define PROGRESSBARDELEGATE_H
      
      #include <QApplication>
      #include <QItemDelegate>
      #include <QModelIndex>
      #include <QObject>
      #include <QPainter>
      #include <QProgressBar>
      
      class ProgressBarDelegate : public QItemDelegate
      {
          Q_OBJECT
      public:
          ProgressBarDelegate( QObject* parent = 0 );
      
      protected:
          // painting
          void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const override;
      };
      
      #endif  // PROGRESSBARDELEGATE_H
      
      

       

      delegate.cpp

       

      //
      #include "progressbardelegate.h"
      #include <QDebug>
      ProgressBarDelegate::ProgressBarDelegate( QObject* parent ) : QItemDelegate( parent ) {}
      
      void ProgressBarDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
      {
      
          if ( index.column() == 1 )
          {
              const QAbstractItemModel* item_model = index.model();
      
              QRect r = option.rect;
      
              QStyleOptionProgressBarV2 progress_bar_option;
              progress_bar_option.textAlignment = Qt::AlignCenter;
              progress_bar_option.rect          = r; 
              progress_bar_option.minimum       = 0;
              progress_bar_option.maximum       = 100;
              progress_bar_option.progress      = 87;
      
              QProgressBar progress_bar;
      
            
              QApplication::style()->drawControl( QStyle::CE_ProgressBar, &progress_bar_option, painter, &progress_bar );
          }
      }
      
      

       

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            vhilshei Volker Hilsheimer
            Reporter:
            liangkdm liang peng
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes