Results 1 to 3 of 3

Thread: QTableView with User checkable checkbox

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Posts
    21
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1

    Default QTableView with User checkable checkbox

    hi everyone.i have a QTableView that the first column must have user editable checkbox i reimplemen the data and flags but user can'nt check or unckeck the checkbox this is my code:
    q::q(QObject *parent)
    :QAbstractTableModel(parent)
    {

    }
    int q::rowCount(const QModelIndex &parent) const
    {
    return 3;
    }
    int q::columnCount(const QModelIndex &parent) const
    {
    return 3;
    }
    QVariant q::data(const QModelIndex &index, int role) const
    {
    if(index.column() == 0 && role == Qt::CheckStateRole)
    return Qt::Unchecked;
    return QVariant();
    }
    Qt::ItemFlags q::flags(const QModelIndex &index) const
    {

    if(index.column() == 0 )
    return Qt::ItemIsUserCheckable | Qt::ItemIsEnabled;
    Qt::ItemFlags flags = QAbstractItemModel::flags(index);

    }

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Wiki edits
    15

    Default Re: QTableView with User checkable checkbox

    You need to write the checked status to the model, this should done in setData() method, check this out..

    1. Save the checked status of item in your model (to be done in setData())
    2. Send it back to the view when requested (to be done in data())

    Qt Code:
    1. QVariant q::data(const QModelIndex &index, int role) const
    2. {
    3. if(index.column() == 0 && role == Qt::CheckStateRole)
    4. {
    5. // return the state saved in setData (refer setData())
    6. }
    7. return QVariant();
    8. }
    9.  
    10. bool q::setData(const QModelIndex& index, const QVariant& value, int role)
    11. {
    12. if(role == Qt::CheckStateRole)
    13. {
    14. // Set the checked status internally in your model
    15. }
    16. emit dataChanged(index,index);
    17. return true;
    18. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Aug 2008
    Posts
    45
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default Re: QTableView with User checkable checkbox

    You're always returning Qt::Unchecked within data() method, so the checkbox will always be in unchecked state, no matter what.
    You need to provide a way to to modify the checkbox's state to Checked or Unchecked state. That is done through setData() method, which you haven't implemented.
    Also, please use CODE-tags around your code, it is hard to read it like it is now.

Similar Threads

  1. Can not remove the checkBox from QtableView?
    By mismael85 in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2010, 14:48
  2. How to centering the checkbox in qtableview
    By nemesisqp in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2010, 18:22
  3. CheckBox inside a QTableView
    By voilier92 in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 06:04
  4. checkable items in QTableView
    By cgorac in forum Qt Programming
    Replies: 6
    Last Post: 11th March 2008, 23:45
  5. QTableView with a checkbox
    By maxpower in forum Qt Programming
    Replies: 17
    Last Post: 18th February 2007, 10:45

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.