Hello!

I have two questions about models and hope that you can help me.


1.)
I want to select a complete row and read out the data. The following function is called on Signal clicked() from QTableView:

Qt Code:
  1. QModelIndex index = m_customerTableView->currentIndex();
  2. int i = index.row(); // now you know which record was selected
  3. m_customerTableView->selectRow(i);
To copy to clipboard, switch view to plain text mode 

Now the row is selected and I can read out the data using

Qt Code:
  1. QModelIndexList indexes = m_customerTableView->selectionModel()->selectedIndexes();
To copy to clipboard, switch view to plain text mode 

So far so good, this works, but since I want only one row to be selected I want to use

Qt Code:
  1. m_customerTableView->setSelectionMode(QAbstractItemView::SingleSelection);
  2. m_customerTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
To copy to clipboard, switch view to plain text mode 

and instead of selectedIndexes() I want to use

Qt Code:
  1. QModelIndexList indexes = m_customerTableView->selectionModel()->selectedColumns();
To copy to clipboard, switch view to plain text mode 

But I could not get this to work. The QModelIndexList is empty.

Is it possible to use it in the way I want or is the method I mentioned first the better way? Or is there another solution?

2.)
So far I read/write the data from/to the rows one field after another. The column-order is set via QMaps, e.g.

Qt Code:
  1. map_customer.insert(0,"Customer No.");
  2. map_customer.insert(1,"Title");
  3. map_customer.insert(2,"First name");
  4. map_customer.insert(3,"Last name");
To copy to clipboard, switch view to plain text mode 

but this is not very low-maintance if something in the data structure is changed.

Is there an easy possibility to read/write data in a row by the models header name?

Pseudo-code could be:

Qt Code:
  1. //set headers
  2. model->setHeaderData(column, Qt::Horizontal, "FirstName");
  3. //write data to column with given header name
  4. int row = 2; //set row to write
  5. model(row, "FirstName")->setData("SomeData")
To copy to clipboard, switch view to plain text mode 

Or better solution?

Thanks in advance.

Kind regards,
HomeR