Issue
I am use a QAbstractTableModel
model that the horizontal header title need to translate different language.
But when I use pylupdate
and linguist
tool, it seems not working.
So how can I translate the QAbstractTableModel
header when app start when select a different language?
Code Snippet
class Model(QAbstractTableModel):
def __init__(self):
super().__init__()
self._data = np.random.randint(0, 100, size=(10, 4)) # type: np.ndarray
self._columns = ["第一列", "第二列", "第三列", "第四列"]
def headerData(self, section: int, orientation: Qt.Orientation, role: int):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return QApplication.translate("Model", self._columns[section])
def rowCount(self, parent: QModelIndex) -> int:
return 1
def columnCount(self, parent: QModelIndex) -> int:
return self._data.shape[1]
view = QTableView()
view.setModel(Model())
Solution
You have to use QT_TRANSLATE_NOOP
:
class Model(QAbstractTableModel):
def __init__(self, parent=None):
super().__init__(parent)
self._data = np.random.randint(0, 100, size=(10, 4)) # type: np.ndarray
self._columns = [
QT_TRANSLATE_NOOP("Model", "First"),
QT_TRANSLATE_NOOP("Model", "Second"),
QT_TRANSLATE_NOOP("Model", "Third"),
QT_TRANSLATE_NOOP("Model", "Fourth"),
]
def rowCount(self, parent: QModelIndex = QModelIndex()) -> int:
if parent.isValid():
return 0
return self._data.shape[0]
def columnCount(self, parent: QModelIndex = QModelIndex()) -> int:
if parent.isValid():
return 0
return self._data.shape[1]
def headerData(self, section: int, orientation: Qt.Orientation, role: int):
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.tr(self._columns[section])
def data(self, index, role=Qt.DisplayRole):
if role == Qt.DisplayRole:
return str(self._data[index.row()][index.column()])
The complete example is here.
Answered By - eyllanesc
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.