Results 1 to 4 of 4

Thread: How to fill an inserted column with data?

  1. #1
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to fill an inserted column with data?

    Hello everyone.

    I want to insert a new column in a model, then fill it up with data.
    The problem below is the simplified one: The model already contains 5 column from a database, filled with data.
    All it want to do is insert a column, and fill each new index with the string "BLA".

    The model is a QSqlTableModel.
    Qt Code:
    1. void ReadersRoom::doPaidColumn()
    2. {
    3. model->insertColumn(5);
    4. model->setHeaderData(5,Qt::Horizontal,trUtf8("Paid"));
    5. QString s = "BLA";
    6.  
    7. for(int i = 0; i < model->rowCount(); ++i)
    8. {
    9. QModelIndex index = model->index(i,5);
    10. if(index.isValid()) // index is valid.
    11. model->setData(index,s); // returns TRUE.
    12. QString test = model->index(i,5).data().toString(); // STRING IS EMPTY!
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 

    The result(on a tableview):
    the new column is there and it contains nothing.

    If I try to rewrite the 4th column with this method it works fine. But the setData won't put any data in the newly added column.

    What am I doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to fill an inserted column with data?

    You can't insert columns into the database like that. insertColumn() may even work but the QSqlRecord that operates on the data in the model knows nothing of your new column so it can't return data for it. It's even probably setData() returns false denoting that the operation has failed.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to fill an inserted column with data?

    I rechecked and setData() returns true.
    My goal is to make a computed column of something related to that database and display it.
    I'm not at all trying to modify the underlying database..

    My plan was to load the desired table from the database to a model, display it, then add one more column to the model, and fill it with whatever I want(it was like this in ado.Net). Your reply suggest that there is a stronger connection between the db and the model.
    I recently started to learn the model/view part of Qt and probably went the wrong way....

    So how can I do something like this?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to fill an inserted column with data?

    Quote Originally Posted by croo View Post
    I rechecked and setData() returns true.
    My goal is to make a computed column of something related to that database and display it.
    I'm not at all trying to modify the underlying database..
    So either subclass the model or implement a proxy model for your database model. A cleaner approach would be to use a proxy although it's probably a bit (just a bit) more work.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Fill a data record in the beforeInsert slot
    By graciano in forum Qt Programming
    Replies: 1
    Last Post: 16th February 2010, 18:36
  2. How to get all column data on row clicked
    By wirasto in forum Qt Programming
    Replies: 7
    Last Post: 9th November 2009, 05:21
  3. Replies: 2
    Last Post: 21st October 2009, 08:13
  4. Replies: 6
    Last Post: 6th August 2009, 17:18
  5. QStandardItem: Setting Top Column Data?
    By AaronMK in forum Qt Programming
    Replies: 2
    Last Post: 24th January 2008, 20:13

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.