QTableView和ItemDelegate用于显示进度条

时间:2016-04-27 12:19:43

标签: c++ qt

我有一个QTableView连接到我的自定义模型:

class QueueItem {
public:
    enum QueueStatus { PENDING = 0, INPROGRESS, FINISHED, FAILED };
private:
    QueueStatus status_;    
    QString filename_;
    QString localPath_;
    long long filesize_;
    int progress_;
public:
    QueueItem(const QString & file, const QString & localPath) :
        filename_(file), localPath_(localPath)
    {
        filesize_ = 0;
        progress_ = 0;
        status_ = PENDING;
    }

    QString getFilename() const
    {
        return filename_;
    }
    QString getLocalPath() const
    {
        return localPath_;
    }

    long long getFilesize() const
    {
        return filesize_;
    }
    int getProgess() const
    {
        return progress_;
    }
    QueueStatus getStatus() const
    {
        return status_;
    }

    void setProgress(unsigned int prg){
        progress_ = prg;
    }

    void setStatus(QueueItem::QueueStatus status){
        status_ = status;
    }

};


class QueueModel : public QAbstractTableModel {
private:
    QList<QueueItem> data_;
public:

    QueueModel(QObject * parent = 0) : QAbstractTableModel(parent) {}
    int rowCount(const QModelIndex &) const { return data_.count(); }
    int columnCount(const QModelIndex &) const { return 4; }

    QVariant data(const QModelIndex &index, int role) const {
        if (role != Qt::DisplayRole && role != Qt::EditRole) return QVariant();
        const QueueItem & queueItem = data_[index.row()];
        switch (index.column()) {
            case 0: return queueItem.getFilename();
            case 1: return queueItem.getFilesize() + " bytes";
            case 2: return queueItem.getProgess() + "%";
            case 3: {
                std::string str;
                switch (queueItem.getStatus()){
                case QueueItem::PENDING :
                    str = "Pending";
                    break;
                case QueueItem::INPROGRESS:
                    str = "In Progress";
                    break;
                case QueueItem::FINISHED:
                    str = "Finished";
                    break;
                case QueueItem::FAILED:
                    str = "Failed";
                    break;

                default:
                    str = "Unkown";
                }
                return QString::fromStdString(str);
            }
            default: return QVariant();
        };
    }

    QVariant headerData(int section, Qt::Orientation orientation, int role) const {
        if (orientation != Qt::Horizontal) return QVariant();
        if (role != Qt::DisplayRole) return QVariant();
        switch (section) {
        case 0: return "Filename";
        case 1: return "Filesize";
        case 2: return "Progress";
        case 3: return "Status";
        default: return QVariant();
        }
    }
    void refresh(){
        emit dataChanged(index(0, 0), index(data_.count()-1, 5));
    }           

    void append(const QueueItem & queueItem) {
        beginInsertRows(QModelIndex(), data_.count(), data_.count());
        data_.append(queueItem);
        endInsertRows();
    }

};

这就是它的用法:

 // Queue Table     
 ui.tvQueue->setModel(&queueModel_);
 ui.tvQueue->setItemDelegateForColumn(2,new ProgressBarDelegate(this));

所以我创建了以下ItemDelegate:

class ProgressBarDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    ProgressBarDelegate::ProgressBarDelegate(QObject *parent = 0)
        : QStyledItemDelegate(parent)
    {
    }
    void ProgressBarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
        const QModelIndex &index) const
    {
        int progress = index.model()->data().toInt();  // How do I access my models .progress_ property?

        QStyleOptionProgressBar progressBarOption;
        progressBarOption.rect = option.rect;
        progressBarOption.minimum = 0;
        progressBarOption.maximum = 100;
        progressBarOption.progress = progress;
        progressBarOption.text = QString::number(progress) + "%";
        progressBarOption.textVisible = true;

        QApplication::style()->drawControl(QStyle::CE_ProgressBar,
            &progressBarOption, painter);
    }

};

正确显示进度条,但它始终位于位置0.我希望问题位于第int progress = index.model()->data().toInt();行的最后一部分

我没有获得模型的正确progress_值,但始终为0.如何访问我的模型progress_属性?

1 个答案:

答案 0 :(得分:3)

您无法将包含非数字字符的字符串转换为该数字。您的第2列数据包含字符,因此请在转换前将其删除:

int progress = index.model()->data().toString().replace("%", "").toInt();