Qt QTableView - Alignment of checkbox when using IsUserCheckable

南楼画角 提交于 2020-01-13 08:51:07

问题


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

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?


回答1:


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.

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

and

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

Also see this similar question here...




回答2:


Probably not the answer you're looking for, however I found it much easier to implement my own checkbox item delegate when using qtableviews.




回答3:


TextAlignmentRole really does mean what it says. Unfortunately, as you probably noticed, there doesn't seem to be any Icon/Widget alignment role available at all.

Bug report: http://bugreports.qt-project.org/browse/QTBUG-9047

Same question with some answers: http://lists.trolltech.com/qt-interest/2006-06/msg00476.html




回答4:


Also you can look at this thread: http://www.qtcentre.org/threads/19157-QTableView-checkbox-center-with-stylesheet?p=181413#post181413




回答5:


Solution for Python (PySide, PyQt) to center the checkbox and with editable allowed:

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)

In your table model, make sure that the flags allow the user to check/uncheck the cell.

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)

Finally, set the BooleanDelagate in your table

self.boolean_delegate = BooleanDelegate()
self.input_gui.setItemDelegateForColumn(5, self.boolean_delegate)


来源:https://stackoverflow.com/questions/4403704/qt-qtableview-alignment-of-checkbox-when-using-isusercheckable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!