Qt QTableView how to have a checkbox only column
2014-05-21 15:47
309 查看
Here is a solution. For this to work properly, your column should not have
the
set. This reads the boolean values from
calls
the
Qt::ItemIsEditableor
Qt::ItemIsUserCheckableflags
set. This reads the boolean values from
Qt::DisplayRoleand
calls
setData()with
Qt::EditRole(i.e. not
Qt::CheckStateRole.)
#include "check_box_delegate.h" #include <QtGui/QApplication> #include <QtGui/QMouseEvent> static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options) { QStyleOptionButton check_box_style_option; QRect check_box_rect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &check_box_style_option); QPoint check_box_point(view_item_style_options.rect.x() + view_item_style_options.rect.width() / 2 - check_box_rect.width() / 2, view_item_style_options.rect.y() + view_item_style_options.rect.height() / 2 - check_box_rect.height() / 2); return QRect(check_box_point, check_box_rect.size()); } CheckBoxDelegate::CheckBoxDelegate(QObject *parent) : QStyledItemDelegate(parent) { } void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { bool checked = index.model()->data(index, Qt::DisplayRole).toBool(); QStyleOptionButton check_box_style_option; check_box_style_option.state |= QStyle::State_Enabled; if (checked) { check_box_style_option.state |= QStyle::State_On; } else { check_box_style_option.state |= QStyle::State_Off; } check_box_style_option.rect = CheckBoxRect(option); QApplication::style()->drawControl(QStyle::CE_CheckBox, &check_box_style_option, painter); } // This is essentially copied from QStyledItemEditor, except that we // have to determine our own "hot zone" for the mouse click. bool CheckBoxDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if ((event->type() == QEvent::MouseButtonRelease) || (event->type() == QEvent::MouseButtonDblClick)) { QMouseEvent *mouse_event = static_cast<QMouseEvent*>(event); if (mouse_event->button() != Qt::LeftButton || !CheckBoxRect(option).contains(mouse_event->pos())) { return false; } if (event->type() == QEvent::MouseButtonDblClick) { return true; } } else if (event->type() == QEvent::KeyPress) { if (static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space && static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select) { return false; } } else { return false; } bool checked = index.model()->data(index, Qt::DisplayRole).toBool(); return model->setData(index, !checked, Qt::EditRole); }
相关文章推荐
- QTableView根据内容自动调整大小(resizeColumnToContents解决不了的)
- Qt17 load sqlite table data to qtableview
- How to view the schema class or attribute definition you first have to install the Active Directory Schema snap-in
- QT30 how to use QcheckBox and QRadioButton in qt
- How to modify non-customizable entity in MS Dynamics CRM 4.0 (Add Product price column to Price List Item view)
- Qt, how to add radio button or checkbox in menu
- How to create .lib file when you only have .dll and .h files
- How to make item view render rich (html) text in Qt
- 【Qt】QTableView中嵌入复选框CheckBox 的四种方法总结
- How to resolve: You do not have a license to use this ActiveX control
- How to create a read-only user in TFS source control
- how to test the real character number if the string include chinese character,notice one chinese character only calculater one b
- How to create a read-only user in TFS source control
- How To Find Out Which Listview Column Was Right-Clicked
- How can I put a checkbox in a column of my DataGrid?
- How to create a read-only user in TFS source control
- how to accepte the array of the checkbox in asp and in php
- Basic Sample - How To Keep ASP.NET ViewState On The Server
- How to get persons who have responsed to a survey in MOSS?
- you have to know about how to create user defined control.