本文介绍了PyQt5 QTablewidget 项目对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要的是对齐 Qtablewidget 中的复选框.这是我的脚本喜欢的

what I want is to align my checkbox which is in Qtablewidget.here is what my script like

mylist = [some list I made..]
for i in range(len(mylist)) :
    row_count = self.tableWidget.rowCount()
    nowset = row_count+1
    self.tableWidget.setRowCount(nowset)
    self.tableWidget.setItem( row_count, 0, QTableWidgetItem(mylist[i]))
    chkBoxItem = QTableWidgetItem()
    chkBoxItem.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)
    chkBoxItem.setCheckState(QtCore.Qt.Unchecked)
    self.tableWidget.setItem( i, 1, chkBoxItem)

self.tableWidget.setFocusPolicy(False)
self.tableWidget.setColumnWidth(1, 150)
self.tableWidget.horizontalHeader().setSectionResizeMode(0, QHeaderView.Stretch)

这里是问题 1. 如何将我的 QtableWidget 项目对齐到中心.

通过谷歌搜索,我发现 cellwidget 确实有效.所以我尝试将代码更改为 cellWidget 而不是 Item.就像

from some googling I found that cellwidget does work. soI tried changing my code to to cellWidget not Item.as like

self.checkBoxList= []
for i in range(len(mylist)):
    ckbox = QCheckBox()
    self.checkBoxList.append(ckbox)

for i in range(len(mylist)):
    cellWidget = QWidget()
    layoutCB = QHBoxLayout(cellWidget)
    layoutCB.addWidget(self.checkBoxList[i])
    layoutCB.setAlignment(QtCore.Qt.AlignCenter)
    layoutCB.setContentsMargins(0,0,0,0)
    cellWidget.setLayout(layoutCB)
    self.tableWidget.setCellWidget(i,1,cellWidget)

这是我需要的对齐复选框.但让其他事情成为问题.

this those align checkbox as I need. but make other things to problem.

问题 2. 使用 cellwidget,如何将所有 chekbox cellwidget 设置为选中状态.

Qestionn 3. 使用 cellwidget,我怎么知道检查了哪一行.

问题 4. 使用 cellwidget,如何更改单元格颜色.

似乎itemcellwidget 的工作方式不同.

it seems item and cellwidget works differently.

因为我有将所有复选框更改为选中状态的按钮

because I have button that change all checkbox to checked and

另一个获取第 0 列文本的按钮,该按钮选中了同一行的复选框.

another button that get column 0's text which same row's checkbox is checked.

我也尝试过这样的委托.

I also tried delegate like this.

class AlignDelegate(QtWidgets.QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super(AlignDelegate, self).initStyleOption(option, index)
        option.displayAlignment = QtCore.Qt.AlignCenter

并将下面的代码放在上面self.tableWidget.setFocusPolicy(False)

and put below code at above self.tableWidget.setFocusPolicy(False)

delegate = AlignDelegate(self.tableWidget)
self.tableWidget.setItemDelegateForColumn(1, delegate)

不知道为什么.但不起作用.问题 5. 为什么不起作用?

don't know why. but doen't work.Question 5. why doesn't work?

这是我的程序截图.

如果你能解决问题 1,那对我来说就足够了.

if you can solve Question 1. that would be enough for me.

因为其他答案会让我更改以下所有代码.

because other answer will make me to change all following code.

其他问题只是告诉我尝试过,但它们会带来更多问题.

other questions are just telling I tried and they make more problem.

推荐答案

如果要使复选框居中,则必须使用 QProxyStyle:

If you want to center the checkbox then you must use a QProxyStyle:

from PyQt5 import QtCore, QtGui, QtWidgets


class CheckBoxStyle(QtWidgets.QProxyStyle):
    def subElementRect(self, element, option, widget=None):
        r = super().subElementRect(element, option, widget)
        if element == QtWidgets.QStyle.SE_ItemViewItemCheckIndicator:
            r.moveCenter(option.rect.center())
        return r


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        data = ["1", "2", "3", "4", "5", "6"]

        self.tableWidget = QtWidgets.QTableWidget(0, 2)
        self.setCentralWidget(self.tableWidget)

        for text in data:
            row = self.tableWidget.rowCount()
            self.tableWidget.insertRow(row)

            text_item = QtWidgets.QTableWidgetItem(text)

            checkbox_item = QtWidgets.QTableWidgetItem()
            checkbox_item.setFlags(
                QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled
            )
            checkbox_item.setCheckState(QtCore.Qt.Unchecked)

            self.tableWidget.setItem(row, 0, text_item)
            self.tableWidget.setItem(row, 1, checkbox_item)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    checkbox_style = CheckBoxStyle(app.style())
    app.setStyle(checkbox_style)

    w = MainWindow()
    w.show()

    sys.exit(app.exec_())

这篇关于PyQt5 QTablewidget 项目对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-16 05:07