Skip to content

Instantly share code, notes, and snippets.

@anddam
Created November 8, 2021 22:20
Show Gist options
  • Save anddam/23924192a6748a146ea054213a206255 to your computer and use it in GitHub Desktop.
Save anddam/23924192a6748a146ea054213a206255 to your computer and use it in GitHub Desktop.
PySide Qt issue with shifted chars in string when placing a TableWidgetItem in a TableWidget
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QMainWindow, QTableWidgetItem
class CMTableWidgetItemNonEditable(QTableWidgetItem):
"""Unset editing flags
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.setFlags(self.flags() & ~Qt.ItemIsEditable)
class MainWindow(QMainWindow):
# ⋮ Lot of other stuff here
def update_table(self, data):
logger.debug("data=%r", data)
for row, (name, value) in enumerate(data.items()):
# Data come in bytes
name, value = name.decode("utf-8"), value.decode("utf-8")
self.readerInfoTable.insertRow(row)
item_1 = CMTableWidgetItemNonEditable(name)
item_2 = CMTableWidgetItemNonEditable(value)
self.readerInfoTable.setItem(row, 0, item_1)
self.readerInfoTable.setItem(row, 1, item_2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment