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:
QModelIndex index
= m_customerTableView
->currentIndex
();
int i = index.row(); // now you know which record was selected
m_customerTableView->selectRow(i);
QModelIndex index = m_customerTableView->currentIndex();
int i = index.row(); // now you know which record was selected
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
QModelIndexList indexes = m_customerTableView->selectionModel()->selectedIndexes();
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
m_customerTableView->setSelectionMode(QAbstractItemView::SingleSelection);
m_customerTableView->setSelectionBehavior(QAbstractItemView::SelectRows);
To copy to clipboard, switch view to plain text mode
and instead of selectedIndexes() I want to use
QModelIndexList indexes = m_customerTableView->selectionModel()->selectedColumns();
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.
map_customer.insert(0,"Customer No.");
map_customer.insert(1,"Title");
map_customer.insert(2,"First name");
map_customer.insert(3,"Last name");
map_customer.insert(0,"Customer No.");
map_customer.insert(1,"Title");
map_customer.insert(2,"First name");
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:
//set headers
model->setHeaderData(column, Qt::Horizontal, "FirstName");
//write data to column with given header name
int row = 2; //set row to write
model(row, "FirstName")->setData("SomeData")
//set headers
model->setHeaderData(column, Qt::Horizontal, "FirstName");
//write data to column with given header name
int row = 2; //set row to write
model(row, "FirstName")->setData("SomeData")
To copy to clipboard, switch view to plain text mode
Or better solution?
Thanks in advance.
Kind regards,
HomeR
Bookmarks