问题描述
我想在QChart中创建一个橡皮筋窗口,但我不知道如何将QChart的坐标映射到QChartView.
I want to make a rubber band window in QChart, but I don't know how to map a coordinate of QChart to QChartView.
例如(0,0)是一个QChart坐标,也许它在QChartView中是(10,10),如何转换呢?我已经看到在QChartView的轴系统中获取鼠标坐标的答案
e.g.(0, 0) is a QChart coordinate, maybe it in QChartView is (10, 10), how to transform it?I have saw the answer of Get mouse coordinates in QChartView's axis system
但是看来这与我的要求相反.
But it seems like that it is reversed with my requirement.
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
来获取原始点的坐标,但似乎不起作用.
I use mapToPosition
to get coordinate of original point, but it seems like that it doesn't work.
推荐答案
如果要执行一些计算,则必须在显示小部件之后执行此操作,并且由于QChartView
只是重新计算项目,因此在以下示例中,我将创建在与QChart
从QPointF(1, 17)
到QPointF(18, 2)
If you want to perform some calculation you must do it after the widget is displayed and since QChartView
just recalculates the items, in the following example I will create a QRubberBand
that is on a rectangle with coordinates regarding QChart
from QPointF(1, 17)
to 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_())
输出:
这篇关于如何将QChart坐标映射到QChartView点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!