zoukankan      html  css  js  c++  java
  • Qt之模型/视图(自定义风格)

    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或者其它组件,我不再多说,成功往往向着有准备的人。。。

  • 相关阅读:
    PHP实现无限极分类
    html2canvas生成并下载图片
    一次线上问题引发的过程回顾和思考,以更换两台服务器结束
    Intellij IDEA启动项目报Command line is too long. Shorten command line for XXXApplication or also for
    mq 消费消息 与发送消息传参问题
    idea 创建不了 java 文件
    Java switch 中如何使用枚举?
    Collections排序
    在idea 设置 git 的用户名
    mongodb添加字段和创建自增主键
  • 原文地址:https://www.cnblogs.com/lvdongjie/p/3736784.html
Copyright © 2011-2022 走看看