gpt4 book ai didi

python - 如何将QChart坐标映射到QChartView点?

转载 作者:太空宇宙 更新时间:2023-11-03 14:05:35 25 4
gpt4 key购买 nike

我想在QChart中制作一个橡皮筋窗口,但我不知道如何将QChart的坐标映射到QChartView。

例如(0, 0) 是一个 QChart 坐标,也许在 QChartView 中是 (10, 10),如何转换呢?我看到了Get mouse coordinates in QChartView's axis system的回答

但是好像和我的要求相反。

import sys
from PyQt5.QtChart import QChart, QChartView, QLineSeries
from PyQt5.QtCore import QPointF, QEvent, QPoint, QRect
from PyQt5.QtWidgets import QApplication, QMainWindow, QRubberBand

class ChartView(QChartView):
def init_rubber_band(self):
chart = self.chart()

start1 = chart.mapToPosition(QPointF(0, 0))
start2 = chart.mapToScene(start1)
start3 = QPoint()
start3.setX(int(start2.x()))
start3.setY(int(start2.y()))

end = QPoint(200, 200)

self.rubber = QRubberBand(QRubberBand.Rectangle, self);
self.rubber.setGeometry(QRect(start3, end))
self.rubber.show()


app = QApplication(sys.argv)
series0 = QLineSeries()
series1 = QLineSeries()

series0 << QPointF(1, 15) << QPointF(3, 17) << QPointF(7, 16) << QPointF(9, 17) \
<< QPointF(12, 16) << QPointF(16, 17) << QPointF(18, 15)
series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) \
<< QPointF(12, 3) << QPointF(16, 4) << QPointF(18, 3)

chart = QChart()
chart.addSeries(series0)
chart.addSeries(series1)
chart.createDefaultAxes()
chartView = ChartView(chart)

chartView.init_rubber_band()

window = QMainWindow()
window.setCentralWidget(chartView)
window.resize(400, 300)
window.show()

sys.exit(app.exec_())

我使用mapToPosition来获取原点坐标,但似乎不起作用。

最佳答案

如果您想执行一些计算,则必须在小部件显示后执行,并且由于 QChartView 只是重新计算项目,在下面的示例中我将创建一个 QRubberBand位于一个矩形上,其坐标与 QChart 相关,从 QPointF(1, 17)QPointF(18, 2)

class ChartView(QChartView):
def init_rubber_band(self):
chart = self.chart()
start_value = QPointF(1, 17)
end_value = QPointF(18, 2)
start = chart.mapToPosition(start_value)
end = chart.mapToPosition(end_value)

self.rubber = QRubberBand(QRubberBand.Rectangle, self)
self.rubber.setGeometry(QRect(start.toPoint(), end.toPoint()))
self.rubber.show()


app = QApplication(sys.argv)
series0 = QLineSeries()
series1 = QLineSeries()

series0 << QPointF(1, 15) << QPointF(3, 17) << QPointF(7, 16) << QPointF(9, 17) \
<< QPointF(12, 16) << QPointF(16, 17) << QPointF(18, 15)
series1 << QPointF(1, 3) << QPointF(3, 4) << QPointF(7, 3) << QPointF(8, 2) \
<< QPointF(12, 3) << QPointF(16, 4) << QPointF(18, 3)

chart = QChart()
chart.addSeries(series0)
chart.addSeries(series1)
chart.createDefaultAxes()
chartView = ChartView(chart)

window = QMainWindow()
window.setCentralWidget(chartView)
window.resize(400, 300)
window.show()

chartView.init_rubber_band()

sys.exit(app.exec_())

输出:

enter image description here

关于python - 如何将QChart坐标映射到QChartView点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48919092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com