如何翻译模型 header 标题?
How to translate a model header title?
我正在使用 QAbstractTableModel
模型,水平 header 标题需要翻译不同的语言。
但是当我使用 pylupdate
和 linguist
工具时,它似乎不起作用。
那么当应用程序启动时 select 不同的语言时,我该如何翻译 QAbstractTableModel
header?
代码段
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())
你必须使用 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()])
完整的例子是here.
我正在使用 QAbstractTableModel
模型,水平 header 标题需要翻译不同的语言。
但是当我使用 pylupdate
和 linguist
工具时,它似乎不起作用。
那么当应用程序启动时 select 不同的语言时,我该如何翻译 QAbstractTableModel
header?
代码段
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())
你必须使用 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()])
完整的例子是here.