Results 1 to 18 of 18

Thread: QTableView with a checkbox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QTableView with a checkbox

    You're changing the model here which is... well... at least controversial when it comes to the MVC paradigm.

    BTW. The model is to be editable so you should subclass QSqlTableModel and also reimplement setData(). Now imagine someone wants to have another view (or access the database through the model) without the checkboxes... Or the column might allow NULLs

    Of course I don't say that you can't do it this way, I only say you should do it with a delegate, as you'll be modifying only the user interaction this way. I wrote about it earlier in the thread.

    Just for completeness - a third way would be to use a proxy model that would translate between the bool and the checkstate role. That would be my second best suggestion (as it doesn't change the source model).

  2. #2
    Join Date
    Feb 2007
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 3 Times in 1 Post

    Default Re: QTableView with a checkbox

    The bounds between View and Model are the conventionalities... Qt4 provides for changing font, color, alignment in model, so it can be useful.

    For NULLs there is Qt::ItemIsTristate.

    setData needs to be reimplemented of course:
    Qt Code:
    1. bool MyModel::setData(const QModelIndex &index, const QVariant &value, int /* role */) {
    2. if (index.column() ......)
    3. return false;
    4.  
    5. QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
    6. int id = QSqlQueryModel::data(primaryKeyIndex).toInt();
    7. //clear();
    8. bool ok;
    9. QSqlQuery query;
    10. if (index.column() == 1) {
    11. query.prepare("update employee set name = ? where id = ?");
    12. query.addBindValue(value.toString());
    13. query.addBindValue(id);
    14. .......
    15. }else if(index.column() == aColWithCheck) {
    16. query.prepare("update employee set married = ? where id = ?");
    17. query.addBindValue(value.toInt());
    18. query.addBindValue(id);
    19. }
    20. ok = query.exec();
    21. refresh();
    22. return ok;
    23. }
    To copy to clipboard, switch view to plain text mode 

    ItemIsUserCheckable in flags() and CheckStateRole in data() achieve the interesting feature: checkbox is clicked -> text label is changed in cell.

    Proxy model 'Bool to CheckStateRole' is realy the best i think

    What about 'Qt Cookbook'?

Similar Threads

  1. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 18:13
  2. checkbox
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2006, 22:59
  3. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 21:31
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42
  5. Multi-line messages in QTableView
    By Conel in forum Qt Programming
    Replies: 6
    Last Post: 13th April 2006, 14:49

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.