2011-03-18 26 views
1

我有subclassed filesystemmodel包括複選框在列表視圖,這是工作正常。我的問題是,每當我點擊一個項目,該項目的文本消失,當我點擊另一個項目時,以前選擇的項目的文本變得可見。任何人都可以告訴我背後的原因。qlistview與使用自定義模型的複選框

這是我實施的代碼。

請告訴我,我是缺少在這裏, 感謝

#include "custommodel.h" 
#include <iostream> 

using namespace std; 

CustomModel::CustomModel() 
{ 

} 

QVariant CustomModel::data(const QModelIndex& index, int role) const 
{ 
    QModelIndex parent=this->parent(index); 
    if(role == Qt::DecorationRole) 
    { 
     if(this->filePath(parent)=="") 
     { 
      return QIcon(":/Icons/HardDisk.png"); 
     } 

     else if(this->isDir(index)) 
     { 
      QDir dir(this->filePath(index)); 
      QFileInfoList files = dir.entryInfoList(QDir::NoDotAndDotDot | 
                QDir::Files | QDir::Dirs); 
      for(int file = 0; file < files.count(); file++) 

       if(files.count()>0) 
        return QIcon(":/Icons/FullFolder.png"); 
      if(files.count()==0) 
       return QIcon(":/Icons/EmptyFolder.png"); 

     } 
     else{ 

      QFileInfo fi(this->filePath(index)); 
      QString ext = fi.suffix(); 

      if(ext=="jpeg"||ext=="jpg"||ext=="png"||ext=="bmp") 
       return QIcon(filePath(index)); 
      } 
    } 

    if (role == Qt::CheckStateRole && !(this->filePath(parent)=="")) 
     return checklist.contains(index) ? Qt::Checked : Qt::Unchecked; 
    return QFileSystemModel::data(index, role); 

} 

Qt::ItemFlags CustomModel::flags(const QModelIndex& index) const 
{ 

    return QFileSystemModel::flags(index)| Qt::ItemIsUserCheckable; 

} 

bool CustomModel::setData(const QModelIndex& index, const QVariant& value, int role) 
{ 

    if (role == Qt::CheckStateRole) { 

     if (value == Qt::Checked) 
      checklist.insert(index); 
     else 
      checklist.remove(index); 

     emit dataChanged(index, index); 
     return true; 
    } 
    return QFileSystemModel::setData(index, value, role); 
} 
+0

請大家,任何建議? – prakashpun 2011-03-21 04:13:17

回答

1

不知道它是否是相關的,但我發現下面的註釋在: http://doc.trolltech.com/4.6/qt.html#ItemFlag-enum

「請注意,可檢查的項目需要給出一組合適的標誌和一個初始狀態,指示是否檢查該項目,這是爲模型/視圖組件自動處理的,但需要爲QListWidgetItem,QTableWidgetItem和QTreeWidgetItem的實例顯式設置。我可以發現,你的代碼看起來是正確的 - 但也許嘗試在基礎QFileSystemModel類(在你的自定義構造函數中)設置ItemIsUserCheckable標誌,然後看看繼承的data()和setData()方法使用role = Qt :: CheckStateRole。如果你需要維護一個目前被其他原因檢查的列表,那麼繼續在派生的setData()中這樣做,但仍然調用QFileSystemModel :: setData()。同時,我正在尋找爲什麼當我修改文件時,我的QTreeView沒有更新時間戳(除非我退出並重新啓動我的應用程序,這樣會失去目的!)...看起來像dataChanged()信號沒有被髮射。