2013-07-22 48 views
4

我有一個模型如何在QAbstractItemModel中設置QCheckBox?

class TreeModel : public QAbstractItemModel 

我與我的TreeItem排除列== 1的實例填充。在列1我創建CheckBoxes

QVariant TreeModel::data(const QModelIndex &index, int role) const { 
    if (!index.isValid()) 
     return QVariant(); 

    if (role != Qt::DisplayRole) { 
     if (role == Qt::CheckStateRole) { 
      if (index.column() == 1) { 
       if (index.row() == 1) { 
        return Qt::Unchecked; 
       } else 
        return Qt::Checked; 
      } 
     } 
     return QVariant(); 
    } 
    if (role == Qt::DisplayRole) { 
     if (index.column() != 1) { 
      TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); 
      return item->data(index.column()); 
     } 
    } 
    return QVariant(); 
    } 

我可以設置這些複選框雕像Qt::CheckedQt::Unchecked但我的問題是: 我不能改變他們後,他們被點擊時(但setData被稱爲適當的index.column==1role==Qt::CheckStateRole)。 我見過與ItemDelegate的例子 - 只有這似乎工作。這是真的?在這種情況下,我必須使用委託嗎?

這裏是我的setData()功能:

bool TreeModel::setData(const QModelIndex & index, const QVariant & value, 
     int role) { 
    if (role==Qt::CheckStateRole && index.column() == 1) { 
     TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); 
     QTreeWidgetItem *check = static_cast<QTreeWidgetItem*>(index.internalPointer()); 

     //if (item->data(index.column()) == Qt::Checked) 
     if (value == Qt::Checked){ 
      int i=1; 
      //check->setCheckState(1,Qt::Checked); //SIGSEGV 
     }else{ 
      //(item->data(index.column())) = Qt::Unchecked; 
      int i=2; 
      //check->setCheckState(1,Qt::Unchecked); 
     } 

     emit dataChanged(index, index); 
     return true; 
    } 
    emit dataChanged(index, index); 
    return true;; 
} 


Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const { 
    if (!index.isValid()) 
     return 0; 

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEditable; 
    } 

回答

4

我不明白Qt的概念。您在使用setData(店檢查狀態)來設置數據,然後填充數據()這一新的價值模型返回的複選框,都這樣:

使用setData()

bool TreeModel::setData(const QModelIndex & index, const QVariant & value, 
     int role) { 
    if (role==Qt::CheckStateRole && index.column() == 1) { 
     TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); 
     QTreeWidgetItem *check = static_cast<QTreeWidgetItem*>(index.internalPointer()); 

     if (value == Qt::Checked){ 
      int i=1; 
      checkedState_=Qt::Checked; 
     }else{ 
      int i=2; 
      checkedState_=Qt::Unchecked; 
     } 

數據()

QVariant TreeModel::data(const QModelIndex &index, int role) const { 
    if (!index.isValid()) 
     return QVariant(); 

    if (role != Qt::DisplayRole) { 
     if (role == Qt::CheckStateRole) { 
      if (index.column() == 1) { 
        return checkedState_; 
      } 
     } 
     return QVariant(); 
    } 
    if (role == Qt::DisplayRole) { 
     if (index.column() != 1) { 
      TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); 
      return item->data(index.column()); 
     } 
    } 
    return QVariant(); 
    } 
    emit dataChanged(index, index); 
    return true; 
} 
emit dataChanged(index, index); 
return true;; 

}

+0

所以,我說的;) – Trompa

+0

非常感謝你。在setData()中存儲數據是我錯過的 – 4pie0

2

我覺得奇怪,我說,你總是返回的數據相同的結果(CheckStateRole)

if (role == Qt::CheckStateRole) { 
      if (index.column() == 1) { 
       if (index.row() == 1) { 
        return Qt::Unchecked; 
       } else 
        return Qt::Checked; 
      } 
     } 

未選中的(1,1)並檢查(不同於1,1)

setData()被調用,但是,當視圖查詢模型的值時,您總是返回相同的數據()

0

從@Trompa和@tinky_winky使用的例子我設法創建這個工作例如,@Trompa建議,我們需要在返回不同的值或者存儲這個值,並找回我們的data()以獲得該特定索引處的實際狀態。

QVector<int> status; 
int SData::getStatus(int pos) 
{ 
    return status.at(pos); 
} 

void SData::setStatus(int pos,int value) 
{ 
    status.insert(pos, value); 
} 
QVariant STableModel::data(const QModelIndex &index, int role) const 
{ 
    if (!index.isValid()) 
     return QVariant(); 

    if (role == Qt::DisplayRole) 
     return rowList[index.row()][index.column()]; 
    else if (role == Qt::CheckStateRole && index.column() == 0) 
    { 

     int status = SData::instance().getStatus(index.row()); 
     if (status) 
      return Qt::Checked; 
     else 
      return Qt::Unchecked; 
    } 
    else 
     return QVariant(); 
} 

bool STableModel::setData(const QModelIndex &index, 
         const QVariant &value, int role) 
{ 
    if (!index.isValid() /*|| role != Qt::EditRole*/) 
     return false; 
    if (role == Qt::CheckStateRole) 
    { 
     if ((Qt::CheckState)value.toInt() == Qt::Checked) 
     { 
      SData::instance().setStatus(index.row(),1); 
      return true; 
     } 
     else 
     { 
      SData::instance().setStatus(index.row(), 0); 
      return true; 
     } 
    }