How to get field value from QStandardItemModel using header name and row number
-
Hi,
I have a QTableView for which i am using QStandardItemModel. I have around 20 columns in this model.
I want to get data of any specific field using row number and column header name.
Write now i am getting values by giving row number and column number in this way,
@
QModelIndex index = finalResultTable->model()->index(20, 0);
QString relativePath = finalResultTable->model()->data(index).toString();
@Problem with this is if i change column sequence in my database(mysql) i need to change all column numbers in code. There must be solution for this but i unable to find out. Any help will be appreciated.
-
ashokb,
You can create table as follows..
@QTableView *table_view;
QStandardItemModel *item_model;void createTable()
{
table_view = new QTableView();
item_model = new QStandardItemModel(0,0,this);
table_view->setModel(item_model);item_model->clear();
//setOfRows() retrives QStringList which contains row wise header names.
//setOfColomns() retrives QStringList which contains colomn wise header names.item_model->setVerticalHeaderLabels(setOfRows()); item_model->setHorizontalHeaderLabels(setOfColomns()); for (int row = 0; row < setOfRows().count(); ++row) { for (int col = 0; col < setOfColomns().count(); ++col) { QModelIndex index = item_model->index(row,col,QModelIndex()); QStandardItem *item = item_model->itemFromIndex(index); item_model->setData(index,0); } } }@
And retrieve data as follows.
@void findData()
{
QString row_name;
QString col_name;
QString relative_path;for (int row = 0; row < setOfRows().count(); ++row) { for (int col = 0; col < setOfColomns().count(); ++col) { QModelIndex index = item_model->index(row,col,QModelIndex()); relative_path = item_model->data(index).toString(); row_name = setOfRows().at(row); col_name = setOfColomns().at(col); } } }@
-
I don't get the question. You say you are using a QStandardItemModel. So far so good. Then, all of a sudden, a database comes into the picture. How is that database hooked up? What is the relationship with your QStandardItemModel? Is there a specific reason you are not using QSqlQueryModel or QSqlTableModel when you are using a database underlying an item view?
-
[quote author="Andre" date="1399444049"]I don't get the question. You say you are using a QStandardItemModel. So far so good. Then, all of a sudden, a database comes into the picture. How is that database hooked up? [/quote]
I also confused. But I assumed he got colomn header names and raw header names from DB.
-
@Andre
Hi, Sorry for confusing question.- I have a table in my database.
- I fetch few columns and one row at a time according to inputs from user on gui.
- Then i construct a QStandardItemModel using QStandardItem for that fetched data. i don't want to select all data from my database table in model, and (as per my knowledge) QSqlTableModel fetch all data in model. (I don't know whether it is possible to fetch data in QSqlTableModel using something like 'WHERE' in mysql).
- Then i set that model(which contains only one row) in table view. Then user edit some fields, some get edited by programmatically(calculations) and when user press save i take data from each field of model, build a query and insert or update that in database.
Now, i want to fetch or set field data from my model by giving column header name. If there is better and efficient way of doing this please let me know, i will implement that, after all i want my software efficient. If i still not clear fill free to ask. (I am little weak in English as well).