Qt之模型/视图(自定义风格)

关于自定义风格是针对视图与委托而言的,使用事件与QSS都可以进行处理,今天关于美化的细节讲解一下。
先看下图:

先撇开界面的美观性(萝卜青菜,各有所爱),就现有的这些风格,使用QSS + Qt二维绘图已经绰绰有余了。当然,如何让界面更美观,这个没有什么捷径,我只能说一句:无他,唯手熟尔!基本功搞扎实了,实现起来就会游刃有余。。。

void DetailProgressBar::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem view_option(option);
if (view_option.state & QStyle::State_HasFocus) {
view_option.state = view_option.state ^ QStyle::State_HasFocus;
}
QStyledItemDelegate::paint(painter, view_option, index);

if (index.column() == 1) {
const QAbstractItemModel *item_model = index.model();

QModelIndex index1 = item_model->index(index.row(), 2, QModelIndex());
QString name = item_model->data(index1, Qt::DisplayRole).toString();
QModelIndex index2 = item_model->index(index.row(), 4, QModelIndex());
qint64 total_size = item_model->data(index2, Qt::DisplayRole).toLongLong();
QModelIndex index3 = item_model->index(index.row(), 5, QModelIndex());
double speed = item_model->data(index3, Qt::DisplayRole).toDouble();
QModelIndex index4 = item_model->index(index.row(), 3, QModelIndex());
qint64 size = item_model->data(index4, Qt::DisplayRole).toLongLong();

QString str_speed = Util::getSpeed(speed);

if(total_size <= 0)
total_size = 1;
double d_size = (size*1.0)/total_size;
QString q_size = QString::number(d_size, 'f', 2);
qint64 progress = q_size.toDouble() * 100;
if(progress > 100)
{
progress = 100;
}

//设置进度条的风格
QStyleOptionProgressBar progress_bar_option;
progress_bar_option.textAlignment = Qt::AlignCenter;
progress_bar_option.rect = QRect(option.rect.x()+5, option.rect.y()+option.rect.height()-4-6, option.rect.width()-20, 6);
progress_bar_option.minimum = 0;
progress_bar_option.maximum = 100;
progress_bar_option.progress = progress;

painter->drawText(QRect(option.rect.x()+5, option.rect.y()+4, option.rect.width(), 16), QString("%1").arg(name));
painter->drawText(QRect(option.rect.right()-50, option.rect.y()+4, option.rect.width(), 16), QString("%1").arg(str_speed));

QProgressBar progress_bar;

//绘制进度条
QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progress_bar_option, painter, &progress_bar);
}
}

1、进度条样式
QProgressBar{
border:none;
background:rgb(210, 225, 240);
border-radius:3px;
text-align:center;
}
QProgressBar::chunk {
background:rgb(60, 140, 220);
border-radius:3px;
}
2、进度条文本
最简单的可以直接通过QStyleOptionProgressBar的text属性来设置,还可以设置对齐方式等信息。
也可以通过委托来绘制任何想要的内容,这里我通过QPainter的drawText来绘制文本,可以设置画刷、画笔等。

这里,我只做进度条的简单介绍,关于QTableView或者其它组件,我不再多说,成功往往向着有准备的人。。。

Qt之模型/视图(自定义风格)的更多相关文章

  1. Qt之模型/视图(自定义进度条)

    简述 在之前的章节中分享过关于QHeaderView表头排序.添加复选框等内容,相信大家模型/视图.自定义风格有了一定的了解,下面我们来分享一个更常用的内容-自定义进度条. 实现方式: 从QAbstr ...

  2. Qt之模型/视图(委托)

    概念 不同于模型 - 视图 - 控制器模式,模型/视图设计不包括用于管理与用户交互的一个完全独立的组件.一般情况,视图负责将模型数据呈现给用户以及处理用户输入.为了输入更加具有灵活性,则由委托来执行交 ...

  3. 【转】Qt之模型/视图

    [本文转自]http://blog.sina.com.cn/s/blog_a6fb6cc90101hh20.html   作者: 一去丶二三里 关于Qt中MVC的介绍与使用,助手中有一节模型/视图编程 ...

  4. QT MVC 模型/视图

    1. 模型视图实例一, QFileSystemModel  QTreeView ,model/view示例. #include <QApplication> #include <QF ...

  5. Qt之模型/视图(实时更新数据)

    上两节简单介绍了Qt中对于模型/视图的编程,大部分助手里说的很清楚了,现在就开始实战部分吧! 在实际应用中,视图展示的数据往往并非一成不变的,那么如何实时更新成了一个很重要的问题!功能:(1)添加委托 ...

  6. Qt之模型/视图(自定义按钮)

    简述 衍伸前面的章节,我们对QTableView实现了数据显示.自定义排序.显示复选框.进度条等功能的实现,本节主要针对自定义按钮进行讲解,这节过后,也希望大家对自定义有更深入的了解,在以后的功能开发 ...

  7. Qt 之模型/视图(自定义按钮)

    https://blog.csdn.net/liang19890820/article/details/50974059 简述 衍伸前面的章节,我们对QTableView实现了数据显示.自定义排序.显 ...

  8. Qt之模型/视图(自定义按钮)(使用QStyleOption的子类进行drawControl,和我用的方法完全不一样)

    http://blog.csdn.net/liang19890820/article/details/50974059

  9. Qt之模型/视图(自定义按钮)(重绘QStyleOptionButton)

    http://blog.csdn.net/liang19890820/article/details/50974059#comments

随机推荐

  1. RCS版本控制

    RCS(Revision Control System)衍生品有两个 SCCS(Source Code Control System) CVS(Concurrent Versions System)是 ...

  2. SQLSEVER刚建表时主键自增

    alter table 表名 drop column ID alter table 表名 add ID int identity(1,1)

  3. SUSE Ceph RBD Mirror - Storage 6

    Ceph采用的是强一致性同步模型,所有副本都必须完成写操作才算一次写入成功,这就导致不能很好地支持跨域部署,因为如果副本在异地,网络延迟就会很大,拖垮整个集群的写性能.因此,Ceph集群很少有跨域部署 ...

  4. 查看安装的centos的操作系统版本

    cat /etc/redhat-release CentOS Linux release 7.6.1810 (Core)

  5. javascript遍历对象属性

    ":[{"name":"a2"}]}; console.log(Object.keys(obj)); Object.keys(obj).forEach ...

  6. Java面试题及答案解析

    面向对象编程(OOP) Java是一个支持并发.基于类和面向对象的计算机编程语言.下面列出了面向对象软件开发的优点: 代码开发模块化,更易维护和修改. 代码复用. 增强代码的可靠性和灵活性. 增加代码 ...

  7. WebForm FindControl的使用方法

    Control.FindControl (String):在当前的命名容器中搜索带指定 id参数的服务器控件. 有点类似javascript中的getElementById(string); 简单的例 ...

  8. 2018 南京网络预赛Sum ——莫比乌斯反演

    题意 设 $f(n)$ 为 $n=ab$ 的方案数,其中 $a,b$ 为无平方因子数.求 $\displaystyle  \sum_{i=1}^nf(i)$,$n \leq 2e7$. 分析 显然,可 ...

  9. selenium之python源码解读-webdriver继承关系

    一.webdriver继承关系 在selenium中,无论是常用的Firefox Driver 还是Chrome Driver和Ie Drive,他们都继承至selenium\webdriver\re ...

  10. 01_第一次如何上传GitHub(转)Updates were rejected because the tip of your current branch is behind

    https://www.cnblogs.com/code-changeworld/p/4779145.html 刚创建的github版本库,在push代码时出错: $ git push -u orig ...