QTableView 添加进度条

时间:2020-12-14 16:39:27

记录一下QTableView添加进度条

例子很小,仅供学习

使用QItemDelegate做的实现

有自动更新进度

QTableView 添加进度条

要在.pro文件里添加

CONFIG   += c++
ProgressBarDelegate类
#ifndef PROGRESSBARDELEGATE_H
#define PROGRESSBARDELEGATE_H #include <QItemDelegate> class ProgressBarDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ProgressBarDelegate(QObject *parent = );
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() == ) {
int value = index.model()->data(index).toInt();
QStyleOptionProgressBarV2 progressBarOption;
progressBarOption.rect = option.rect.adjusted(, , -, -);
progressBarOption.minimum = ;
progressBarOption.maximum = ;
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 = );
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 = );
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() << "" << "");
data.append(QStringList() << "" << "");
data.append(QStringList() << "" << "");
data.append(QStringList() << "" << "");
data.append(QStringList() << "" << "");
m_model->setData(data); m_progressBarDelegate = new ProgressBarDelegate(this);
this->setItemDelegate(m_progressBarDelegate);
emit m_model->layoutChanged();
this->setColumnWidth(, );
}
MainWindow 类
#ifndef MAINWINDOW_H
#define MAINWINDOW_H #include <QMainWindow> class TableView;
class QTimer; class MainWindow : public QWidget
{
Q_OBJECT public:
MainWindow(QWidget *parent = );
~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(, );
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();
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().toInt();
qDebug() << value;
if (value < ) {
value += ;
qDebug() << value;
v[] = QString::number(value);
emit model->layoutChanged();
} }
}
github:

https://github.com/lpxxn/QTableViewAddProgressBar/tree/master/TableViewAddProgressDemo