【问题标题】:PyQt TableView align Icons to CenterPyQt TableView 将图标对齐到中心
【发布时间】:2020-11-20 11:01:42
【问题描述】:

我有一个带有图标的 QTableView,这些图标默认显示在左侧,而我想将它们对齐到中心。从 Qt 文档中,对齐仅发生在 Qt.role 下的 TextAlignment 中,它仅具有 Qt.DisplayRole() 的作用。

如何将具有 Qt.DecorationRole() 的图标设置为中心。

【问题讨论】:

    标签: python pyqt qtableview


    【解决方案1】:

    一种可能的解决方案是使用委托:

    class IconDelegate(QtWidgets.QStyledItemDelegate):
        def initStyleOption(self, option, index):
            super().initStyleOption(option, index)
            option.decorationSize = option.rect.size()
    
    delegate = IconDelegate(tableview)
    tableview.setItemDelegate(delegate)
    

    【讨论】:

    • 这成功了。谢谢。
    猜你喜欢
    • 2015-11-29
    • 2020-05-13
    • 2019-01-06
    • 1970-01-01
    • 2016-08-13
    • 2013-05-30
    • 1970-01-01
    • 2011-11-10
    • 2010-12-16
    相关资源
    最近更新 更多