QTableView添加进度条

使用QItemDelegate做的实现

有自动更新进度

要在.pro文件里添加 

CONFIG   += c++11
ProgressBarDelegate类
  1. #ifndef PROGRESSBARDELEGATE_H
  2. #define PROGRESSBARDELEGATE_H
  3. #include <QItemDelegate>
  4. class ProgressBarDelegate : public QItemDelegate
  5. {
  6. Q_OBJECT
  7. public:
  8. explicit ProgressBarDelegate(QObject *parent = 0);
  9. void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
  10. signals:
  11. public slots:
  12. };
  13. #endif // PROGRESSBARDELEGATE_H

  1. #include "progressbardelegate.h"
  2. #include <QPainter>
  3. #include <QApplication>
  4. ProgressBarDelegate::ProgressBarDelegate(QObject *parent) :
  5. QItemDelegate(parent)
  6. {
  7. }
  8. void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  9. {
  10. if(index.column() == 1) {
  11. int value = index.model()->data(index).toInt();
  12. QStyleOptionProgressBarV2 progressBarOption;
  13. progressBarOption.rect = option.rect.adjusted( 4, 4, -4, -4);
  14. progressBarOption.minimum = 0;
  15. progressBarOption.maximum = 100;
  16. progressBarOption.textAlignment = Qt::AlignRight;
  17. progressBarOption.textVisible = true;
  18. progressBarOption.progress = value;
  19. progressBarOption.text = tr( "%1%").arg(progressBarOption.progress);
  20. painter->save();
  21. if (option.state & QStyle::State_Selected) {
  22. painter->fillRect(option.rect, option.palette.highlight());
  23. painter->setBrush(option.palette.highlightedText());
  24. }
  25. QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter);
  26. painter->restore();
  27. } else {
  28. return QItemDelegate::paint (painter, option, index);
  29. }
  30. }
TableModel类
  1. #ifndef TABLEMODEL_H
  2. #define TABLEMODEL_H
  3. #include <QAbstractTableModel>
  4. class TableModel : public QAbstractTableModel
  5. {
  6. Q_OBJECT
  7. public:
  8. explicit TableModel(QObject *parent = 0);
  9. int rowCount(const QModelIndex &parent) const;
  10. int columnCount(const QModelIndex &parent) const;
  11. QVariant data(const QModelIndex &index, int role) const;
  12. Qt:: ItemFlags flags(const QModelIndex &index) const;
  13. void setHorizontalHeader(const QStringList& headers);
  14. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
  15. void setData(const QVector<QStringList>& data);
  16. QVector<QStringList>& DataVector() { return m_data;}
  17. ~TableModel( void);
  18. signals:
  19. public slots:
  20. private:
  21. QStringList m_HorizontalHeader;
  22. QVector<QStringList> m_data;
  23. };
  24. #endif // TABLEMODEL_H

  1. #include "tablemodel.h"
  2. TableModel::TableModel(QObject *parent) :
  3. QAbstractTableModel(parent)
  4. {
  5. }
  6. TableModel::~TableModel()
  7. {
  8. }
  9. int TableModel::rowCount( const QModelIndex &parent) const
  10. {
  11. return m_data.size();
  12. }
  13. int TableModel::columnCount( const QModelIndex &parent) const
  14. {
  15. return m_HorizontalHeader.count();
  16. }
  17. QVariant TableModel::data( const QModelIndex &index, int role) const
  18. {
  19. if (!index.isValid())
  20. return QVariant();
  21. if (role == Qt::DisplayRole) {
  22. int ncol = index.column();
  23. int nrow = index.row();
  24. QStringList values = m_data.at(nrow);
  25. if (values.size() > ncol)
  26. return values.at(ncol);
  27. else
  28. return QVariant();
  29. }
  30. return QVariant();
  31. }
  32. Qt::ItemFlags TableModel::flags( const QModelIndex &index) const
  33. {
  34. if (!index.isValid())
  35. return Qt::NoItemFlags;
  36. Qt::ItemFlags flag = QAbstractItemModel::flags(index);
  37. // flag|=Qt::ItemIsEditable // 设置单元格可编辑,此处注释,单元格无法被编辑
  38. return flag;
  39. }
  40. void TableModel::setHorizontalHeader( const QStringList &headers)
  41. {
  42. m_HorizontalHeader = headers;
  43. }
  44. QVariant TableModel::headerData( int section, Qt::Orientation orientation, int role) const
  45. {
  46. if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
  47. return m_HorizontalHeader.at(section);
  48. }
  49. return QAbstractTableModel::headerData(section, orientation, role);
  50. }
  51. void TableModel::setData( const QVector<QStringList> &data)
  52. {
  53. m_data = data;
  54. }
