Choosing columns for display in an QSqlTableModel
Solved
General and Desktop
-
Hi,
I came up with the following code:QSqlQuery query2 ("SELECT Count(*) FROM Items"); query2.first (); row1 = query2.value (0).toInt (); QStandardItemModel *smodel = new QStandardItemModel; ui->tableView->setModel (smodel); ui->tableView->setColumnHidden (0,true); for(int i = 4; i < 14; i++) { ui->tableView->setColumnHidden (i,true); } row1 = 0; while(query.next ()) { QStandardItem *Item1 = new QStandardItem(); QStandardItem *Item2 = new QStandardItem(); QStandardItem *Item3 = new QStandardItem(); Item1->setData (NameD = query.value(0).toString (),Qt::DisplayRole); byteArray = query.value (1).toByteArray (); Pixmap.loadFromData (byteArray); Pixmap = Pixmap.scaled (100,100,Qt::KeepAspectRatio); Item2->setData (QVariant(Pixmap),Qt::DecorationRole ); smodel->setItem (row1++,1,Item2); Item3->setData (DescrD = query.value (2).toString (),Qt::DisplayRole); } Now it displays the images, but not the texts. What did I miss? Thank you for your help.
-
No need to use the QStandardItem here
for(;query.next ();++row1) { smodel->setData (smodel->index(row1,0), NameD = query.value(0).toString ()); Pixmap.loadFromData (query.value (1).toByteArray ();); smodel->setData (smodel->index(row1,1),Pixmap,Qt::DecorationRole ); smodel->setData (smodel->index(row1,2), DescrD = query.value (2).toString ()); }
-
@VRonin
Thank you. I have the following:QStandardItemModel *smodel = new QStandardItemModel; row1 = 0; for(;query.next (); row1++) { smodel->setData (smodel->index(row1,0), NameD = query.value(0).toString ()); qDebug() << "row1: " << row1; qDebug() << "NameD: " << NameD; Pixmap.loadFromData (query.value (1).toByteArray ()); smodel->setData (smodel->index(row1,1),Pixmap,Qt::DecorationRole ); smodel->setData (smodel->index(row1,2), DescrD = query.value (2).toString ()); qDebug() << "DescrD: " << DescrD; } ui->tableView->show ();
qDebug() shows that the variables have the right values, but nothing shows in the table. What did I miss?
-
Hi
Maybe
smodel->setData (xx for DescrD needs Qt::DisplayRole
like u do for Qt::DecorationRole ? -
Hi,
Why not just use a QSqlQueryModel and a QStyledItemDelegate ?
If you want to have specific names for the columns you are selecting then use the
SELECT myColumn AS NewName
construct. -
QStandardItemModel *smodel = new QStandardItemModel(this); // maybe give it a parent to avoid memory leaks for(int row1=0;query.next (); ++row1) { if(row1==0){ const QSqlRecord qRec=query.record(); smodel->insertColumns(0,qRec.count()); Q_ASSERT(query.size()>=0) // if NO COUNT is set in the database then you have to calculate the size manually smodel->inserRows(0,query.size()); // Optional: set headers for(int i=0;i<qRec.count();++i) smodel->setHeaderData(i,Qt::Horizontal,qRec.fieldName(i)); } smodel->setData (smodel->index(row1,0), NameD = query.value(0).toString ()); qDebug() << "row1: " << row1; qDebug() << "NameD: " << NameD; Pixmap.loadFromData (query.value (1).toByteArray ()); smodel->setData (smodel->index(row1,1),Pixmap,Qt::DecorationRole ); smodel->setData (smodel->index(row1,2), DescrD = query.value (2).toString ()); qDebug() << "DescrD: " << DescrD; }
-
Looks like rows are still mot inserted, try this:
for(int row1=0;query.next (); ++row1) { if(row1==0){ const QSqlRecord qRec=query.record(); smodel->insertColumns(0,qRec.count()); // Optional: set headers for(int i=0;i<qRec.count();++i) smodel->setHeaderData(i,Qt::Horizontal,qRec.fieldName(i)); } smodel->inserRow(row1); smodel->setData (smodel->index(row1,0), query.value(0).toString ()); Pixmap.loadFromData (query.value (1).toByteArray ()); smodel->setData (smodel->index(row1,1),Pixmap,Qt::DecorationRole ); smodel->setData (smodel->index(row1,2), query.value (2).toString ()); }
-
Check your query and your database content.