Select complete row, get/set data in row by columns header names
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:
Code:
QModelIndex index
= m_customerTableView
->currentIndex
();
int i = index.row(); // now you know which record was selected
m_customerTableView->selectRow(i);
Now the row is selected and I can read out the data using
Code:
QModelIndexList indexes = m_customerTableView->selectionModel()->selectedIndexes();
So far so good, this works, but since I want only one row to be selected I want to use
and instead of selectedIndexes() I want to use
Code:
QModelIndexList indexes = m_customerTableView->selectionModel()->selectedColumns();
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.
Code:
map_customer.insert(0,"Customer No.");
map_customer.insert(1,"Title");
map_customer.insert(2,"First name");
map_customer.insert(3,"Last name");
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:
Code:
//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")
Or better solution?
Thanks in advance.
Kind regards,
HomeR
Re: Select complete row, get/set data in row by columns header names
Ok, I found an answer to question 2. I will solve this using enums.
I will search for an answer myself now, but is it possible to pass an enum as a parameter to a function?
So that it can define all enums in a central place and pass them e.g. to my DataReader to put the data correctly into a model?
Re: Select complete row, get/set data in row by columns header names
Why not simply put a few defines into the headerfile of the DataModel?
Code:
#define COLUMN_FIRST_NAME 0
#define COLUMN_LAST_NAME 1
etc
Re: Select complete row, get/set data in row by columns header names
Thanks for you answer. Hm, I guess that would be similar to an enum. But since I am moving away from a xml-file based storage towards an SQLite-database this is easier to handle anyway.
Kind regards,
HomeR