TableView类
  1. #ifndef TABLEVIEW_H
  2. #define TABLEVIEW_H
  3. #include <QTableView>
  4. class TableModel;
  5. class ProgressBarDelegate;
  6. class TableView : public QTableView
  7. {
  8. Q_OBJECT
  9. public:
  10. explicit TableView(QWidget *parent = 0);
  11. TableModel* tableModel() { return m_model;}
  12. ~TableView();
  13. signals:
  14. public slots:
  15. private:
  16. void iniData();
  17. private:
  18. TableModel *m_model;
  19. ProgressBarDelegate *m_progressBarDelegate;
  20. };
  21. #endif // TABLEVIEW_H
  1. #include "tableview.h"
  2. #include "tablemodel.h"
  3. #include "progressbardelegate.h"
  4. TableView::TableView(QWidget *parent) :
  5. QTableView(parent)
  6. {
  7. iniData();
  8. }
  9. TableView::~TableView()
  10. {
  11. delete m_model;
  12. }
  13. void TableView::iniData()
  14. {
  15. m_model = new TableModel();
  16. this->setModel(m_model);
  17. QStringList headers;
  18. headers << "Id" << "Progress";
  19. m_model->setHorizontalHeader(headers);
  20. QVector<QStringList> data;
  21. data.append(QStringList() << "1" << "22");
  22. data.append(QStringList() << "2" << "32");
  23. data.append(QStringList() << "3" << "2");
  24. data.append(QStringList() << "4" << "80");
  25. data.append(QStringList() << "5" << "40");
  26. m_model->setData(data);
  27. m_progressBarDelegate = new ProgressBarDelegate( this);
  28. this->setItemDelegate(m_progressBarDelegate);
  29. emit m_model->layoutChanged();
  30. this->setColumnWidth( 1, 500);
  31. }

MainWindow 类
  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. class TableView;
  5. class QTimer;
  6. class MainWindow : public QWidget
  7. {
  8. Q_OBJECT
  9. public:
  10. MainWindow(QWidget *parent = 0);
  11. ~MainWindow();
  12. private:
  13. void init();
  14. void initTimer();
  15. public slots:
  16. void updateProgressValue();
  17. private:
  18. TableView *tv;
  19. QTimer *timer;
  20. };
  21. #endif // MAINWINDOW_H

  1. #include "mainwindow.h"
  2. #include "tableview.h"
  3. #include "tablemodel.h"
  4. #include <QLayout>
  5. #include <QVBoxLayout>
  6. #include <QTimer>
  7. #include <QDebug>
  8. MainWindow::MainWindow(QWidget *parent)
  9. : QWidget(parent)
  10. {
  11. init();
  12. initTimer();
  13. }
  14. MainWindow::~MainWindow()
  15. {
  16. delete tv;
  17. delete timer;
  18. }
  19. void MainWindow::init()
  20. {
  21. this->resize( 800, 600);
  22. tv = new TableView( this);
  23. QVBoxLayout* layout = new QVBoxLayout();
  24. layout->addWidget(tv);
  25. this->setLayout(layout);
  26. //this->layout()->addWidget(tv);
  27. }
  28. void MainWindow::initTimer()
  29. {
  30. timer = new QTimer( this);
  31. timer->setInterval( 1000);
  32. connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressValue()));
  33. timer->start();
  34. }
  35. void MainWindow::updateProgressValue()
  36. {
  37. TableModel* model = tv->tableModel();
  38. QVector<QStringList>& data = model->DataVector();
  39. for (QStringList& v : data) {
  40. int value =v.at( 1).toInt();
  41. qDebug() << value;
  42. if (value < 100) {
  43. value += 2;
  44. qDebug() << value;
  45. v[ 1] = QString::number(value);
  46. emit model->layoutChanged();
  47. }
  48. }
  49. }

猜你喜欢

转载自blog.csdn.net/u014746838/article/details/81037246