2016-04-27 9 views
0

を示すために、私は私のカスタムモデルに接続QTableViewを持っている:QTableViewとItemDelegateプログレスバー

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_プロパティにアクセスしますか?

+0

が見えます – drescherjm

答えて

1

数値以外の文字列を数値に変換することはできません。あなたの列2つのデータは文字が含まれているので、変換する前に削除する:あなたは `のsetValue(int値)`への接続が欠落しているよう

int progress = index.model()->data().toString().replace("%", "").toInt(); 
関連する問題