gpt4 book ai didi

Qt QTableView - 使用 IsUserCheckable 时复选框的对齐方式

转载 作者:行者123 更新时间:2023-12-04 07:28:57 36 4
gpt4 key购买 nike

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

在阅读了一些关于对齐的内容以尝试使单元格内的复选框居中之后,我将 Qt::AlignCenter 作为模型 data() 函数中的 TextAlignmentRole 返回。

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

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

然而,这并没有对齐我的复选框。

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

最佳答案

Python(PySide,PyQt)将复选框居中并允许可编辑的解决方案:

class BooleanDelegate(QItemDelegate):

def __init__(self, *args, **kwargs):
super(BooleanDelegate, self).__init__(*args, **kwargs)

def paint(self, painter, option, index):
# Depends on how the data function of your table model is implemented
# 'value' should recive a bool indicate if the checked value.
value = index.data(Qt.CheckStateRole)
self.drawCheck(painter, option, option.rect, value)
self.drawFocus(painter, option, option.rect)

def editorEvent(self, event, model, option, index):
if event.type() == QEvent.MouseButtonRelease:
value = bool(model.data(index, Qt.CheckStateRole))
model.setData(index, not value)
event.accept()
return super(BooleanDelegate, self).editorEvent(event, model, option, index)

在您的表格模型中,确保标志允许用户选中/取消选中单元格。
class MyTableModel(QAbstractTableModel):

...

def flags(self, index):
if not index.isValid():
return Qt.ItemIsEnabled
if index.column() in self.columns_boolean:
return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable
return Qt.ItemFlags(QAbstractTableModel.flags(self, index) | Qt.ItemIsEditable)

最后,设置 BooleanDelagate在你的 table 上
self.boolean_delegate = BooleanDelegate()
self.input_gui.setItemDelegateForColumn(5, self.boolean_delegate)

关于Qt QTableView - 使用 IsUserCheckable 时复选框的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4403704/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com