Results 1 to 6 of 6

Thread: Need help with checkbox in tableview

  1. #1
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Unhappy Need help with checkbox in tableview

    I'm trying to get my table to display a QCheckBox for a column that is boolean (actually int) from a QSqlQueryModel.

    I've implemented a delegate so when the field is clicked a check box appears and this works fine, but I can't work out what code to implement to get the column to display a check box instead of a 0 or 1.

    I've read in another post:

    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().
    With this in mind, I reimplemented flags() which seems to work

    Qt Code:
    1. Qt::ItemFlags PurchaseOrdersRequests::flags(
    2. const QModelIndex &index) const
    3. {
    4. Qt::ItemFlags flags = QSqlQueryModel::flags(index);
    5. flags |= Qt::ItemIsEditable;
    6. if (index.column() == 6)
    7. {
    8. flags |= Qt::ItemIsUserCheckable;
    9. }
    10. return flags;
    11. }
    To copy to clipboard, switch view to plain text mode 

    ...but data() is totally wrong.

    Qt Code:
    1. QVariant PurchaseOrdersRequests::data ( const QModelIndex & index, int role ) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5.  
    6. // if (role == Qt::ItemIsUserCheckable) // NEVER happens !
    7. // qDebug() << " ItemIsUserCheckable";
    8.  
    9. if (role == Qt::DisplayRole)
    10. {
    11. QSqlRecord rec = record(index.row());
    12. QString val = rec.value(index.column()).toString();
    13. return val; // ALWAYS returns an empty string :-(
    14. }
    15. else
    16. return QVariant();
    17. }
    To copy to clipboard, switch view to plain text mode 

    Problems are:
    1) record() is returning empty values.
    2) ItemIsUserCheckable returns false, I thought flags() would be called here where I set it (?)
    3) I don't know what to do with CheckStateRole so there's nothing in my code to return a checkbox for display.
    Last edited by vieraci; 18th June 2009 at 14:16.

  2. #2
    Join Date
    May 2009
    Posts
    62
    Thanks
    2
    Thanked 16 Times in 15 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help with checkbox in tableview

    The role you need to use in QAbstractItemModel::data is Qt::CheckStateRole. I think the method should look like this:

    Qt Code:
    1. QVariant PurchaseOrdersRequests::data ( const QModelIndex & index, int role ) const
    2. {
    3. if (!index.isValid())
    4. {
    5. return QVariant();
    6. }
    7. QSqlRecord rec = record(index.row());
    8. QVariant val = rec.value(index.column());
    9.  
    10. switch (role)
    11. {
    12. case Qt::CheckStateRole:
    13. if (index.column() != 6)
    14. return QVariant();
    15. else if (val.toInt() == 0) // or whatever
    16. return Qt::Unchecked;
    17. else
    18. return Qt::Checked;
    19. case Qt::DisplayRole:
    20. return val;
    21. default:
    22. return QVariant();
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    Depending on the type stored in val, QVariant::toString() may well return an empty string. Use QVariant::typeName() to check what type it is. I think it's best to just pass on the QVariant value.

    No guarantees, I did not check the code above, but I hope it gives you an idea.

  3. #3
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Need help with checkbox in tableview

    Thanks, case Qt::CheckStateRole is what I was missing.

    Now the program crashes at
    QSqlRecord rec = this->record(index.row());
    It appears to continually recurse the function.

    If I change the line to:
    QSqlRecord rec = this->record();
    it doesn't crash, but I get no data. typeName() is blank (one space).

    This function is re-entering like over 100 times and I only have 9 columns and one row of data. that's strange ?

  4. #4
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Need help with checkbox in tableview

    It turns out the required code is:
    Qt Code:
    1. QVariant value = QSqlQueryModel::data(index, role);
    To copy to clipboard, switch view to plain text mode 
    Now data() seems to be called once for DisplayRole and again for CheckStateRole.
    What I end up with is a unchecked checkbox and in the same column, 0 or 1.
    Qt Code:
    1. switch (role)
    2. {
    3. case Qt::CheckStateRole:
    4. if (index.column() != 6)
    5. return value;
    6. else if (value.toInt() == 0) // or whatever
    7. return Qt::Unchecked;
    8. else
    9. return Qt::Checked;
    10. case Qt::DisplayRole:
    11. return value;
    12. default:
    13. return QVariant();
    14. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  5. #5
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Need help with checkbox in tableview

    Can anyone help me out here ? This is acting strange.

    When role is Qt::CheckStateRole the data returned from the model seems to be empty, in contrast to when role is DisplayRole, data is valid.

    Still, I don't know why the column shows a checkbox (ALWAYS not checked because of what I just stated) and 0 or 1 next to it in the same column which seems to be getting into the column during role == DisplayRole.

    This is the whole code:
    Qt Code:
    1. QVariant PurchaseOrdersRequests::data ( const QModelIndex & index, int role ) const
    2. {
    3. if (!index.isValid())
    4. return QVariant();
    5. QVariant value = QSqlQueryModel::data(index, role);
    6.  
    7. if (role == Qt::CheckStateRole && index.column() == 6)
    8. return (value.toInt() != 0) ? Qt::Checked : Qt::Unchecked;
    9. else if (role == Qt::DisplayRole)
    10. return value;
    11. return QVariant();
    12. }
    To copy to clipboard, switch view to plain text mode 
    This code I'm sure is correct, if not, can someone correct me or suggest what else could be the problem ?
    Last edited by vieraci; 19th June 2009 at 17:23. Reason: Added comments

  6. #6
    Join Date
    Apr 2007
    Location
    Sunny Darwin, NT Australia
    Posts
    186
    Thanks
    29
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Need help with checkbox in tableview

    OK I worked out what was happening.
    data() was returning a checkbox AND a value. To stop it, I had to modify it:

    Qt Code:
    1. QVariant PurchaseOrdersRequests::data ( const QModelIndex & index, int role ) const
    2. {
    3. QVariant value = QStandardItemModel::data(index, role);
    4. if (index.column() == 6)
    5. {
    6. if (role == Qt::CheckStateRole)
    7. return (QStandardItemModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
    8. else if (role == Qt::DisplayRole)
    9. return QVariant(); // Don't show value !!
    10. }
    11. return value;
    12. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. boolean field checkbox QTableView
    By skuda in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2010, 13:48
  2. How can I center a checkbox in a treeview?
    By 3str in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2009, 06:47
  3. QTreeView: How to center a checkbox in a cell
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 12:11
  4. Need some help re-painting a checkbox
    By JimDaniel in forum Qt Programming
    Replies: 2
    Last Post: 25th January 2008, 16:08
  5. checkbox
    By nErnie in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2006, 21: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.