Here a small example showing the painting of a button in the delegate.
You can adapt it to your needs (i.e. draw two buttons instead one and detect which button should be down when pressed)
import sys
from PySide6.QtCore import QAbstractItemModel
from PySide6.QtCore import QEvent
from PySide6.QtCore import QModelIndex
from PySide6.QtCore import Signal
from PySide6.QtGui import QMouseEvent
from PySide6.QtGui import QStandardItem
from PySide6.QtGui import QStandardItemModel
from PySide6.QtWidgets import QApplication
from PySide6.QtWidgets import QStyle
from PySide6.QtWidgets import QStyledItemDelegate
from PySide6.QtWidgets import QStyleOptionButton
from PySide6.QtWidgets import QStyleOptionViewItem
from PySide6.QtWidgets import QTableView
class Delegate(QStyledItemDelegate):
clicked = Signal(QModelIndex)
def __init__(self, parent=None) -> None:
super().__init__(parent)
self._is_down = False
def _build_option(self, option, index: QModelIndex) -> QStyleOptionButton:
pb_style = QStyleOptionButton()
pb_style.state = option.state
pb_style.state |= QStyle.State_Sunken if self._is_down else QStyle.State_Raised
pb_style.fontMetrics = option.fontMetrics
pb_style.rect = option.rect
pb_style.text = index.data()
return pb_style
def editorEvent(
self,
event: QEvent,
model: QAbstractItemModel,
option: QStyleOptionViewItem,
index: QModelIndex,
) -> bool:
if isinstance(event, QMouseEvent):
if event.type() == QEvent.MouseButtonPress:
self._is_down = True
elif event.type() == QEvent.MouseButtonRelease:
self._is_down = False
self.clicked.emit(index)
return True
return False
def paint(self, painter, option, index) -> None:
button_option = self._build_option(option, index)
qApp.style().drawControl(
QStyle.CE_PushButton, button_option, painter, self.parent()
)
def show_text(index: QModelIndex) -> None:
print(index.data())
if __name__ == "__main__":
app = QApplication(sys.argv)
view = QTableView()
model = QStandardItemModel(3, 2)
for row in range(model.rowCount()):
for column in range(model.columnCount()):
item = QStandardItem(f"row {row}, column {column}")
model.setItem(row, column, item)
view.setModel(model)
delegate = Delegate(view)
view.setItemDelegateForColumn(1, delegate)
view.show()
delegate.clicked.connect(show_text)
sys.exit(app.exec())