Hello everyone I have a problem using table view. I have a tableview connected with a QSqlTableModel. On its 2nd column I want to have check state to control the row's Status (active/inactive) but when I try to push the check box QTableView scrolls to the bottom and returns to it's previous position. Is there any solution?


Qt Code:
  1. bool ProjectsModel::setData(const QModelIndex &index, const QVariant &value, int role)
  2. {
  3. if(index.column() == STATUS_INDEX) {
  4. QSqlRecord rec = this->record(index.row());
  5. QString qrstr;
  6.  
  7. qrstr = (rec.field(index.column()).value().toInt() == 1) ? "update " + tableName() + " set Status=0 where BRACE_ID=" : "update " + tableName() + " set Status=1 where BRACE_ID=";
  8. qrstr += QString().setNum(index.row()+1);
  9.  
  10. braceDatabase->exec(qrstr.toUtf8().constData());
  11. return true;
  12. }
  13.  
  14. return false;
  15. }
  16.  
  17. void DataBase::updateTablesSlot()
  18. {
  19. if(projectsModel)
  20. projectsModel->select();
  21. if(attributesModel)
  22. attributesModel->select();
  23. if(targetsModel)
  24. targetsModel->select();
  25. }
  26.  
  27. void DataBase::exec(const std::string &querystr)
  28. {
  29. QSqlQuery qr(db);
  30. qr.exec(querystr.c_str());
  31. //qDebug() <<"Command :" << querystr.c_str() << "-> "<< query->lastError().text();
  32. updateTables();
  33. }
To copy to clipboard, switch view to plain text mode 


35b599e7-599d-482b-af36-b69bf3062b02.jpg