QTableView 添加进度条
记录一下QTableView添加进度条
例子很小,仅供学习
使用QItemDelegate做的实现
有自动更新进度

要在.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
QTableView 添加进度条的更多相关文章
- QTableView 添加进度条 添加按钮 TreeWidget 增删改
http://www.cnblogs.com/li-peng/p/3961386.html http://www.cnblogs.com/li-peng/p/3961843.html http://w ...
- struts2上传文件添加进度条
给文件上传添加进度条,整了两天终于成功了. 想要添加一个上传的进度条,通过分析,应该是需要不断的去访问服务器,询问上传文件的大小.通过已上传文件的大小, 和上传文件的总长度来评估上传的进度. 实现监听 ...
- EasyUI添加进度条
EasyUI添加进度条 添加进度条重点只有一个,如何合理安排进度刷新与异步调用逻辑,假如我们在javascript代码中通过ajax或者第三方框架dwr等对远程服务进行异步调用,实现进度条就需要做到以 ...
- c#devexpress GridContorl添加进度条
demo 的实现图 下边是步骤和代码 1定义 时钟事件,定时的增加进度条的增量. 2: 添加进度条 3;定义字段属性 using System; using System.Collections.G ...
- iOS WKWebView添加进度条02
之前写了一个是关于webview添加进度条的,现在补一个WKWebView进度条. //添加一个全局属性 @property(nonatomic,strong)CALayer *progresslay ...
- iOS-仿支付宝加载web网页添加进度条
代码地址如下:http://www.demodashi.com/demo/11727.html 目前市场上APP常会嵌入不少的h5页面,参照支付宝显示web页面的方式, 做了一个导航栏下的加载进度条. ...
- WebView的使用及添加进度条
实现的效果比较简单类似于微信打开网页,头部有个进度条显示加载进度 下载地址:http://download.csdn.net/detail/qq_29774291/9666941 1.在安卓端加载一个 ...
- QStandardItemModel简单好用,QTableView带进度条
类QabstractItemModel,QabstractListModel,QAbstractTableModel不保存数据,用户需要从这些类派生出子类,并在子类中定义某种数据结构来保存数据.与此不 ...
- ASP添加进度条
今日在学习JavaScript所有写个通用的进度条,防止网页假死.让用户更清楚地知道此网页正在进行加载或者处理一些事情,所有加载进度条是一个网站的必要性. 在网页中Page_load首先要加载此进度条 ...
随机推荐
- 正则表达式 exec 获取字符串中的汉字
要求:仅获取attr中的 “编辑发起状态的执行人表单” ,路径C:\fakepath\是不固定的,可以是C:\fakepath\hhh\hhhh\ 解决: var attr = C:\fakepath ...
- 【hadoop】——window下elicpse连接hadoop集群基础超详细版
1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...
- poi生成word文件
一.简介 对于poi来说,poi可以完成对word.excel.ppt的处理.word目前有两种文件格式,一种是doc后缀.另一种是docx后缀的.2007之前的版本都是doc后缀的,这种格式poi使 ...
- Android开发中XML布局的常用属性说明
<!-- 常用属性说明: android:id="@+id/button" 为控件指定Id android:text="NNNNNNNNNN" 指定控件的 ...
- cookie工具类,解决servlet3.0以前不能添加httpOnly属性的问题
最近在解决XSS注入的问题,由于使用的servlet版本是2.5,不支持httpOnly的属性,故做了个工具类来实现cookie的httpOnly的功能.全类如下: /** * cookie工具类,解 ...
- [原]在win上编译 subversion 源码实践Tonyfield的专栏
(百度和网页的作者无关,不对其内容负责。百度快照谨为网络故障时之索引,不代表被搜索网站的即时页面。) [原]在win上编译 subversion 源码实践 2013-6-9阅读400 评论0 (参考 ...
- windows下 MySQL手动安装与卸载
下载文件以后进行解压 ,指定文件的具体位置 1.安装 选择路径下的mysqld --intall 指定服务名称 --设置配置文件 例子: C:\Users\Administrator\Desktop ...
- finereport普通报表的移动端自适应方案
移动端报表呈现,首先要求的是页面随手机屏幕大小自动放缩(自适应),下面给出一个普通报表中的finereport移动端自适应方案,适用于finereport 7.1之前的版本. 首先,了解一下当前我们可 ...
- Windows 10 Threshold 2 升级记录
昨天(11月17日)升级到Windows 10 Threshold 2版本.我的使用的设备是Surface Pro 3,4G内存,128G硬盘. Threshold 2是作为一个Windows系统更新 ...
- Codeforces 549D. Hear Features[贪心 英语]
D. Haar Features time limit per test 1 second memory limit per test 256 megabytes input standard inp ...