Qt QTableView-使用IsUserCheckable时复选框的对齐 [英] Qt QTableView - Alignment of checkbox when using IsUserCheckable

查看:524
本文介绍了Qt QTableView-使用IsUserCheckable时复选框的对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用QTableView的 Qt :: ItemIsUserCheckable 复选框标记在表格单元格中显示一个复选框。

I am using QTableView's checkbox flag of Qt::ItemIsUserCheckable to display a checkbox in a table cell.

为了使复选框在单元格内居中,我正在从模型data()函数返回 Qt :: AlignCenter 作为TextAlignmentRole。

After reading some things on alignment in an attempt to center the checkbox within the cell, I am returning the Qt::AlignCenter as the TextAlignmentRole from the models data() function.

QVariant ExampleModel::data(const QModelIndex &index, int role) const 
{
  if(!index.isValid())
     return QVariant();

  if (role == Qt::TextAlignmentRole) 
       return Qt::AlignCenter | Qt::AlignVCenter;
}

但这与我的复选框不符。

This however is not aligning my checkbox.

有人知道这种模式是如何对齐复选框的吗?

Does anyone know how to align checkboxes is this mode?

推荐答案

在进一步研究了委托选项之后,我找到了一个不错的参考(遗憾的是不再可用),并使用QItemDelegate和IsUserCheckable提出了以下混合形式。

After further investigation into delegate options I found a nice reference (unfortunately no longer available) and came up with the following hybrid using a QItemDelegate and IsUserCheckable.

本质上,您需要扩展QItemDelegate并重新实现,使用 drawCheck 函数居中并使用 editorEvent 处理鼠标和键盘事件,同时将模型设置为适当的状态。

Essentially, you need to extend QItemDelegate, and reimplement, using the drawCheck function to center and use the editorEvent to handle mouse and keyboard events while setting the model with the appropriate state.

void drawCheck(QPainter* painter, QStyleOptionViewItem const& option, QRect const& rect, Qt::CheckState state) const

bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)

也可以看到类似的问题此处 ...

Also see this similar question here...

这篇关于Qt QTableView-使用IsUserCheckable时复选框的对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