PDA

View Full Version : QTableview, checkboxes and SQlite



mariuccio5
22nd May 2014, 20:02
Hello to each and everyone, I'm trying to put in a tableview a checkbox which should be linked to a value in the db of SQlite, the program select first a query and insert data into the table but I want to add a column with a checkbox for each record found in the db. How can i add it to the QSqlQueryModel and Qtableview already created by QTcreator?
Thank you so much

This is my code:

void myClass::on_pushButton_clicked()
{
Widget conn;
QSqlQueryModel * model = new QSqlQueryModel();
conn.connOpen();
QSqlQuery* qry = new QSqlQuery(conn.db);
qry->prepare("select * from plan where username='"+username_temp+"'");
qry->exec();
QTextStream (stdout) << qry->lastQuery();

model->setQuery(*qry);

ui->tableView->setModel(model);


conn.connClose();
qDebug() << (model->rowCount());
}

nix
23rd May 2014, 08:18
You have to create your own data model inheriting from QSqlTableModel.
Reimplement flags() method and use Qt::ItemIsUserCheckable for the column with the checkbox.
Reimplement data() and setData() to manage the role Qt::CheckStateRole properly for the column with the checkbox.

mariuccio5
25th May 2014, 19:59
Ok, I'm trying to do that, I have the delegate but I don't know how to link with the database. Can you give me an example?

anda_skoa
25th May 2014, 21:52
nix suggested to create a subclass of the model you are currently using, so that you can either make one of the existing columns checkable or add a column.
Another option is to use a proxy model.

Delegate has nothing to do with that.

Cheers,
_