Qt-interest Archive, March 2008
QTableView not allowing updates of records with NULL fields
Message 1 in thread
Hi,
I have a small application which is using a QTableView and a
QSqlTableModel. On the backend I have a sqlite database.
The QSqlTableModel is created by doing the following SQL: create table
IF NOT EXISTS foodlist (food text, units text, calories text)
Then, I insert a row into the table: insert into foodlist values
('fooddata','100g', '100')
All pretty standard. I'm not connecting any slots or signals. Just
creating the QSqlTableModel and passing it to the QTableView. I can see
the data in the table without any problems.
But, suppose I do this instead: insert into foodlist values
('fooddata',NULL,NULL)
The query succeeds, and the row is inserted. I can see the data in the
QTableView. BUT when I edit the data, to change any or all of the three
columns, the changes do not stick. The data in the database is not
updated, and the table does show that.
I've tried connecting my own slot to the beforeUpdate signal on
QSqlTableModel. Inside of that slot, I read the QSqlRecord and I can
verify that the data I want written in the update is indeed present.
But, it never gets updated on the disk.
Am I doing something obviously wrong?
Thanks.
--
[ signature omitted ]
Message 2 in thread
You need to have a primary key column.
--
[ signature omitted ]
Message 3 in thread
Acenes wrote:
> You need to have a primary key column.
>
>
That's led me to another problem. I added a column to the table, of type
"id integer primary key autoincrement". I am also hiding that column so
it's not displayed.
When I try to edit the table, it says "QSqlQuery::value: not positioned
on a valid record"
If I were executing a query, I'd do a next() to get to a result record.
But, I'm updating by editing the QTableView attached to the model. Isn't
the model supposed to take care of this?
Thanks,
--
[ signature omitted ]
Message 4 in thread
> "id integer primary key autoincrement". I am also hiding that column so
> it's not displayed.
> When I try to edit the table, it says "QSqlQuery::value: not positioned
> on a valid record"
Hiding the primary key column from the view should work.
But I suppose you removed the column from the model instead.
--
[ signature omitted ]
Message 5 in thread
Acenes wrote:
> Hiding the primary key column from the view should work.
> But I suppose you removed the column from the model instead.
>
Wow you're good. Both of your replies were exactly right. Thank you for
helping me out.
--
[ signature omitted ]