Results 1 to 5 of 5

Thread: Editable SqlQueryModel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    8

    Default Editable SqlQueryModel

    Hello,
    I have a TableView that uses an SqlQueryModel. I want some of the cells in the table to be editable but the edits should not affect the original database. I subclassed SqlQueryModel with ItemFlags that allow it to be editiable. Because I do not want the changes to affect the database, I can't re implement the setData function, and so any edits get ignored as soon as I hit enter. Any ideas on how I can get the data entered in the view to stay there without affecting the model? Also, how can I tell the subclassed SqlQueryModel that I only want the cells in the second column that do not contain data to be editable?
    The following is my subclass:

    Qt Code:
    1. #include "editablesqlmodel.h"
    2. class EditableSQLModel;
    3.  
    4. EditableSQLModel::EditableSQLModel(QObject *parent):
    5. {
    6.  
    7. }
    8. Qt::ItemFlags EditableSQLModel::flags(const QModelIndex &index) const
    9. {
    10. Qt::ItemFlags flags = QSqlQueryModel::flags(index);
    11. if (index.column() == 1) flags |= Qt::ItemIsEditable;
    12. return flags;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Miss Engineer; 14th May 2014 at 11:37.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Editable SqlQueryModel

    You will have to reimplement the setData() function and store the edited data in your model subclass.
    In data() you then need to check if you have local data for the respective cell and return it instead.

    From you description I take that you have only one such column, so a simple mapping from inf (row) to QVariant (data) should do the trick for the local storage.

    Cheers,
    _

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

    Miss Engineer (15th May 2014)

  4. #3
    Join Date
    Apr 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    8

    Default Re: Editable SqlQueryModel

    Quote Originally Posted by anda_skoa View Post
    You will have to reimplement the setData() function and store the edited data in your model subclass.
    In data() you then need to check if you have local data for the respective cell and return it instead.

    From you description I take that you have only one such column, so a simple mapping from inf (row) to QVariant (data) should do the trick for the local storage.

    Cheers,
    _
    Thanks for the tip.. However, being a beginner, I can't wrap my head around it..
    tried this and I know something is missing as setData is not calling Data()

    Qt Code:
    1. QByteRef EditableSQLModel::data(const QModelIndex &index, int role)
    2. {
    3. QVariant value = QSqlQueryModel::data(index, role);
    4. if (value!="")
    5. return value.toByteArray();
    6. else
    7. return ArgsLength;
    8.  
    9. }
    10. bool EditableSQLModel::setData(const QModelIndex &index, const QVariant &value, int /* role */)
    11. {
    12. if (index.column() !=1)
    13. return false;
    14. ArgsLength= value;
    15.  
    16.  
    17.  
    18. return 1;
    19. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: Editable SqlQueryModel

    You would have something like this

    Qt Code:
    1. class MyModel : public QSqlQueryModel
    2. {
    3. private:
    4. QHash<int, QVariant> m_localData;
    5. };
    To copy to clipboard, switch view to plain text mode 

    In setData() you would store depending on row

    Qt Code:
    1. if (index.column() == 1) {
    2. m_localData.insert(index.row(), value);
    3. }
    To copy to clipboard, switch view to plain text mode 
    in data() you would check if you have local data
    Qt Code:
    1. if (index.column() == 1 && m_localData.contains(index.row()) {
    2. return m_localData.value(index.row())
    3. }
    4. // else get value from base class
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    Miss Engineer (15th May 2014)

  7. #5
    Join Date
    Apr 2014
    Posts
    14
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    8

    Default Re: Editable SqlQueryModel

    Quote Originally Posted by anda_skoa View Post
    You would have something like this

    Qt Code:
    1. class MyModel : public QSqlQueryModel
    2. {
    3. private:
    4. QHash<int, QVariant> m_localData;
    5. };
    To copy to clipboard, switch view to plain text mode 

    In setData() you would store depending on row

    Qt Code:
    1. if (index.column() == 1) {
    2. m_localData.insert(index.row(), value);
    3. }
    To copy to clipboard, switch view to plain text mode 
    in data() you would check if you have local data
    Qt Code:
    1. if (index.column() == 1 && m_localData.contains(index.row()) {
    2. return m_localData.value(index.row())
    3. }
    4. // else get value from base class
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Thanks a lot.. Now it makes sense

Similar Threads

  1. Replies: 1
    Last Post: 19th January 2014, 23:29
  2. Replies: 2
    Last Post: 1st May 2013, 06:49
  3. TableView + SqlQueryModel: can't insert or edit rows
    By jiveaxe in forum Qt Programming
    Replies: 3
    Last Post: 27th September 2008, 21:55
  4. editable rectangles
    By parmar ranjit in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2008, 09:59

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
  •  
Qt is a trademark of The Qt Company.