Questions tagged [qsqltablemodel]

A QSqlTableModel instance is used in the Qt framework to provide an editable data model for a single database table.

QSqlTableModel is a high-level interface for reading and writing database records from a single table. It is built on top of the lower-level QSqlQuery and can be used to provide data to view classes such as QTableView.

QSqlTableModel can also be used to access a database programmatically, without binding it to a view.

The official Qt documentation can be found here for Qt 4.8 and here for Qt 5.

206 questions
13
votes
1 answer

How to refresh a QSqlTableModel while preserving the selection?

I am using a QSqlTableModel and QTableView to view an SQLite database table. I would like to have the table auto refresh every second or so (it's not going to be a very large table - a couple of hundred rows). And i can do this - like so: QTimer…
will
  • 10,260
  • 6
  • 46
  • 69
13
votes
2 answers

Set color to a QTableView row

void MyWindow::initializeModelBySQL(QSqlQueryModel *model,QTableView *table,QString sql){ model = new QSqlQueryModel(this); model->setQuery(sql); } With this method i can set a QSQlQueryModels to my QTableviews. But How i can set…
Tineo
  • 519
  • 1
  • 4
  • 19
10
votes
0 answers

Implementing setEditStrategy in editable QSqlQueryModel

This is a follow-up to this question. In there, we created an editable subclass of QSqlQueryModel, to use with complex queries. Now I need to add a functionality like QTableModel's setEditStrategy so I can cache all changes and accept or revert…
CodingCat
  • 4,999
  • 10
  • 37
  • 59
8
votes
3 answers

QSqlQuery::prepare + MySQL ODBC Connector

I was using Qt's MySQL driver with 32bit MinGW Qt. This was working: QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); db.setDatabaseName("MyDatabase"); //SETUP if (db.open) { QSqlQuery q; if (q.prepare("SELECT id FROM Things WHERE…
smsware
  • 429
  • 1
  • 13
  • 41
8
votes
0 answers

QSqlTableModel::setFilter and Sql Injection

I was wondering if there is a way to prevent SQL injection while using QSqlTableModel::setFilter and no validation for WHERE clause Condition. I don't want to use QSqlQueryModel since I need edit functionality.
SaMax
  • 169
  • 2
  • 10
7
votes
3 answers

When or how to use fetchMore() on QSqlTableModel with a SQLite database for rowCount() to work?

My class DataTable is derived from QAbstractTableModel. It uses a QSqlTableModel object internally to fetch data from a db table. It represents a record for every row in the db (it does more but the record count is always the number of rows in the…
basic6
  • 3,643
  • 3
  • 42
  • 47
6
votes
0 answers

Filter options provided by QSqlRelationalDelegate/QSqlRelation acourding to some WHERE close

I have a QTableView populated by QSqlRelationalTableModel. There is a column that references another table, so when I edit/create a row, this column's editor is QCombobox which gets its data from the related table. The problem is that I want to…
4
votes
2 answers

How to change orientation of Qt TableView

Hi I am using a QTableView to display data from a sql table using the qsqltablemodel asfollows: MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); model = new…
Maelstorm
  • 580
  • 2
  • 10
  • 29
4
votes
1 answer

QSqlRelationalModel model/view update after changes

I've got a problem with updating model/view after changes. To explain better what I mean I wrote a simple example with SQLite. So the main.cpp file: #include #include "MainForm.h" void createConnection() { QSqlDatabase db =…
Daniel
  • 635
  • 1
  • 5
  • 22
3
votes
3 answers

Output top few records by date using kdb

Suppose I have a table, with date, sym, and size as columns. Dates are in ascending order and sizes are in descending order for each date. How do I abridge the table, such that each date, only top few, say 10, records are kept? The simpler the…
3
votes
0 answers

QSqlTableModel primary key to row

I have a QSqlTableModel and I want to insert and update records in it with a special form in a child-window. It's a design choice not to allow "inline editing" which I disabled on purpose. When the user selects an entry (which can be sorted and…
user4063815
3
votes
2 answers

QSqlRelationalTableModel - insert record greater than 256

I have a table node={id,name}, and a table segment={id,nodeFrom,nodeTo} in a SQLite db, where node.id and segment.id are AUTOINCREMENT fields. I'm creating a QSqlTableModel for Node, as follows: nodeModel = new…
mvc
  • 653
  • 1
  • 5
  • 9
3
votes
0 answers

Grid-like Inline editing with QTableView and QSqlTableModel

I am trying to make QTableView behave like a grid table, i.e there is always one empty row in bottom where user input new records to be inserted. Think MS Access subform, or in web dev I think we call it inline editing. I am using Qt 4.7.4, pretty…
Evan
  • 492
  • 1
  • 3
  • 15
3
votes
1 answer

Disable QSql(Relational)TableModel's prefetch/caching behaviour

For some (well, performance) reason, Qt's "model" classes only fetch 256 rows from the database so if you want to append the row to the end of the recordset, you, apparently, must do something along the lines of while (model->canFetchMore()) { …
shylent
  • 10,076
  • 6
  • 38
  • 55
3
votes
5 answers

QSqlRecord sets column with default value to null in query

I have a problem with inserting new QSqlRecord to Postgresql database. Target table is defined as follows: create table samochod( samochod_id integer primary key default nextval('samochod_id_seq'), marka varchar(30), poj_silnika numeric, …
KCH
  • 2,794
  • 2
  • 23
  • 22
1
2 3
13 14