2017-09-02 30 views
0

QTreeview C++プロジェクトで選択したツリービューアイテムのチェックボックスを設定するにはどうすればよいですか?選択したQTreeviewアイテムのチェックボックスを設定する

私の目標は、選択したTreeviewアイテムを繰り返し、チェックボックスの状態をtrueまたはfalseに設定することです。私はrighclickメニューからこれをやっています。私はちょうど選択されたアイテムを収集する方法を知らない。

すべてのアイテムをループしてチェックボックスをtrueまたはfalseに設定する関数を作成しましたが、選択したアイテムのみを設定する方法はわかりません。

void ShotsEditor::checkAll() 
{ 
    for (int i = 0; i < d->sourceModel->rowCount(); i++){ 
     QStandardItem* item = d->sourceModel->item(i); 
     if (item->isCheckable()) 
     { 
      item->setCheckState(Qt::Checked); 
     } 
     else{ 
      if (item->hasChildren()) 
      { 
       for (int j = 0; j < item->rowCount(); j++){ 
        QStandardItem* item1 = item->child(j); 
        if (item1->isCheckable()) 
        { 
         item1->setCheckState(Qt::Checked); 
        } 
       } 
      } 
     } 
    } 
} 

私がこだわっているところこれはこれは私がpysideでそれを行うだろうかです...

void ShotsEditor::checkSelected() 
{ 
    QModelIndexList selected = d->treeView->selectionModel()->selectedIndexes(); 
    qDebug() << "selected indexes" << selected; 
    foreach (QModelIndex index, selected) 
    { 
     if (index.column()==0) 
     { 
      int row = index.row(); 
      qDebug() << "row" << row; 
     } 
    } 
} 

enter image description here

です。私はC++

def set_checked(self): 
     indexes = self.treeview.selectedIndexes() 

     for i in indexes: 
      model = i.model() 
      item = i.model().itemFromIndex(i) 
      print i, model, item 

      if item.isSelectable(): 
       item.setCheckState(QtCore.Qt.Checked) 
+0

あなたが選択したとはどういう意味ですか、あなたは私をしてください説明できます。 – eyllanesc

+0

@eyllanescが質問を更なる情報で更新しました – JokerMartini

+0

@JokerMartiniどのように選択しましたか? – Macias

答えて

1

項目がselectionChangedスロットを上書きされ、選択を取得する適切な方法は、それを選択して選択解除項目を返すにはどのように行うのか分からない、我々はインデックスとチェックを通じてアイテムを取得しますそれら。

class TreeView : public QTreeView 
{ 
    Q_OBJECT 
protected slots: 
    void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected){ 
     Q_UNUSED(deselected) 
     const auto *m = qobject_cast<QStandardItemModel *>(model()); 
     if(m){ 
      for(const auto index: selected.indexes()){ 
       m->itemFromIndex(index)->setCheckState(Qt::Checked); 
      } 
     } 
    } 
}; 

#include "main.moc" 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    TreeView w; 
    QStandardItemModel *model = new QStandardItemModel; 
    for (int i = 0; i < 4; ++i){ 
     QStandardItem *parent =new QStandardItem(QString("Family %1").arg(i)); 
     for (int j = 0; j < 4; ++j){ 
      QStandardItem *child = new QStandardItem(QString("item %1").arg(j)); 
      parent->appendRow(child); 
     } 
     model->appendRow(parent); 
    } 

    w.setModel(model); 
    w.show(); 

    return a.exec(); 
} 

完全な例あなたはこの同じ問題を抱えている人のために、次のlink

+0

@JokerMartini私のソリューションを使いましたか? – eyllanesc

0

で見つける:?

QModelIndexList selected = d->treeView->selectionModel()->selectedIndexes(); 
qDebug() << "selected" << selected; 
foreach (QModelIndex index, selected) 
{ 
    QStandardItem* item = d->sourceModel->itemFromIndex(index); 
    qDebug() << "item" << item; 
    if (item->isSelectable() && item->isCheckable()) 
    { 
     item->setCheckState(Qt::Unchecked); 
    } 
}