2011-11-17 12 views

Odpowiedz

18

Po pierwsze, trzeba zmodyfikować treeitem śledzić sprawdzanego państwa:

private: 
    ... 
    bool checked; 

i setter i getter:

bool isChecked() const { return checked; } 
void setChecked(bool set) { checked = set; } 

Teraz model będzie musiał zostać zmodyfikowany tak, że widok wie o stanie sprawdzenia:

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

    TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); 

    if (role == Qt::CheckStateRole && index.column() == 0) 
     return static_cast<int>(item->isChecked() ? Qt::Checked : Qt::Unchecked); 

    if (role != Qt::DisplayRole) 
     return QVariant(); 

    return item->data(index.column()); 
} 

i zmodyfikuj metodę flagi modelu, aby pozwolić widoki wiedzą, że model zawiera elementy podlegające kontroli:

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

    Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; 

    if (index.column() == 0) 
     flags |= Qt::ItemIsUserCheckable; 

    return flags; 
} 

Myślę, że powinno się to zrobić. Jeśli chcesz móc zaktualizować stan sprawdzania TreeItem, gdy użytkownik zaznaczy i odepnie elementy, musisz podać metodę QAbstractItemModel :: setData w swoim TreeModelu.

+0

Bardzo fajne! Dokładnie to, czego szukałem. Jednak dowiedziałem się, że powinienem był używać TreeWidget do tego, co próbuję zbudować. Dziękuję bardzo za stanowisko. – Drise

+0

Nie, żebym mógł zaproponować lepsze rozwiązanie bankomatu, ale boolean oczywiście nie ma niesamowitej TRISTATNOŚCI. (f.e. dla częściowo wybranych folderów) – eMPee584

14

Przekonwertowałem powyższe do PyQt dla własnych celów i uznałem, że będę je udostępniać.

def data(self, index, role): 
    if not index.isValid(): 
     return None 

    item = index.internalPointer(); 

    if role == Qt.CheckStateRole and index.column() == self.check_col: 
     return int(Qt.Checked if item.isChecked() else Qt.Unchecked) 

    return super(TreeModel, self).data(index, role) 


def flags(self, index): 
    if not index.isValid(): 
     return None 

    if index.column() == self.check_col: 
     flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsUserCheckable 
    else: 
     flags = super(TreeModel, self).flags(index) 

    return flags 


def setData(self, index, value, role=Qt.EditRole): 
    if index.column() == self.check_col: 
     if role == Qt.EditRole: 
      return False 
     if role == Qt.CheckStateRole: 
      item = self.getItem(index) 
      item.setChecked(value) 
      self.dataChanged.emit(index, index) 
      return True 

    return super(TreeModel, self).setData(index, value, role) 
+0

Stary wątek, ale czy jest szansa, że ​​mógłbyś podzielić się całą klasą? – atomSmasher

0

Oto kolejny przykład kompletny PyQt Woking użyciu QStandardItemModel:

model = QStandardItemModel() 
parent_item = model.invisibleRootItem() # type: QStandardItem 
for row in [ 
    (Qt.Unchecked, 'unchecked'), 
    (Qt.PartiallyChecked, 'partially'), 
    (Qt.Checked, 'checked') 
]: 
    checked, text = row 
    check_item = QStandardItem('') 
    check_item.setCheckable(True) 
    check_item.setCheckState(checked) 
    parent_item.appendRow([check_item, QStandardItem(text)]) 
treeview.setModel(model) 

Btw, to powinien również pracować dla wszelkich aplikacji C++.

Powiązane problemy