Results 1 to 8 of 8

Thread: Edit TableView Content

  1. #1
    Join Date
    May 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Edit TableView Content

    Hi,

    I'm trying to figure out how to edit the content of a qtableview.

    I have a QTableView which displays sqlite data from a QSqlQuery.

    Qt Code:
    1. QSqlQuery query("SELECT * FROM IdentityLog", db);
    2. while (query.next()) {
    3.  
    4. int event = query.value(3).toInt();
    5.  
    6. switch ( event )
    7. {
    8. case 1:
    9. //set value;
    10. break;
    11. case 2:
    12. //set value
    13. break;
    14. default:
    15. break;
    16. }
    17. }
    18.  
    19. model->setQuery( query );
    20. }
    To copy to clipboard, switch view to plain text mode 

    Where and how do I set the value. I do not want to edit the database, only the table. I've been reading through the classes for QTableView and for QSqlQuery but I can't figure out how to modify the value at a certain index.

    Any help is greatly appreciated. Thanks in advance!

    Emorgen

  2. #2
    Join Date
    Jun 2010
    Posts
    23
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt/Embedded Qt Jambi
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Edit TableView Content

    Hi emorgen,

    you have to use QAbstractTableModel or QSqlQueryModel to set values into QTableView. If you inherent from one of that model, you have to override the data() method to set the values. Maybe this will help you: http://doc.trolltech.com/4.6/itemviews-addressbook.html

  3. The following user says thank you to CeeKey for this useful post:

    emorgen (4th June 2010)

  4. #3
    Join Date
    Oct 2009
    Posts
    151
    Thanks
    6
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Edit TableView Content

    As an aside, QT does not guarantee to order that data is returned in when using 'SELECT *' so value(3) may not always be what you expect.
    Got to keep the loonies on the path ...

  5. #4
    Join Date
    May 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Edit TableView Content

    I am using a QSqlQueryModel and I look at the record() function and it returns a QSqlRecord. QSqlRecord has a setValue member that says it can be used if the content is not read only.

    I check and my data is not set to read only, yet everytime I try and use setValue it does not modify my tables.

    you have to use QAbstractTableModel or QSqlQueryModel to set values into QTableView. If you inherent from one of that model, you have to override the data() method to set the values.
    I have started working on a new class that does this and it should work but I am still curious if this is necessary. It seems like QSqlQueryModel has the capabilities to edit it's data.


    Thanks,

    Emorgen

  6. #5
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Edit TableView Content

    Quote Originally Posted by emorgen View Post
    Where and how do I set the value. I do not want to edit the database, only the table.
    Let me be sure I understand what you want. You want to display data from an SQL query to the user in a QTableView. You want the user to be able to edit the displayed data in table view. You don’t want any changes the user makes to affect the database copy, though... just the information to be used in this instance of the running program. Is that correct?

    It strikes me that the simplest way to do that would be to select the data you need into a new, temporary database table, then access that table using a QSqlTableModel (not a QSqlQueryModel).

    Quote Originally Posted by emorgen View Post
    I am using a QSqlQueryModel and I look at the record() function and it returns a QSqlRecord. QSqlRecord has a setValue member that says it can be used if the content is not read only.
    The first sentence of the documentation for QSqlQueryModel states that it provides a read-only data model. You can’t change data through that model.

    You could subclass that model to allow for changing data, but QSqlTableModel already does that. The thing that’s different about your case (if I’ve understood correctly) is that you want to keep the edited values separate from the original table, letting them “override” the values in the database only for the current run of the program, then discarding them. Doing this by subclassing QSqlQueryModel would require building your own mini-database of the changed values and making sure the data function in your subclass returns the edited value when there is one, but falls back to the database value for items that were not edited. In my opinion, it would be much simpler to make a temporary table that can be discarded when your program finishes, then use an editable model (QSqlTableModel) to access that table, unless there is some good reason you can’t use a temporary table.

  7. #6
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Edit TableView Content

    Quote Originally Posted by emorgen View Post
    I do not want to edit the database, only the table.
    My earlier post was incorrect about needing a temporary table; you can use QSqlTableModel::setEditStrategy(QSqlTableModel::OnManualSubmit) to have the model cache the edits for you, then call QSqlTableModel::revertAll() to discard them.

    Use QSqlTableModel::setData to change data in the model from your program.

  8. #7
    Join Date
    May 2010
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Edit TableView Content

    The first sentence of the documentation for QSqlQueryModel states that it provides a read-only data model. You can’t change data through that model.
    Strange, I checked the isReadOnly and it said false. I guess the copy is editable so it edits the copy but will not alter the original.

    I originally was using a QSqlTableModel but I went to a query because I want to add restrictions to the table. I was afraid it will still attempt to read the database before the filter which could be costly.

    Anyway, I'll give it a try and let you know how it goes. Thanks for the suggestions.

  9. #8
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Edit TableView Content

    Quote Originally Posted by emorgen View Post
    Strange, I checked the isReadOnly and it said false.
    One of the nice things about Qt is that sometimes you can figure something out by just opening up the source code and taking a look. Here’s the code for QSqlQueryModel::record(int):
    Qt Code:
    1. QSqlRecord QSqlQueryModel::record(int row) const
    2. {
    3. Q_D(const QSqlQueryModel);
    4. if (row < 0)
    5. return d->rec;
    6.  
    7. QSqlRecord rec = d->rec;
    8. for (int i = 0; i < rec.count(); ++i)
    9. rec.setValue(i, data(createIndex(row, i), Qt::EditRole));
    10. return rec;
    11. }
    To copy to clipboard, switch view to plain text mode 
    Without looking very far, we can see that all the QSqlFields in the returned QSqlRecord are set using QSqlRecord::setValue just before the record is returned; and the code for that function is:
    Qt Code:
    1. void QSqlRecord::setValue(int index, const QVariant& val)
    2. {
    3. if (!d->contains(index))
    4. return;
    5. detach();
    6. d->fields[index].setValue(val);
    7. }
    To copy to clipboard, switch view to plain text mode 
    where d->fields is a QVector<QSqlField>.

    Since QSqlField::setValue is defined to do nothing when QSqlField::isReadOnly returns true, we can conclude that isReadOnly for a field in a record from a QSqlQueryModel returns no useful information: it must always have been set to false.

    Quote Originally Posted by emorgen View Post
    I guess the copy is editable so it edits the copy but will not alter the original.
    Only the fields in the QSqlRecord returned to you are writable; changing them will not affect the model (neither the database nor what is seen in an attached view); and QSqlQueryModel::setData will not work, because it has not been re-implemented from QAbstractItemModel::setData.

    Quote Originally Posted by emorgen View Post
    I originally was using a QSqlTableModel but I went to a query because I want to add restrictions to the table. I was afraid it will still attempt to read the database before the filter which could be costly.
    According to the documentation at QSqlTableModel::setFilter, that function sets up a WHERE clause; and as long as you call it before you call QSqlTableModel::select(), you won’t query the database except using the WHERE clause.

Similar Threads

  1. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 19:06
  2. Replies: 1
    Last Post: 17th July 2009, 08:22
  3. How to use the content of line Edit in different forms
    By grsandeep85 in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2009, 05:55
  4. Character encoding in text edit and llne edit
    By greenvirag in forum Qt Programming
    Replies: 3
    Last Post: 20th January 2009, 08:45
  5. TableView + SqlQueryModel: can't insert or edit rows
    By jiveaxe in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2008, 21:55

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.