Return items (in QString form) from a QSqlTableModel
-
Hi,
I want return one item (in QString form) from a QSqlTableModel that I maked, how can I do this?
is the QSqlQueryModel class useful? -
You can get items from QSqlTableModel by QSqlRecord, then convert it as you like.
-
Hi,
I found that The QSqlRecord class cant retrieve data from QSqlTableModel object, it just write/update. In the other hand I think that QSqlQueryModel can do this.
Show me how it works with QSqlRecord if you never use it.
-
I am really not sure what you want to achieve. Could you try, perhaps with some pseudo-code, what you are after?
-
I want retrieve records or fields from an QSqlTableModel object, but not directly from the database through the QSqlQueryModel class.
I want this because, in addition to insert a QSqlTableModel object it in a view, I want retrieve/display one field from this QSqlTableModel once I click in the corresponding field in the view.
Since the model is there, I prefer exploit it; but tell me if my purpose is logic or not?
All that is for defining a slot of the Widget:
@void Widget::itemSelected(QModelIndex i)
{
int r = i.row();
QSqlQueryModel *queryModel = new QSqlQueryModel;
queryModel->setQuery("SELECT * FROM table");
QString text = queryModel->record(r).value("text").toString();
ui->textText->setPlainText(text);
}@ -
Why don't you use the model's data() method?
-
I don't understand well what you are trying to do. The selection that you use to point one row in your qsqlmodel comes from another widget? And is it possible that it does not tell you something that can help filtering the query? I mean, you are doing a select * to get a single row, and worst, a single column. At least change your query to reflect the column you need and the row you need. Moreover, I would load data once into qsqlquerymodel and use such model to populate all the widgets in your view(s) without having to redo a query against the database (unless you need to get refreshed data).
-
Hi,
What I need is:I want display a listView of one sorted/filtered(commanded by a lineEdit) column from a database table.
And then when an item of the this view is selected, a textEdit displays an other corresponding field (in the same record as the selected item) ; so the content of the textEdit must change according to the selected item.
So what are the convenient model classes to use and steps to establish this?
I hope its clear now!
regards! -
- Have your model contain all relevant data.
- Set the desired column to display with QListView's setModelColumn() method
- get the text for the textEdit with the data() method from the model
-
[quote author="Volker" date="1322785621"]* Have your model contain all relevant data.
- Set the desired column to display with QListView's setModelColumn() method
- get the text for the textEdit with the data() method from the model[/quote]
Agree! You have to load data once and to display single parts as you want in the widgets you want.
-
And for the filtering/sorting, you use a [[doc:QSortFilterProxyModel]]. You do that by setting your SQL model as the source model of the proxy model, and then setting the proxy model as the source for the view.
-
[quote author="freecamellia" date="1322756692"]Hi,
I found that The QSqlRecord class cant retrieve data from QSqlTableModel object, it just write/update. In the other hand I think that QSqlQueryModel can do this.
Show me how it works with QSqlRecord if you never use it.[/quote]
Maybe i didn't understand you but i meant this:
@ QSqlTableModel tab;
...
QSqlRecord rec = tab.record(rowIndex);
QString str = rec.value(columnName).toString()@ -
In your case
[quote author="Volker" date="1322785621"]- Have your model contain all relevant data.
- Set the desired column to display with QListView's setModelColumn() method
- get the text for the textEdit with the data() method from the model[/quote]
is better solution.
-
[quote author="Volker" date="1322785621"]* Have your model contain all relevant data.
- Set the desired column to display with QListView's setModelColumn() method
- get the text for the textEdit with the data() method from the model[/quote]
Voker is right.
You should try something like this:
@
textEdit->setText(listView->currentIndex().data(Qt::DisplayRole).toString());
@