Results 1 to 6 of 6

Thread: Adding extra column for QTableView while having QSqlTableModel

  1. #1
    Join Date
    Aug 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Adding extra column for QTableView while having QSqlTableModel

    here is what i want to appear on my QTableView


    ---col1 col2 cus1
    r1
    r2
    r3
    ..

    cus1 will be my custom column and i want to put some text or notes on it. col1 and col2 are columns from the database and it will be automatically populated. I have a fully working QTableView with a QSqlTableModel.

    Ive been reading on how to add virtual columns they say that it can be done by using QProxyModel.I checked the documentation and found out that it's "obsolete".

    What alternatives do i have and where should i start?

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

    Default Re: Adding extra column for QTableView while having QSqlTableModel

    You have two options:

    - use a proxy model (and no, the proxy model approach is not obsolete)
    - derive from the sql model class, override columnCount(), data() and headerData()

    Cheers,
    _

  3. #3
    Join Date
    Aug 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding extra column for QTableView while having QSqlTableModel

    edit: oh nvm i made it work somehow. weird though because i don't see columnCount.

    I just override the columnCount and i set the headerdata.

    Now i need to know how to set its data.
    Last edited by Sekkkyyy; 14th August 2015 at 08:59.

  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: Adding extra column for QTableView while having QSqlTableModel

    By reimplementing setData() and storing the data somewhere.
    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.


  5. #5
    Join Date
    Aug 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Adding extra column for QTableView while having QSqlTableModel

    Hello!

    Here is what i have so far:


    Qt Code:
    1. class Manifest : public QSqlTableModel
    2. {
    3. public:
    4. Manifest(QObject * parent = 0, QSqlDatabase db = QSqlDatabase() ):
    5. QSqlTableModel(parent, db){
    6.  
    7. }
    8.  
    9. Qt::ItemFlags flags ( const QModelIndex & index ) const
    10. {
    11. if (index.column() == 1 || index.column() == 2)
    12. return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
    13. else
    14. return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    15. }
    16.  
    17.  
    18. int columnCount(const QModelIndex &/*parent*/) const
    19. {
    20. return 3;
    21. }
    22.  
    23.  
    24. QVariant data(const QModelIndex &index, int role) const
    25. {
    26.  
    27.  
    28.  
    29. return QSqlTableModel::data(index, role);
    30. }
    31.  
    32. };
    To copy to clipboard, switch view to plain text mode 

    This is my WIP.
    I reimplemented flags so that only certain indexes can be edited.
    I am currently trying to override the "data", because i want to put a checkbox on it.

    Are there any sites wherein can i see the implementations? It's kinda hard if i can't see the underlying implementations.
    Last edited by Sekkkyyy; 14th August 2015 at 10:09.

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

    Default Re: Adding extra column for QTableView while having QSqlTableModel

    http://code.woboq.org/qt5/

    Since you want to add a column, you columnCount() would probably first get the value from the base class and then add 1

    Qt Code:
    1. int Manifest::columnCount(const QModelIndex &parent)
    2. {
    3. return QSqlQueryModel::columnCount(parent) + 1;
    4. }
    To copy to clipboard, switch view to plain text mode 
    In data() you would then check if index.column() is your column and if not, delegate to base class as-is.

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 29th July 2013, 10:10
  2. QToolTip tables adding an extra row
    By ksierens in forum Qt Programming
    Replies: 2
    Last Post: 20th April 2013, 18:05
  3. Replies: 4
    Last Post: 18th June 2012, 08:31
  4. Replies: 1
    Last Post: 15th November 2011, 23:10
  5. Make a column read-only in a QSqlTableModel/QTableView
    By graciano in forum Qt Programming
    Replies: 0
    Last Post: 15th November 2009, 18:18

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.