[SOLVED] About setting a QSqlTableModel to a QStringList
-
If I want a column of values from the database to appear in a QComboBox, I use the model/view programming to achieve it with the code given below
@
QSqlTableModel m = new QSqlTableModel();
m->setTable("table");
m->setModelColumn(3);
m->select();QComboBox *comboBox = new QCombobox();
comboBox->setModel(m);
@I don't want a comboBox but I simply want to store the values in a QStringList and loop through each value to do something with it. My questions are:
- Is it possible with a QStringList? Because I'm not able to set the model it as
@
QStringList stringList;
stringList.setModel();
@- If not QStringList, is it ok to use a QComboBox itself? If not, what are the other options?
-
You can do that:
@
QSqlTableModel m = new QSqlTableModel();
m->setTable("table");
m->setModelColumn(3);
m->select();QStringList stringList; for(int i = 0; i < m->rowCount(); i++) stringList.append( m->record(i).value(your_index_column).toString());
@
And your values is charged in stringList
-
I'd just skip using models at all, and use [[doc:QSqlQuery]] directly:
@
QSqlQuery query = QSqlQuery(db); //db is your database object
query.exec("SELECT MyColumnName FROM MyTableName"); //optionally add filtering or ordering using WHERE and ORDER BY clauses
QStringList results;
while(query.next()) {
results << query.value(0).toString();
}
@
The model/view architecture is there for putting data in views in a generic way. It is not all that suitable for other purposes. Other APIs are more suitable for that. If you find yourself directly manipulating QAbstractItemModel API while you're not building your own models and views, you are probably doing it wrong(TM). -
Your code should work:
@QSqlTableModel *m = new QSqlTableModel();
m->setTable("table");
m->select();QStringList stringList; for(int i = 0; i < m->rowCount(); i++) stringList.append( m->record(i).value(your_index_column).toString());@
but Andre shows more optimal variant
-
Thanks for the reply guys. Why I said I'm using a model is because I have been asked to use a model instead of direct SQL queries. I will however show both versions i.e. maxoreli and Andre's code, to my mentor and see which one I can use. I'll get back to you guys and tell you which one I finally used.
Thanks a lot!
-
Hi again!
Just an update to let y'all know that I went with Andre's solution. Using models became very complicated and resulted in many many lines of code. So yeah, I used QSqlQuery directly.
On the other hand, when I tried maxoreli's solution, I had
@
model = new QSqlQueryModel();
model->setQuery(s);
label->setText(model->record(0).toString();
@and this gives me an error:
- Use of undefined type QSqlRecord
- Left of .toString must point to class/struct/union/generic type
Why?
-
EDIT: Please ignore the following comment. There was a problem with my database and not my code.
Ok guys, I followed Andre's method and I'm not getting any errors. However, I'm not getting any output either. Here's the code
@
QSqlQuery query = QSqlQuery(db); //db is the database object
QString st = QString("SELECT TIME FROM SCHEDULE WHERE ID = %1").arg(str);
query.prepare(st);
query.exec();QStringList strlist;
while(query.next())
{
strlist.append(query.value(0).toString());
qDebug()<<query.value(0).toString();
}
@The qDebug does not display anything. Where am I going wrong? Please help.