bool DelegateInfoCheckBox::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if(index.column() == 0){ return false; } 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); }
void DelegateInfoCheckBox::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if(index.column() == 0){ QColor color(51,51,51); QColor background(0xDA,0xDA,0xDA); QStyleOptionViewItemV2 opt = setOptions(index, option); opt.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter; opt.palette.setColor(QPalette::Text,color); opt.font.setBold(true); painter->fillRect(option.rect,background); painter->setBackground(background); QItemDelegate::paint(painter, opt, index); }else if(index.column() == 1){ 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); } }
void ItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { if (index.row()!=4) QStyledItemDelegate::paint(painter,option,index); else { int checked=index.model()->data(index,Qt::DisplayRole).toInt(); QStyleOptionButton*checkBoxOption=new QStyleOptionButton(); if (checked!=2) checkBoxOption->state|=QStyle::State_Enabled; if (checked) checkBoxOption->state|=QStyle::State_On; else checkBoxOption->state|=QStyle::State_Off; checkBoxOption->rect=CheckBoxRect(option); QApplication::style()->drawControl(QStyle::CE_CheckBox,checkBoxOption,painter); } }
void table_widget_delegate::paintCheckbox(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { bool checked = index.model()->data(index, Qt::EditRole).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); }
bool ItemDelegate::editorEvent(QEvent *event,QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if (index.row()!=4) return QStyledItemDelegate::editorEvent(event,model,option,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) { QKeyEvent*key_event=static_cast<QKeyEvent*>(event); if (key_event->key()!=Qt::Key_Space&&key_event->key()!=Qt::Key_Select) return false; } else return false; int checked=index.model()->data(index,Qt::DisplayRole).toInt(); return model->setData(index,checked<=1?!checked:true,Qt::DisplayRole); }
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ //获取值 int checked = index.model()->data(index, Qt::DisplayRole).toInt(); //按钮的风格选项 QStyleOptionButton *checkBoxOption = new QStyleOptionButton(); checkBoxOption->state |= QStyle::State_Enabled; //根据值判断是否选中 if(checked) { checkBoxOption->state |= QStyle::State_On; } else { checkBoxOption->state |= QStyle::State_Off; } //返回QCheckBox几何形状 checkBoxOption->rect = CheckBoxRect(option); //绘制QCheckBox QApplication::style()->drawControl(QStyle::CE_CheckBox,checkBoxOption,painter); }
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyledItemDelegate::paint(painter, option, QModelIndex()); rqt_log_viewer::Tristate tristate; QVariant dataVariant = index.data(Qt::DisplayRole); if(dataVariant.type() == QVariant::Bool) tristate = dataVariant.toBool() ? rqt_log_viewer::TRISTATE_TRUE : rqt_log_viewer::TRISTATE_FALSE; else tristate = (rqt_log_viewer::Tristate)dataVariant.toInt(); QStyleOptionButton check_box_style_option; check_box_style_option.state |= QStyle::State_Enabled; switch(tristate) { case rqt_log_viewer::TRISTATE_INVALID: return; case rqt_log_viewer::TRISTATE_FALSE: check_box_style_option.state |= QStyle::State_Off; break; case rqt_log_viewer::TRISTATE_TRUE: check_box_style_option.state |= QStyle::State_On; break; case rqt_log_viewer::TRISTATE_TRISTATE: check_box_style_option.state |= QStyle::State_NoChange; break; } check_box_style_option.rect = CheckBoxRect(option); QApplication::style()->drawControl( QStyle::CE_CheckBox, &check_box_style_option, painter ); }
void checkBoxEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { // bool checked = index.model()->data(index, Qt::DisplayRole).toBool(); QStyleOptionButton combo_box_style_option; // combo_box_style_option.state |= QStyle::State_Enabled; // if (checked) { // combo_box_style_option.state |= QStyle::State_On; // } else { // combo_box_style_option.state |= QStyle::State_Off; // } combo_box_style_option.rect = CheckBoxRect(option); QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &combo_box_style_option, painter); if(index.model()->data(index, Qt::DisplayRole).toString()=="0") painter->drawText(option.rect.x()+3,option.rect.y(),option.rect.width(),option.rect.height(),Qt::AlignVCenter,"Non-Joinor"); else painter->drawText(option.rect.x()+3,option.rect.y(),option.rect.width(),option.rect.height(),Qt::AlignVCenter,"Joinor"); }
bool checkBoxEditDelegate::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) { if ((event->type() == QEvent::MouseButtonRelease) ||(event->type() == QEvent::MouseButtonPress) || (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) {qDebug()<<"asdas"; 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; } return model->setData(index, model->data(index).toString(), Qt::EditRole); }
static void paintCheckbox(const QStyledItemDelegate* delegate, QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) { QVariant value = index.model()->data(index, Qt::DisplayRole); delegate->QStyledItemDelegate::paint(painter, option, index); if(!value.isValid()) return; 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); }
QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const { return CheckBoxRect(option).size(); }