QTableWidget和setCellWidget:调整大小问题

2024-05-03 23:34:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在QTableWidget的某个单元格中插入一些QRadioButton。情况与this post类似。具体来说,@eyllansc与PySide2的解决方案如下

import sys

from PySide2.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, \
    QButtonGroup, QRadioButton

app = QApplication(sys.argv)

searchView = QTableWidget(0, 4)
colsNames = ['A', 'B', 'C']
searchView.setHorizontalHeaderLabels(['DIR'] + colsNames)
dirNames = {'A': ['/tmp', '/tmp/dir1'], 'B': ['/tmp/dir2'],
            'C': ['/tmp/dir3']}

rowCount = sum(len(v) for (name, v) in dirNames.items())
searchView.setRowCount(rowCount)

index = 0

for letter, paths in dirNames.items():
    for path in paths:
        it = QTableWidgetItem(path)
        searchView.setItem(index, 0, it)
        group = QButtonGroup(searchView)
        for i, name in enumerate(colsNames):
            button = QRadioButton()
            group.addButton(button)
            searchView.setCellWidget(index, i + 1, button)
            if name == letter:
                button.setChecked(True)
        index += 1

searchView.show()
sys.exit(app.exec_())

在调整列或行的大小时,我注意到一种奇怪的行为:当我按下鼠标按钮并调整列或原始列的大小时,QRadioButtons仍然保持在它们的位置,导致一些冲突;然后,当我最终释放鼠标按钮时,每个QRadioButton都会回到它的位置。有没有办法避免aka在调整大小的过程中使QRadioButtons也移动


Tags: nameinimportforindexsysbuttontmp
1条回答
网友
1楼 · 发布于 2024-05-03 23:34:11

由于我以前的解决方案会产生其他问题,因此在此解决方案中,我将展示另一种备选方案:

  • 使用Qt::CheckStateRole通过委托实现排除逻辑
  • QProxyStyle可用于绘画
import sys

from PySide2.QtCore import Qt, QEvent
from PySide2.QtWidgets import (
    QApplication,
    QTableWidget,
    QTableWidgetItem,
    QStyledItemDelegate,
    QStyle,
    QStyleOptionViewItem,
    QProxyStyle,
)


class RadioButtonDelegate(QStyledItemDelegate):
    def editorEvent(self, event, model, option, index):
        flags = model.flags(index)
        if (
            not (flags & Qt.ItemIsUserCheckable)
            or not (option.state & QStyle.State_Enabled)
            or not (flags & Qt.ItemIsEnabled)
        ):
            return False
        state = index.data(Qt.CheckStateRole)
        if state is None:
            return False
        widget = option.widget
        style = widget.style() if widget is not None else QApplication.style()
        # make sure that we have the right event type
        if (
            (event.type() == QEvent.MouseButtonRelease)
            or (event.type() == QEvent.MouseButtonDblClick)
            or (event.type() == QEvent.MouseButtonPress)
        ):
            viewOpt = QStyleOptionViewItem(option)
            self.initStyleOption(viewOpt, index)
            checkRect = style.subElementRect(
                QStyle.SE_ItemViewItemCheckIndicator, viewOpt, widget
            )
            me = event
            if me.button() != Qt.LeftButton or not checkRect.contains(me.pos()):
                return False
            if (event.type() == QEvent.MouseButtonPress) or (
                event.type() == QEvent.MouseButtonDblClick
            ):
                return True
        else:
            return False

        if state != Qt.Checked:
            for c in range(model.columnCount()):
                if c not in (0, index.column()):
                    ix = model.index(index.row(), c)
                    model.setData(ix, Qt.Unchecked, Qt.CheckStateRole)
            return model.setData(index, Qt.Checked, Qt.CheckStateRole)
        return False


class RadioStyle(QProxyStyle):
    def drawPrimitive(self, element, option, painter, widget=None):
        if element == QStyle.PE_IndicatorItemViewItemCheck:
            element = QStyle.PE_IndicatorRadioButton
        super().drawPrimitive(element, option, painter, widget)


app = QApplication(sys.argv)

searchView = QTableWidget(0, 4)
style = RadioStyle(searchView.style())
searchView.setStyle(style)

delegate = RadioButtonDelegate(searchView)
searchView.setItemDelegate(delegate)

colsNames = ["A", "B", "C"]
searchView.setHorizontalHeaderLabels(["DIR"] + colsNames)
dirNames = {"A": ["/tmp", "/tmp/dir1"], "B": ["/tmp/dir2"], "C": ["/tmp/dir3"]}

rowCount = sum(len(v) for (name, v) in dirNames.items())
searchView.setRowCount(rowCount)

index = 0

for letter, paths in dirNames.items():
    for path in paths:
        it = QTableWidgetItem(path)
        searchView.setItem(index, 0, it)
        for i, name in enumerate(colsNames):
            it = QTableWidgetItem()
            searchView.setItem(index, i + 1, it)
            it.setCheckState(Qt.Checked if name == letter else Qt.Unchecked)
        index += 1

searchView.show()
sys.exit(app.exec_())

相关问题 更多 >