Results 1 to 4 of 4

Thread: A checkbox only column in QTableView

  1. #1
    Join Date
    Aug 2013
    Posts
    25
    Thanks
    7
    Qt products
    Qt5

    Default A checkbox only column in QTableView

    Hi,

    I have table in Sqlite database which I display using QTableview and QSqlQueryModel. The first column needs to have a header which is a checkbox and all the items in the column needs to be checkbox too. Since the checkbox needs to be centered, I need to use a delegate to paint it.

    I have painted checkbox using the following code. This creates checkboxes, but they cannot be checked or unchecked.

    Qt Code:
    1. static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) {
    2. QStyleOptionButton check_box_style_option;
    3. QRect check_box_rect = QApplication::style()->subElementRect(
    4. QStyle::SE_CheckBoxIndicator,
    5. &check_box_style_option);
    6. QPoint check_box_point(view_item_style_options.rect.x() +
    7. view_item_style_options.rect.width() / 2 -
    8. check_box_rect.width() / 2,
    9. view_item_style_options.rect.y() +
    10. view_item_style_options.rect.height() / 2 -
    11. check_box_rect.height() / 2);
    12. return QRect(check_box_point, check_box_rect.size());
    13. }
    14.  
    15.  
    16. CheckBoxDelegate::CheckBoxDelegate(QObject *parent) :
    17. QStyledItemDelegate(parent)
    18. {
    19. }
    20.  
    21. void CheckBoxDelegate::paint(QPainter *painter,
    22. const QStyleOptionViewItem &option,
    23. const QModelIndex &index) const {
    24. bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
    25.  
    26. QStyleOptionButton check_box_style_option;
    27. check_box_style_option.state |= QStyle::State_Enabled;
    28. if (checked) {
    29. check_box_style_option.state |= QStyle::State_On;
    30. } else {
    31. check_box_style_option.state |= QStyle::State_Off;
    32. }
    33. check_box_style_option.rect = CheckBoxRect(option);
    34.  
    35. QApplication::style()->drawControl(QStyle::CE_CheckBox,
    36. &check_box_style_option,
    37. painter);
    38. }
    To copy to clipboard, switch view to plain text mode 

    I still do not have a clear idea how to go about it. I read somewhere that I need to re-implement data(), setdata() and flags() of QSqlQueryModel. The following code shows how I use the QSqlQueryModel for QTableView. Could you please tell me how to go about it? Any help is appreciated.

    Qt Code:
    1. //Load the tableview with the database table
    2.  
    3. //Initializaton of the query
    4. QSqlQuery *query = new QSqlQuery(dbm->db);
    5.  
    6. query->prepare("SELECT * FROM UserData");
    7.  
    8. if(query->exec())
    9. {
    10. model->setQuery(*query);
    11. ui->tableView->setModel(model);
    12.  
    13. //The header delegate to paint a checkbox on the header
    14. HeaderDelegate *myHeader = new HeaderDelegate(Qt::Horizontal, ui->tableView);
    15. ui->tableView->setHorizontalHeader(myHeader);
    16.  
    17. int RowCount = model->rowCount();
    18.  
    19. qDebug() << RowCount;
    20.  
    21. CheckBoxDelegate *myCheckBoxDelegate = new CheckBoxDelegate();
    22.  
    23. for(int i = 0; i <model->rowCount(); i++)
    24. {
    25. ui->tableView->setItemDelegateForColumn(0,myCheckBoxDelegate);
    26. }
    27.  
    28. ui->tableView->horizontalHeader()->setClickable(true);
    29.  
    30. ui->tableView->setSortingEnabled(true);
    31. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by gfernandes; 28th January 2014 at 13:00.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A checkbox only column in QTableView

    You have two options:

    - make your delegate create a QCheckBox in createEditor and also implement setEditorData and setModelData
    - make the model return a value for Qt::CheckStateRole in column 0 instead of a value for Qt:isplayRole. Might not even need a delegate in this case.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    gfernandes (28th January 2014)

  4. #3
    Join Date
    Aug 2013
    Posts
    25
    Thanks
    7
    Qt products
    Qt5

    Default Re: A checkbox only column in QTableView

    Do you have any example code that I could refer. I think the second method will not have a checkbox centered. I tried the following and it creates a normal checkbox, which is not centered. And here again I cannot check or uncheck. I am so confused.

    QVariant CheckBoxSqlQueryModel::data( const QModelIndex &index, int role /*= Qt:isplayRole*/ ) const
    {
    QVariant value = QSqlQueryModel::data(index,role);


    if (role == Qt::CheckStateRole && index.column() == 0)
    {
    return (QSqlQueryModel::data(index).toInt() != 0) ? Qt::Checked : Qt::Unchecked;
    }
    return value;
    }

    Qt::ItemFlags CheckBoxSqlQueryModel::flags( const QModelIndex &index ) const
    {
    Qt::ItemFlags flags = QSqlQueryModel::flags(index);
    if (index.column() == 0)
    {
    flags |= Qt::ItemIsUserCheckable;
    flags |= Qt::ItemIsSelectable;
    }
    else
    {
    flags |= Qt::ItemIsEditable;
    flags |= Qt::ItemIsSelectable;
    }
    return flags;
    }

    bool CheckBoxSqlQueryModel::setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole )
    {
    if (index.column() == 0)
    {
    role = Qt::CheckStateRole;
    }
    QSqlQueryModel::setData(index,value);
    return true;
    }

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: A checkbox only column in QTableView

    What if you return QVariant() in column == 0 and role != Qt::CheckStateRole?

    Or, if that does not center the checkbox, provide Qt::AlignCentered for Qt::AlignmentRole?

    Cheers,
    _

Similar Threads

  1. Can not remove the checkBox from QtableView?
    By mismael85 in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2010, 13:48
  2. How to centering the checkbox in qtableview
    By nemesisqp in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2010, 17:22
  3. CheckBox inside a QTableView
    By voilier92 in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 05:04
  4. QTableView with a checkbox
    By maxpower in forum Qt Programming
    Replies: 17
    Last Post: 18th February 2007, 09:45
  5. CheckBox and selection in QTableView
    By Mike Krus in forum Qt Programming
    Replies: 1
    Last Post: 21st September 2006, 20:31

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.