Results 1 to 3 of 3

Thread: QTableView checkbox center with stylesheet

  1. #1
    Join Date
    Mar 2009
    Location
    Poland
    Posts
    1

    Default QTableView checkbox center with stylesheet

    Hi!

    I tried to center the checkbox in QTableView by something like this:
    Qt Code:
    1. qApp->setStyleSheet("::indicator {subcontrol-position: center; subcontrol-origin: padding;}");
    To copy to clipboard, switch view to plain text mode 

    But it doesn't work for QTableView item (set by Qt::CheckStateRole in model), although it works good for any other usual widget like QCheckBox or QRadioButton.
    Is that bug in Qt or maybe Qt just does't provide this stylesheet subcontrol feature for item views?

    I see that other properties like e.g 'left' also work for checkbox indicator in view.
    Last edited by quiet; 1st March 2009 at 21:34.

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

    Default Re: QTableView checkbox center with stylesheet

    Hi,

    almost a year since you posted this issue. Did you find a solution? I have the exact problem and it would be really nice if I could solve it using stylesheets. I haven't found a way to solve it in the model-view-delegate either, it is really annoying with the left-aligned checkboxes in my treeviews and tableviews.

  3. #3
    Join Date
    Dec 2010
    Posts
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTableView checkbox center with stylesheet

    Hi,

    If this is still actual here is how I managed to put checkboxes at the center:

    1) Create widget class:

    Qt Code:
    1. #ifndef CENTEREDCHECKBOXWIDGET_H
    2. #define CENTEREDCHECKBOXWIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QCheckBox;
    7.  
    8. class CenteredCheckBoxWidget : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit CenteredCheckBoxWidget(QWidget *parent = 0);
    13.  
    14. QCheckBox *checkBox() const;
    15.  
    16. private:
    17. QCheckBox *checkbox;
    18.  
    19. };
    20.  
    21. #endif // CENTEREDCHECKBOXWIDGET_H
    22.  
    23. #include "centeredcheckboxwidget.h"
    24. #include <QtGui>
    25.  
    26. CenteredCheckBoxWidget::CenteredCheckBoxWidget(QWidget *parent) :
    27. QWidget(parent)
    28. {
    29. setContentsMargins(0, 0, 0, 0);
    30. setAutoFillBackground(true);
    31.  
    32. QHBoxLayout *layout=new QHBoxLayout();
    33. checkbox=new QCheckBox();
    34. layout->addWidget(checkbox);
    35. layout->setAlignment(checkbox, Qt::AlignHCenter | Qt::AlignVCenter);
    36. layout->setContentsMargins(0, 0, 0, 0);
    37. layout->setSpacing(0);
    38.  
    39. setLayout(layout);
    40.  
    41. setFocusProxy(checkbox);
    42. }
    43.  
    44. QCheckBox *CenteredCheckBoxWidget::checkBox() const
    45. {
    46. return checkbox;
    47. }
    To copy to clipboard, switch view to plain text mode 

    2) Create custom delegate:
    Qt Code:
    1. #ifndef BOOLEANDELEGATE_H
    2. #define BOOLEANDELEGATE_H
    3.  
    4. #include <QStyledItemDelegate>
    5.  
    6. class BooleanDelegate : public QStyledItemDelegate
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit BooleanDelegate(QObject *parent, bool defaultValue=true);
    11.  
    12. void paint(QPainter *painter, const QStyleOptionViewItem &option,
    13. const QModelIndex &index) const;
    14.  
    15. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    16. const QModelIndex &index) const;
    17.  
    18. void setEditorData(QWidget *editor, const QModelIndex &index) const;
    19. void setModelData(QWidget *editor, QAbstractItemModel *model,
    20. const QModelIndex &index) const;
    21.  
    22. void updateEditorGeometry(QWidget *editor,
    23. const QStyleOptionViewItem &option, const QModelIndex &index) const;
    24.  
    25. private:
    26. bool defaultValue;
    27.  
    28. };
    29.  
    30. #endif // BOOLEANDELEGATE_H
    31.  
    32. #include "booleandelegate.h"
    33. #include "widgets/centeredcheckboxwidget.h"
    34. #include <QtGui>
    35.  
    36. BooleanDelegate::BooleanDelegate(QObject *parent, bool defaultValue) :
    37. QStyledItemDelegate(parent), defaultValue(defaultValue)
    38. {
    39. }
    40.  
    41. void BooleanDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
    42. const QModelIndex &index) const
    43. {
    44. QVariant value=index.data();
    45. if (!value.isValid() || qVariantCanConvert<bool>(value)) {
    46. bool boolVal = value.isValid() ? value.toBool() : defaultValue;
    47.  
    48. QStyle *style=qApp->style();
    49.  
    50. QRect checkBoxRect=style->subElementRect(QStyle::SE_CheckBoxIndicator, &option);
    51. int chkWidth=checkBoxRect.width();
    52. int chkHeight=checkBoxRect.height();
    53.  
    54. int centerX=option.rect.left() + qMax(option.rect.width()/2-chkWidth/2, 0);
    55. int centerY=option.rect.top() + qMax(option.rect.height()/2-chkHeight/2, 0);
    56. QStyleOptionViewItem modifiedOption(option);
    57. modifiedOption.rect.moveTo(centerX, centerY);
    58. modifiedOption.rect.setSize(QSize(chkWidth, chkHeight));
    59. if(boolVal){
    60. modifiedOption.state |= QStyle::State_On;
    61. }
    62.  
    63. style->drawPrimitive(QStyle::PE_IndicatorItemViewItemCheck, &modifiedOption, painter);
    64. }else {
    65. QStyledItemDelegate::paint(painter, option, index);
    66. }
    67. }
    68.  
    69. QWidget *BooleanDelegate::createEditor(QWidget *parent,
    70. const QStyleOptionViewItem &/* option */,
    71. const QModelIndex &/* index */) const
    72. {
    73. CenteredCheckBoxWidget *editor=new CenteredCheckBoxWidget(parent);
    74. editor->checkBox()->setChecked(defaultValue);
    75.  
    76. return editor;
    77. }
    78.  
    79. void BooleanDelegate::setEditorData(QWidget *editor,
    80. const QModelIndex &index) const
    81. {
    82. QVariant data=index.model()->data(index, Qt::EditRole);
    83. bool value;
    84. if(!data.isNull()){
    85. value=data.toBool();
    86. }else{
    87. value=defaultValue;
    88. }
    89.  
    90.  
    91. CenteredCheckBoxWidget *checkBoxWidget = static_cast<CenteredCheckBoxWidget*>(editor);
    92. checkBoxWidget->checkBox()->setChecked(value);
    93. }
    94.  
    95. void BooleanDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
    96. const QModelIndex &index) const
    97. {
    98. CenteredCheckBoxWidget *checkBoxWidget = static_cast<CenteredCheckBoxWidget*>(editor);
    99.  
    100. bool value = checkBoxWidget->checkBox()->isChecked();
    101.  
    102. model->setData(index, value, Qt::EditRole);
    103. }
    104.  
    105. void BooleanDelegate::updateEditorGeometry(QWidget *editor,
    106. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
    107. {
    108. CenteredCheckBoxWidget *checkBoxWidget = static_cast<CenteredCheckBoxWidget*>(editor);
    109.  
    110. QSize size=checkBoxWidget->sizeHint();
    111. editor->setMinimumHeight(size.height());
    112. editor->setMinimumWidth(size.width());
    113. editor->setGeometry(option.rect);
    114. }
    To copy to clipboard, switch view to plain text mode 

    3) Set the above delegate for the column:
    Qt Code:
    1. BooleanDelegate *isNullableDelegate=new BooleanDelegate(this);
    2. table->setItemDelegateForColumn(columnIndex, isNullableDelegate);
    To copy to clipboard, switch view to plain text mode 

    When retrieving data for this cell use Qt::EditRole and cast it to boolean
    Qt Code:
    1. QVariant nullable=model->index(rowIndex, columnIndex).data(Qt::EditRole);
    2. bool isNullable=nullable.isValid() ? nullable.toBool() : defaultValue; //replace defaultValue with true or false depending on your scenario
    To copy to clipboard, switch view to plain text mode 

    Hope this is useful.

  4. The following user says thank you to rovshanb for this useful post:

    Kyef (16th May 2016)

Similar Threads

  1. QTableView paint row in stylesheet hover
    By estanisgeyer in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2012, 09:17
  2. boolean field checkbox QTableView
    By skuda in forum Qt Programming
    Replies: 4
    Last Post: 8th November 2010, 13:48
  3. QTreeView: How to center a checkbox in a cell
    By chezifresh in forum Qt Programming
    Replies: 3
    Last Post: 19th December 2008, 12:11
  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

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.