我完全陷入了两个问题:
1)我正在使用QSlider设置一些值(它们浮动~0.5,所以我使用* 1000)。 SingleStep和PageStep可以很好地用于键盘输入和鼠标滚轮,所有设置的滴答......但是当我使用鼠标拖动滑块时 - 它会忽略所有这些刻度,步骤等,我希望它只能从一个勾到另一个。
self.ui.x_coord.setMaximum(l_d*1000)
self.ui.x_coord.setSingleStep(l_d/N*1000)
self.ui.x_coord.setTickInterval(l_d/N*1000)
self.ui.x_coord.setTickPosition(QtGui.QSlider.TicksBothSides)
self.ui.x_coord.setPageStep(l_d/N * 10000)
是我的代码中缺少的东西(可能像setMouseStep一样)?
2)QSlider连接到一个函数
self.graph = BPlot(self.ui)
self.ui.x_coord.valueChanged.connect(self.setCoordLabelValue)
....
def setCoordLabelValue(self):
x = self.ui.x_coord.value()/1000
y = self.ui.y_coord.value()/1000
self.graph.setCoordText(x,y)
....
class BPlot(QtGui.QGraphicsView):
def __init__(self, ui, parent=None):
super(BPlot, self).__init__(parent)
self.scene = QtGui.QGraphicsScene()
self.ui = ui
self.coordText = QtGui.QGraphicsTextItem(None, self.scene)
self.coordText.setPlainText("123")
self.x_offset = 40
self.y_offset = 20
self.currentPoint = QtGui.QGraphicsRectItem(None, self.scene)
self.cph = 4
self.cpw = 4
def resizeEvent(self, event):
size = event.size()
def showEvent(self, event):
aw = self.viewport().width()
ah = self.viewport().height()
self.scene.setSceneRect(0,0,aw,ah)
self.setScene(self.scene)
self.axis_pen = QtGui.QPen(QtCore.Qt.DashDotLine)
self.scene.addLine(0, 3/4*ah, aw, 3/4*ah, self.axis_pen)
self.normal_pen = QtGui.QPen(QtCore.Qt.SolidLine)
self.scene.addLine(self.x_offset, 3/4*ah - self.y_offset, aw - self.x_offset, 3/4*ah - self.y_offset)
self.currentPoint.setRect(self.x_offset - self.cpw/2, 3/4*ah - self.y_offset - self.cph/2, self.cpw, self.cph)
def setCoordText(self, x, y):
self.coordText.setPlainText(str(x) + ":" + str(y))
问题是setCoordText func不会重绘coordText。如果我将使用print(coordText.toPlainText()) - 我会得到正确的输出,但在屏幕上仍然会有来自__init __
的“123”我已经尝试将self.scene.update()添加到setCoordText的末尾,但没有运气。
答案 0 :(得分:0)
呃......解决了。逻辑,你好,我亲爱的朋友???
def setCoordLabelValue(self):
x = self.ui.x_coord.value()/1000
y = self.ui.y_coord.value()/1000
self.graph.setCoordText(x,y)
self.graph.invalidateScene()
.......
def paintEvent(self, event):
painter = QtGui.QPainter(self.viewport())
# set color and width of line drawing pen
painter.setPen(QtGui.QPen(QtCore.Qt.black, 2))
# drawLine(x1, y1, x2, y2) from point (x1,y1) to (x2,y2)
# draw the baseline
painter.drawText(10,20,str(x_coord))
# set up color and width of the bars
width = 20
painter.setPen(QtGui.QPen(QtCore.Qt.red, width))
delta = width + 5
x = 30
painter.end()
self.viewport().update()