我想连接PyQtGraph鼠标滚轮缩放功能的QSlider小部件。所以当我放大/缩小图形时,我会得到viewbox的范围,滑动窗口应该在滑动范围内跟随。我希望它是怎样的最接近的例子可以在这个PyQtGraph例子中找到:http://www.pyqtgraph.org/downloads/0.10.0/pyqtgraph-0.10.0-deb/pyqtgraph-0.10.0/examples/crosshair.py
所以我想以某种方式把下面的定义联系起来。你知道吗
def update_plot(self):
self.axX = self.p6.getAxis('bottom')
self.xmin = self.axX.range[0]
self.xmax = self.axX.range[0]
print(self.axX.range)
return xmin, xmax
def update_slider(self, xmin, xmax):
self.size = self.w1.slider.value()
self.p6.setXRange(self.xmin+self.size,self.xmax+self.size)
print(self.size)
然而,我似乎无法让它工作。我在下面附上了我的例子的完整代码。你能帮我什么忙吗?你知道吗
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QHBoxLayout, QLabel, QSizePolicy, QSlider, QSpacerItem, \
QVBoxLayout, QWidget
import pyqtgraph as pg
import numpy as np
class Slider(QWidget):
def __init__(self, minimum, maximum, parent=None):
super(Slider, self).__init__(parent=parent)
self.verticalLayout = QVBoxLayout(self)
self.label = QLabel(self)
self.verticalLayout.addWidget(self.label)
self.horizontalLayout = QHBoxLayout()
spacerItem = QSpacerItem(0, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem)
self.slider = QSlider(self)
self.slider.setOrientation(Qt.Vertical)
self.horizontalLayout.addWidget(self.slider)
spacerItem1 = QSpacerItem(0, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
self.horizontalLayout.addItem(spacerItem1)
self.verticalLayout.addLayout(self.horizontalLayout)
self.resize(self.sizeHint())
self.minimum = minimum
self.maximum = maximum
self.slider.valueChanged.connect(self.setLabelValue)
self.x = None
self.setLabelValue(self.slider.value())
def setLabelValue(self, value):
self.x = self.minimum + (float(value) / (self.slider.maximum() - self.slider.minimum())) * (
self.maximum - self.minimum)
self.label.setText("{0:.4g}".format(self.x))
class Widget(QWidget):
def __init__(self, parent=None):
super(Widget, self).__init__(parent=parent)
self.horizontalLayout = QHBoxLayout(self)
# plot (p6)
self.win = pg.GraphicsWindow(title="Basic plotting examples")
self.horizontalLayout.addWidget(self.win)
self.p6 = self.win.addPlot(title="My Plot")
x = np.arange(1e5)
self.y1 = np.random.randn(x.size)
self.p6.plot(self.y1, pen="r")
self.p6.setMouseEnabled(x=True, y=False)
self.p6.setXRange(0,300)
self.p6.setLimits(xMin=0, xMax=len(self.y1))
self.p6.sigRangeChanged.connect(self.update_plot)
# slider (w1)
self.w1 = Slider(0, len(self.y1))
self.horizontalLayout.addWidget(self.w1)
self.w1.slider.setMinimum(0)
self.w1.slider.setMaximum(len(self.y1))
self.w1.slider.valueChanged.connect(self.update_slider)
def update_plot(self):
self.axX = self.p6.getAxis('bottom')
self.xmin = self.axX.range[0]
self.xmax = self.axX.range[0]
print(self.axX.range)
return self.xmin, self.xmax
def update_slider(self, xmin, xmax):
self.size = self.w1.slider.value()
self.p6.setXRange(self.xmin+self.size,self.xmax+self.size)
print(self.size)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = Widget()
w.show()
sys.exit(app.exec_())
首先,你首先问的问题实际上对StackOverflow的“行为”更有用。在编辑之后,您只留下了一个pyqtgraph示例中的无用的纯拷贝/粘贴。你知道吗
不要那样做。你知道吗
人们很少经过编辑,看到你问题的现状,他们会认为你根本没有努力去解决你的问题(导致他们忽视你的问题)。我建议您撤消编辑,或者至少恢复原始问题示例代码。你知道吗
您的问题的主要问题是缩放不是线性。从普通用户的角度来看,缩放滑块的每一步都应该增加或减少一个缩放因子(pyqtgraph实际上是这样做的)。你知道吗
滑块通常是一个线性接口,这意味着它的每一步都应该与其上一步或下一步成相等比例。
简单地说,如果滑块的“中间步骤”等于图像的原始大小,并且“下一步”等于两倍大小,则用户期望下一步将导致大小再次加倍。显然,同样的方法也适用于相反的情况。你知道吗
把它想象成一个spinbox,甚至是几个
+/-
按钮。你知道吗一个可能的解决方案是实现一个考虑到所有这些的滑块。你知道吗
相关问题 更多 >
编程相关推荐