Results 1 to 3 of 3

Thread: QSqlTableModel and checkboxes with SQLite database

  1. #1
    Join Date
    Feb 2008
    Location
    South Korea
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlTableModel and checkboxes with SQLite database

    Hi,
    I just started to use Qt Open Source edition last week.

    In my test-project I'm using a QSqlTableModel with a QTableView to display data.
    One column has an integer value of 0 or 1 which I want to be displayed as a checkbox in the QTableView. The user should also be able to click on it to check/uncheck.

    My database engine is SQLite, and it does not have a boolean column type, so no automatic definition can be made.

    I've been browsing the forum and following some examples I've sub-classed the QItemDelegate and used setItemDelegateForColumn(), but this only displays a checkbox after double-clicking for editing.

    I want a checkbox to always be displayed in the view.
    I haven't re-implemented the paint event yet, but it seems a little awkward to have to re-implement so many functions when there is a Qt::ItemIsUserCheckable flag for the model.

    Have I started out all wrong?

    Is there a way that I could set the Qt::ItemIsUserCheckable flag in the table model for my column to handle this more or less automatically?

    Best Regards,
    Ingemar

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QSqlTableModel and checkboxes with SQLite database

    Quote Originally Posted by ibergmark View Post
    Is there a way that I could set the Qt::ItemIsUserCheckable flag in the table model for my column to handle this more or less automatically?
    You can reimplement QAbstractItemModel::flags() to return Qt::ItemIsUserCheckable together with other flags for the checkable column. But notice that you must also handle Qt::CheckStateRole in data() and setData().
    J-P Nurmi

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

    ibergmark (23rd February 2008)

  4. #3
    Join Date
    Feb 2008
    Location
    South Korea
    Posts
    14
    Thanks
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlTableModel and checkboxes with SQLite database

    Quote Originally Posted by jpn View Post
    But notice that you must also handle Qt::CheckStateRole in data() and setData().
    Aha, that's where I went wrong... I'm going to try this approach also...

    Thanks!

    / Ingemar

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.