MaPo
MaPo

Reputation: 855

QTableWidget and setCellWidget: resize problem

I tried to insert some QRadioButton inside some cell of a QTableWidget. The situation is similar to the one of this post. In particular, the solution by @eyllanesc, with PySide2 is the following

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_())

When resizing the columns or rows, I notice a weird behavior: while I'm pressing the mouse button and resizing the column or the raw, the QRadioButtons remain still at their places, causing some clashes; then, when I finally release the mouse button, every QRadioButton come to its place. Is there a way to avoid that aka to make the QRadioButtons move as well during the resizing process?

Upvotes: 3

Views: 641

Answers (2)

Adam Sirrelle
Adam Sirrelle

Reputation: 397

I had this problem and managed to fix it with a fairly straightforward solution.

Create a connection from your QTableWidget's QHeader's sectionResized signal to a custom method (_refresh_row_size in my case).

self.table.horizontalHeader().sectionResized.connect(self._refresh_row_size)
self.table.verticalHeader().sectionResized.connect(self._refresh_row_size)

I only wanted the first row and first column to get resized, as that is where I have inserted a QCheckbox. This is what I used:

def _refresh_row_size(self, logicalIndex, oldSize, newSize):

    self.table.rowResized(0, oldSize, newSize)
    self.table.columnResized(0, oldSize, newSize)

    return

For extra context, this is the QTableItem I added to the QTable:

# Create checkbox widget
self.widget = QtWidgets.QWidget()
self.checkbox = QtWidgets.QCheckBox(self.widget)

self.layout = QtWidgets.QHBoxLayout(self.widget)
self.layout.addWidget(self.checkbox)
self.layout.setAlignment(QtCore.Qt.AlignCenter)
self.layout.setContentsMargins(0, 0, 0, 0)

# Add new row to table
row_position = self.table.rowCount()
self.table.insertRow(row_position)

# Create new table item, add item, add widget
item = QtWidgets.QTableWidgetItem()

self.table.setItem(0, 0, item)
self.table.setCellWidget(0, 0, self.widget)

Upvotes: 0

eyllanesc
eyllanesc

Reputation: 244291

Since my previous solution generates other problems then in this solution I will show another alternative:

  • Implement the logic of exclusion through a delegate using the Qt :: CheckStateRole.
  • A QProxyStyle can be used for painting
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_())

Upvotes: 3

Related Questions