Results 1 to 2 of 2

Thread: QSqlQueryModel in QTableView with CheckBox in first column - how?

  1. #1
    Join Date
    Jul 2009
    Posts
    21
    Thanks
    1

    Default QSqlQueryModel in QTableView with CheckBox in first column - how?

    Hello everyone.

    This is my first post here so please be “gentle”

    I'm writing application that uses SQL database (Sqlite to be exact). In this program I have few QtableViews populated by QsqlQueryModels. Now I need to have check-boxes in first column of one of this views. I already read many posts about similar problems but I can't seem to understand how exactly should I proceed. Right now I have created custom delegate (for the first column) and model (see code below), and using them I can get check-box displayed, and can even check it and uncheck it... but it seems that state of this check-box is not saved – because as soon as I deselect cell the check-box is in, state resets. Even if my application loses focus, selection state resets...
    I probably messed something up with either delegate or model, but I can't find it .

    Please Help.

    Here are my delegate and model:

    checkablesqlquerymodel.h:
    Qt Code:
    1. #ifndef CHECKABLESQLQUERYMODEL_H
    2. #define CHECKABLESQLQUERYMODEL_H
    3.  
    4. #include <QSqlQueryModel>
    5. #include <QMessageBox>
    6. #include <QString>
    7. #include <QAbstractItemModel>
    8.  
    9. #include <iostream>
    10.  
    11. class checkablesqlquerymodel : public QSqlQueryModel
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. checkablesqlquerymodel(QObject *parent = 0);
    17.  
    18. virtual Qt::ItemFlags flags ( const QModelIndex & index ) const;
    19. //virtual bool setData ( const QModelIndex & index, const QVariant & value, int role = Qt::EditRole );
    20. //virtual void setEditorData ( QWidget * editor, const QModelIndex & index );
    21. };
    22.  
    23. #endif // CHECKABLESQLQUERYMODEL_H
    To copy to clipboard, switch view to plain text mode 

    checkablesqlquerymodel.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "checkablesqlquerymodel.h"
    3.  
    4. checkablesqlquerymodel::checkablesqlquerymodel(QObject *parent):QSqlQueryModel(parent){}
    5.  
    6. Qt::ItemFlags checkablesqlquerymodel::flags ( const QModelIndex & index ) const
    7. {
    8. /*
    9.   if(!index.isValid())
    10.   return 0;*/
    11.  
    12. if(index.column()==0)
    13. {
    14. return Qt::ItemIsEditable | Qt::ItemIsEnabled | Qt::ItemIsSelectable;
    15. }
    16. else
    17. {
    18. return QAbstractItemModel::flags(index);
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    patchesdelegate.h:
    Qt Code:
    1. #ifndef PATCHESDELEGATE_H
    2. #define PATCHESDELEGATE_H
    3.  
    4. #include <QSqlRelationalDelegate>
    5. #include <QCheckBox>
    6. #include <QEvent>
    7. #include <QModelIndex>
    8. #include <QPixmap>
    9. #include <QSize>
    10. #include <QSqlRelationalDelegate>
    11. #include <QCheckBox>
    12. #include <QComboBox>
    13. #include <QtGui>
    14. #include <QMessageBox>
    15.  
    16.  
    17. class patchesdelegate : public QItemDelegate
    18. {
    19. public:
    20. patchesdelegate(QObject *parent);
    21.  
    22. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    23. const QModelIndex &index) const;
    24.  
    25. bool editorEvent(QEvent *event, QAbstractItemModel *model,
    26. const QStyleOptionViewItem &option,
    27. const QModelIndex &index);
    28.  
    29. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    30. const QModelIndex &index) const;
    31. };
    32.  
    33. #endif // PATCHESDELEGATE_H
    To copy to clipboard, switch view to plain text mode 

    patchesdelegate.cpp:
    Qt Code:
    1. #include "patchesdelegate.h"
    2.  
    3. patchesdelegate::patchesdelegate(QObject *parent):QItemDelegate(parent)
    4. {
    5. }
    6.  
    7. void patchesdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    8. const QModelIndex &index) const
    9. {
    10. static int i;
    11. QStyleOptionViewItemV3 opt = option;
    12. opt.rect.adjust(0, 0, -1, -1); // since we draw the grid ourselves
    13. QItemDelegate::paint(painter, opt, index);
    14. if(index.column()==0)
    15. {
    16. bool value =index.data(Qt::UserRole).toBool();
    17. cbo.rect=option.rect;
    18. value ? cbo.state=QStyle::State_On : QStyle::State_Off;
    19. cbo.text="Enabled";
    20.  
    21. QApplication::style()->drawControl(QStyle::CE_CheckBox,&cbo, painter);
    22. }
    23. }
    24.  
    25. bool patchesdelegate::editorEvent(QEvent *event, QAbstractItemModel *model,
    26. const QStyleOptionViewItem &option,
    27. const QModelIndex &index)
    28. {
    29. return QItemDelegate::editorEvent(event, model, option, index);
    30. }
    31.  
    32.  
    33. QWidget *patchesdelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    34. const QModelIndex &index) const
    35. {
    36. QCheckBox *cb=new QCheckBox(parent);
    37. return cb;
    38. }
    To copy to clipboard, switch view to plain text mode 

    and to see how it looks like, please see attachment.

    I use QTSDK-2009.02 currently on Debian 5.0. I write my code in QTCreator.

    Thanks in advance.

    P.S
    Sorry for my English – it is not my native language.
    Attached Images Attached Images

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSqlQueryModel in QTableView with CheckBox in first column - how?

    You probably need to add Qt::ItemIsUserCheckable to your flags for that column.

    You should implement data() and setData() in your model for the Qt::CheckStateRole of the first column. Return (or accept) a Qt::CheckState value: code needs to worry about getting it in/out of the database.

    The standard delegate will then draw a check box in the cell with the check state specified by the data(Qt::CheckStateRole) value.

Similar Threads

  1. QTableView checkbox center with stylesheet
    By quiet in forum Qt Programming
    Replies: 2
    Last Post: 10th March 2011, 14:52
  2. boolean field checkbox QTableView
    By skuda in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2010, 14:48
  3. QTableView column trouble
    By nategoofs in forum Qt Programming
    Replies: 6
    Last Post: 27th October 2009, 21:14
  4. QTableView + QSqlQueryModel column alignment
    By frido in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2008, 07:12
  5. QTableView without the first counter column?
    By pmaktieh.sirhc in forum Qt Programming
    Replies: 2
    Last Post: 4th January 2007, 23:03

Tags for this Thread

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.