2013-09-28 9 views
7

C'è un modo per ottenere dati da una riga selezionata da una vista tabella? ho usato QModelIndexList ids = ui->tableView->selectionModel()->selectedRows(); che restituisce un elenco degli indici delle righe selezionate. Non ho bisogno dell'indice. Ho bisogno dei dati di ogni cella della riga selezionata.Qt C++ Ottieni dati da ogni cella di una riga selezionata da una QTableView

+0

usando 'QModelIndex :: dati (ruolo int)' rende sence? – vahancho

risposta

2
QVariant data(const QModelIndex& index, int role) const 

viene utilizzato per restituire dati. Se avete bisogno di ottenere i dati che state facendo qui sulla base di QModelIndex riga e colonna e recuperare da qualche contenitore, forse

std::vector<std::vector<MyData> > data; 

È necessario definire tale mappatura e utilizzarlo in data() e setData() funzioni per gestire l'interazione con dati del modello sottostante.

alternativa QAbstractItemModel e QTreeView offre il modo per assegnare la classe cioè TreeItem ad ogni QModelIndex, in modo da poter prossima recuperare un puntatore ad ogni dati utilizzando static_cast del puntatore restituito da QModelIndex.internalPointer() funzione:

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

così allora si può creare un po 'di mappatura:

// sets the role data for the item at <index> to <value> and updates 
// affected TreeItems and ModuleInfo. returns true if successful 
// otherwise returns false 
bool ModuleEnablerDialogTreeModel::setData(const QModelIndex & index, 
    const QVariant & value, int role) { 
    if (role 
     == Qt::CheckStateRole&& index.column()==ModuleEnablerDialog_CheckBoxColumn) { 
    TreeItem *item = static_cast<TreeItem*>(index.internalPointer()); 
    Qt::CheckState checkedState; 
    if (value == Qt::Checked) { 
     checkedState = Qt::Checked; 
    } else if (value == Qt::Unchecked) { 
     checkedState = Qt::Unchecked; 
    } else { 
     checkedState = Qt::PartiallyChecked; 
    } 
    //set this item currentlyEnabled and check state 
    if (item->hierarchy() == 1) { // the last level in the tree hierarchy 
     item->mModuleInfo.currentlyEnabled = (
      checkedState == Qt::Checked ? true : false); 
     item->setData(ModuleEnablerDialog_CheckBoxColumn, checkedState); 
     if (mRoot_Systems != NULL) { 
     updateModelItems(item); 
     } 
    } else { // every level other than last level 
     if (checkedState == Qt::Checked || checkedState == Qt::Unchecked) { 
     item->setData(index.column(), checkedState); 
     // update children 
     item->updateChildren(checkedState); 
     // and parents 
     updateParents(item); 

example of implementation

7

si può provare questo

int rowidx = ui->tblView->selectionModel()->currentIndex().row(); 
ui->txt1->setText(model->index(rowidx , 0).data().toString()); 
ui->txt2->setText(model->index(rowidx , 1).data().toString()); 
ui->txt3->setText(model->index(rowidx , 2).data().toString()); 
ui->txt4->setText(model->index(rowidx , 3).data().toString()); 
1
Try this for getting data. selectedRows(0) indicates first column of selected rows, selectedRows(1) indicates second column of selected rows row likewise 

QItemSelectionModel *select = ui->existingtable->selectionModel(); 
qDebug()<<select->selectedRows(0).value(0).data().toString(); 
qDebug()<<select->selectedRows(1).value(0).data().toString(); 
qDebug()<<select->selectedRows(2).value(0).data().toString(); 
qDebug()<<select->selectedRows(3).value(0).data().toString(); 
Problemi correlati