QValueAxis показать грязный код (китайский язык)? - PullRequest
1 голос
/ 01 ноября 2019

на моем графике ось X должна отображать китайский язык, а ось Y - английский, а ось X показывает грязный код. кто-нибудь может мне помочь?

self.chart.createDefaultAxes()
axis_x, axis_y = self.chart.axes()
axis_x.setLabelFormat('%.2f分')
axis_y.setLabelFormat('%dmA')

это выглядит так: enter image description here

1 Ответ

1 голос
/ 01 ноября 2019

Кажется, что есть несовместимость между кодировщиками, которые QString использует в QtCharts и str (даже выясняя причину проблемы), но мне удалось реализовать решение, которое выполняет преобразование:

import random
from PyQt5 import QtCore, QtWidgets, QtChart


def convert(word):
    return "".join(chr(e) for e in word.encode())


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        series = QtChart.QLineSeries(name="random serie")

        for i in range(20):
            series << QtCore.QPointF(0.1 * i, random.uniform(-10, 10))

        self.chart = QtChart.QChart()
        self.chart.setTitle("Title")
        self.chart.addSeries(series)
        self.chart.createDefaultAxes()
        axis_x, axis_y = self.chart.axes()
        axis_x.setLabelFormat(convert("%.2f分"))
        axis_y.setLabelFormat("%dmA")

        view = QtChart.QChartView(self.chart)
        self.setCentralWidget(view)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)

    w = MainWindow()
    w.resize(640, 240)
    w.show()
    sys.exit(app.exec_())

enter image description here

...