QTableView 添加进度条
记录一下QTableView添加进度条
例子很小,仅供学习
使用QItemDelegate做的实现
有自动更新进度
要在.pro文件里添加
CONFIG += c++11
ProgressBarDelegate类
#ifndef PROGRESSBARDELEGATE_H #define PROGRESSBARDELEGATE_H #include <QItemDelegate> class ProgressBarDelegate : public QItemDelegate { Q_OBJECT public: explicit ProgressBarDelegate(QObject *parent = 0); void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const; signals: public slots: }; #endif // PROGRESSBARDELEGATE_H
#include "progressbardelegate.h" #include <QPainter> #include <QApplication> ProgressBarDelegate::ProgressBarDelegate(QObject *parent) : QItemDelegate(parent) { } void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if(index.column() == 1) { int value = index.model()->data(index).toInt(); QStyleOptionProgressBarV2 progressBarOption; progressBarOption.rect = option.rect.adjusted(4, 4, -4, -4); progressBarOption.minimum = 0; progressBarOption.maximum = 100; progressBarOption.textAlignment = Qt::AlignRight; progressBarOption.textVisible = true; progressBarOption.progress = value; progressBarOption.text = tr("%1%").arg(progressBarOption.progress); painter->save(); if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, option.palette.highlight()); painter->setBrush(option.palette.highlightedText()); } QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption, painter); painter->restore(); } else { return QItemDelegate::paint (painter, option, index); } }
TableModel类
#ifndef TABLEMODEL_H #define TABLEMODEL_H #include <QAbstractTableModel> class TableModel : public QAbstractTableModel { Q_OBJECT public: explicit TableModel(QObject *parent = 0); int rowCount(const QModelIndex &parent) const; int columnCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role) const; Qt::ItemFlags flags(const QModelIndex &index) const; void setHorizontalHeader(const QStringList& headers); QVariant headerData(int section, Qt::Orientation orientation, int role) const; void setData(const QVector<QStringList>& data); QVector<QStringList>& DataVector() {return m_data;} ~TableModel(void); signals: public slots: private: QStringList m_HorizontalHeader; QVector<QStringList> m_data; }; #endif // TABLEMODEL_H
#include "tablemodel.h" TableModel::TableModel(QObject *parent) : QAbstractTableModel(parent) { } TableModel::~TableModel() { } int TableModel::rowCount(const QModelIndex &parent) const { return m_data.size(); } int TableModel::columnCount(const QModelIndex &parent) const { return m_HorizontalHeader.count(); } QVariant TableModel::data(const QModelIndex &index, int role) const { if (!index.isValid()) return QVariant(); if (role == Qt::DisplayRole) { int ncol = index.column(); int nrow = index.row(); QStringList values = m_data.at(nrow); if (values.size() > ncol) return values.at(ncol); else return QVariant(); } return QVariant(); } Qt::ItemFlags TableModel::flags(const QModelIndex &index) const { if (!index.isValid()) return Qt::NoItemFlags; Qt::ItemFlags flag = QAbstractItemModel::flags(index); // flag|=Qt::ItemIsEditable // 设置单元格可编辑,此处注释,单元格无法被编辑 return flag; } void TableModel::setHorizontalHeader(const QStringList &headers) { m_HorizontalHeader = headers; } QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const { if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { return m_HorizontalHeader.at(section); } return QAbstractTableModel::headerData(section, orientation, role); } void TableModel::setData(const QVector<QStringList> &data) { m_data = data; }
TableView类
#ifndef TABLEVIEW_H #define TABLEVIEW_H #include <QTableView> class TableModel; class ProgressBarDelegate; class TableView : public QTableView { Q_OBJECT public: explicit TableView(QWidget *parent = 0); TableModel* tableModel() {return m_model;} ~TableView(); signals: public slots: private: void iniData(); private: TableModel *m_model; ProgressBarDelegate *m_progressBarDelegate; }; #endif // TABLEVIEW_H
#include "tableview.h" #include "tablemodel.h" #include "progressbardelegate.h" TableView::TableView(QWidget *parent) : QTableView(parent) { iniData(); } TableView::~TableView() { delete m_model; } void TableView::iniData() { m_model = new TableModel(); this->setModel(m_model); QStringList headers; headers << "Id" << "Progress"; m_model->setHorizontalHeader(headers); QVector<QStringList> data; data.append(QStringList() << "1" << "22"); data.append(QStringList() << "2" << "32"); data.append(QStringList() << "3" << "2"); data.append(QStringList() << "4" << "80"); data.append(QStringList() << "5" << "40"); m_model->setData(data); m_progressBarDelegate = new ProgressBarDelegate(this); this->setItemDelegate(m_progressBarDelegate); emit m_model->layoutChanged(); this->setColumnWidth(1, 500); }
MainWindow 类
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> class TableView; class QTimer; class MainWindow : public QWidget { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); private: void init(); void initTimer(); public slots: void updateProgressValue(); private: TableView *tv; QTimer *timer; }; #endif // MAINWINDOW_H
#include "mainwindow.h" #include "tableview.h" #include "tablemodel.h" #include <QLayout> #include <QVBoxLayout> #include <QTimer> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QWidget(parent) { init(); initTimer(); } MainWindow::~MainWindow() { delete tv; delete timer; } void MainWindow::init() { this->resize(800, 600); tv = new TableView(this); QVBoxLayout* layout = new QVBoxLayout(); layout->addWidget(tv); this->setLayout(layout); //this->layout()->addWidget(tv); } void MainWindow::initTimer() { timer = new QTimer(this); timer->setInterval(1000); connect(timer, SIGNAL(timeout()), this, SLOT(updateProgressValue())); timer->start(); } void MainWindow::updateProgressValue() { TableModel* model = tv->tableModel(); QVector<QStringList>& data = model->DataVector(); for (QStringList& v : data) { int value =v.at(1).toInt(); qDebug() << value; if (value < 100) { value += 2; qDebug() << value; v[1] = QString::number(value); emit model->layoutChanged(); } } }
github:
https://github.com/lpxxn/QTableViewAddProgressBar/tree/master/TableViewAddProgressDemo
作者:李鹏
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。