Results 1 to 5 of 5

Thread: boolean field checkbox QTableView

  1. #1
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question boolean field checkbox QTableView

    Hello,
    I have a QTableView with a QSqlRelationalTableModel and i want the boolean colums to be checkboxes in display and edit, I have searching the forum but i have not understand the full picture here i think, i have tried two ways to do that:

    -Create a QItemDelegate subclass and reimplement the methods createEditor, setEditorData, setModelData and updateEditorGeometry to represent a checkbox on edit.

    -Create a QSortProxyModel subclass and reimplement flags to add a Qt.ItemIsUserCheckable on the boolean column an check for Qt::CheckStateRole in data method to reply with the boolean value of the column.

    If i use the sortproxymodel i get the checkbox in display with the correct values but i still see the "true, false" strings and to change the value i get a combobox with the values true or false.

    If i use the delegate i get the checkbox to edit on doble click but i dont see checkboxes in display mode, only my well know "true, false" strings.

    If i use the delegate and the sortproxymodel i get a checkbox on the left of the cell on display mode and when i try to change the value i see other checkbox at the right of the first where i can change the value, after i commit the change the second checkbox is gone and the left display checkbox shows the change.

    Do i have any way to have a checkbox column in display and edit (the same) and not see the text "true, false"?

    Thanks in advance.

  2. #2
    Join Date
    Oct 2007
    Posts
    65
    Thanks
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: boolean field checkbox QTableView

    With a little help from qt-interest mailinglist i have get the effect i want, and without use persistenteditor or delegate, i have subclassed QSortFilterProxyModel and react there to Qt.CheckStateRole, because i am using the same model in one QDataWidgetMapper, i paste the code here to anyone with this problem in the future, is is python but easily interpretable.

    Qt Code:
    1. class GenericSortProxyModel(QSortFilterProxyModel):
    2. def __init__(self, parent):
    3. super(GenericSortProxyModel, self).__init__()
    4. self.booleanSet = set()
    5. self.nullVariant = QVariant()
    6.  
    7. def setParametros(self, booleanColumns=None):
    8. if booleanColumns:
    9. for columna in booleanColumns:
    10. self.booleanSet.add(columna)
    11.  
    12. def flags(self, index):
    13. if not index.isValid():
    14. return Qt.ItemIsEnabled
    15.  
    16. if index.column() in self.booleanSet:
    17. return Qt.ItemIsUserCheckable | Qt.ItemIsSelectable | Qt.ItemIsEnabled
    18. else:
    19. return QSortFilterProxyModel.flags(self, index)
    20.  
    21. def data(self, index, role):
    22. if not index.isValid():
    23. return self.nullVariant()
    24.  
    25. if index.column() in self.booleanSet and role in (Qt.CheckStateRole, Qt.DisplayRole):
    26. if role == Qt.CheckStateRole:
    27. value = QVariant(Qt.Checked) if index.data(Qt.EditRole).toBool() else QVariant(Qt.Unchecked)
    28. return value
    29. else: #if role == Qt.DisplayRole:
    30. return self.nullVariant
    31. else:
    32. return QSortFilterProxyModel.data(self, index, role)
    33.  
    34. def setData(self, index, data, role):
    35. if not index.isValid():
    36. return False
    37.  
    38. if index.column() in self.booleanSet and role == Qt.CheckStateRole:
    39. value = QVariant(True) if data.toInt()[0] == Qt.Checked else QVariant(False)
    40. return QSortFilterProxyModel.setData(self, index, value, Qt.EditRole)
    41. else:
    42. return QSortFilterProxyModel.setData(self, index, data, role)
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Apr 2010
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default boolean checkbox field in QTableView

    in C++ syntax:
    genericsortproxymodel.h content:
    Qt Code:
    1. #ifndef GENERICSORTPROXYMODEL_H
    2. #define GENERICSORTPROXYMODEL_H
    3.  
    4. #include <QSortFilterProxyModel>
    5.  
    6. class GenericSortProxyModel : public QSortFilterProxyModel
    7. {
    8. public:
    9. GenericSortProxyModel(QObject *parent=0);
    10. void setparametros(QList<bool> booleanColumns);
    11. protected:
    12. virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
    13. virtual QVariant data ( const QModelIndex & index, int role ) const;
    14. virtual bool setData ( const QModelIndex & index, const QVariant & value, int role );
    15.  
    16. private:
    17. QList<bool> booleanSet;
    18. QVariant nullVariant;
    19.  
    20. };
    21.  
    22. #endif // GENERICSORTPROXYMODEL_H
    To copy to clipboard, switch view to plain text mode 

    genericsortproxymodel.cpp content:
    Qt Code:
    1. #include "genericsortproxymodel.h"
    2.  
    3. GenericSortProxyModel::GenericSortProxyModel(QObject *parent)
    4. {
    5.  
    6. }
    7. void GenericSortProxyModel::setparametros(QList<bool> booleanColumns)
    8. {
    9. if(!booleanColumns.empty())
    10. foreach(bool column , booleanColumns)
    11. {
    12. this->booleanSet.append(column);
    13. }
    14.  
    15. }
    16.  
    17. Qt::ItemFlags GenericSortProxyModel::flags ( const QModelIndex & index ) const
    18. {
    19.  
    20. if(!index.isValid())
    21. return Qt::ItemIsEnabled;
    22. if(booleanSet.value(index.column()))
    23. return Qt::ItemIsUserCheckable | Qt::ItemIsSelectable | Qt::ItemIsEnabled;
    24. else
    25. return QSortFilterProxyModel::flags(index);
    26. }
    27.  
    28. QVariant GenericSortProxyModel::data ( const QModelIndex & index, int role = Qt::DisplayRole ) const
    29. {
    30. if(!index.isValid())
    31. return this->nullVariant;
    32. if(booleanSet.value(index.column()) && (role==Qt::CheckStateRole || role==Qt::DisplayRole)) {
    33. if(role==Qt::CheckStateRole)
    34. return index.data(Qt::EditRole).toBool()?QVariant(Qt::Checked):QVariant(Qt::Unchecked);
    35. else if(role==Qt::DisplayRole)
    36. return this->nullVariant;
    37. }
    38. else
    39. return QSortFilterProxyModel::data(index,role);
    40.  
    41. }
    42. bool GenericSortProxyModel::setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole )
    43. {
    44. if(!index.isValid())
    45. return false;
    46. if(booleanSet.value(index.column()) && role==Qt::CheckStateRole)
    47. {
    48. QVariant data= value.toInt()==Qt::Checked?QVariant(true):QVariant(false);
    49. return QSortFilterProxyModel::setData(index,data,Qt::EditRole);
    50. }
    51. else
    52. return QSortFilterProxyModel::setData(index,value,role);
    53. }
    To copy to clipboard, switch view to plain text mode 

    and use in main.cpp :
    Qt Code:
    1. ....
    2. QList<bool> chkCols;
    3. // fields that should be checkbox are 1
    4. chkCols<<0<<0<<0<<0<<0<<0<<0<<0<<0<<1;
    5. proxyModel->setparametros(chkCols);
    6. proxyModel->setSourceModel(model);
    7. ui->tableView->setModel(proxyModel);
    8. ....
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2010
    Posts
    17
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: boolean field checkbox QTableView

    but using proxy, this code doesnt work anymore!(relations are lost) :
    Qt Code:
    1. ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));
    To copy to clipboard, switch view to plain text mode 

    does anyone know solution?

  5. #5
    Join Date
    Jul 2006
    Location
    Cagliari - Sardinia - Italy
    Posts
    26
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: boolean field checkbox QTableView

    Sorry, i've wrong
    Last edited by villy; 8th November 2010 at 14:02.

Similar Threads

  1. Some very weird compilation warnings
    By MarkoSan in forum Qt Programming
    Replies: 21
    Last Post: 23rd January 2008, 16:48
  2. QTableView with a checkbox
    By maxpower in forum Qt Programming
    Replies: 17
    Last Post: 18th February 2007, 09:45
  3. How to know when field is edited within QTableView
    By dkite in forum Qt Programming
    Replies: 3
    Last Post: 1st January 2007, 18:51
  4. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31

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.