Results 1 to 4 of 4

Thread: QAbstractTableModel::flags -> The view has a checkbox?

  1. #1
    Join Date
    Jan 2006
    Posts
    75
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8

    Default QAbstractTableModel::flags -> The view has a checkbox?

    I've implemented the followings flags function for a custom model. For some reason, the view has a checkbox in each item in the view. Any ideas why?

    Qt Code:
    1. Qt::ItemFlags ConstantsModel::flags(const QModelIndex &index)const
    2. {
    3. if(index.isValid())
    4. return QAbstractTableModel::flags(index) | Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    5.  
    6. return Qt::NoItemFlags;
    7. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Posts
    75
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8

    Default Re: QAbstractTableModel::flags -> The view has a checkbox?

    I'm still not sure what the problem is.

    Here is my data method. When I use the commented code, the cells don't have checkboxes (but input data doesn't stick). When I use the code as it is below, the data sticks, but one checkbox is in each index.

    Qt Code:
    1. QVariant ConstantsModel::data(const QModelIndex &index, int role) const
    2. {
    3. if(index.column() == 0)
    4. //return QVariant(ConstantValues->at(index.row()));
    5. return QVariant(ConstantValues->at(index.row()).X);
    6.  
    7. if(index.column() == 1)
    8. //return QVariant(ConstantValues->at(index.row()));
    9. return QVariant(ConstantValues->at(index.row()).Y);
    10.  
    11. return QVariant();
    12. }
    To copy to clipboard, switch view to plain text mode 

    Here is my setData method (if this helps any):

    Qt Code:
    1. bool ConstantsModel::setData(const QModelIndex &index, const QVariant &Value, int role)
    2. {
    3. ConstantsModelPoint point = ConstantValues->takeAt(index.row());
    4.  
    5. if(index.column() == 0)
    6. {
    7. point.X = Value.toString();
    8. ConstantValues->insert(index.row(), point);
    9. emit this->layoutChanged();
    10. return true;
    11. }
    12. if(index.column() == 1)
    13. {
    14. point.Y = Value.toDouble();
    15. ConstantValues->insert(index.row(), point);
    16. emit this->layoutChanged();
    17. return true;
    18. }
    19. return false;
    20. }
    To copy to clipboard, switch view to plain text mode 

    Finally the data container "ConstantValues" is a QList containing elements from this class:

    Qt Code:
    1. class ConstantsModelPoint : public QVariant
    2. {
    3. public:
    4. ConstantsModelPoint();
    5. ~ConstantsModelPoint();
    6.  
    7. double Y;
    8. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by kroenecker; 24th June 2008 at 20:05.

  3. #3
    Join Date
    Jan 2006
    Posts
    75
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8

    Default Re: QAbstractTableModel::flags -> The view has a checkbox?

    I figured it out:

    Qt Code:
    1. QVariant ConstantsModel::data(const QModelIndex &index, int role) const {
    2. if (!index.isValid())
    3. return QVariant();
    4.  
    5. if (role == Qt::DisplayRole) {
    6. if (index.column() == 0)
    7. return QVariant(ConstantValues->at(index.row()).X);
    8.  
    9. if (index.column() == 1)
    10. return QVariant(QString::number(ConstantValues->at(index.row()).Y));
    11. }
    12. return QVariant();
    13. }
    To copy to clipboard, switch view to plain text mode 

    I wasn't checking for the Qt::DisplayRole. Somehow the QString and the double were being interpreted as asking for a checkbox. I don't know exactly, but this has fixed things.

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 127 Times in 121 Posts

    Default Re: QAbstractTableModel::flags -> The view has a checkbox?

    Read the docs . The Qt::ItemDataRole enum has a value named Qt::CheckStateRole. If the value returned is not false (when converted to bool from QVariant) then a checkbox is displayed...
    Current Qt projects : QCodeEdit, RotiDeCode

Similar Threads

  1. Replies: 3
    Last Post: 1st April 2011, 04:58
  2. Replies: 2
    Last Post: 22nd July 2007, 19:21
  3. Model, View and Proxy
    By No-Nonsense in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2006, 08:50
  4. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01
  5. Replies: 6
    Last Post: 20th April 2006, 10:23

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.