0

我完全陷入了两个问题:

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 函数不会重绘 coordText。如果我使用 print(coordText.toPlainText()) - 我会得到正确的输出,但在屏幕上仍然会有来自 __init__ 的“123”

我已经尝试将 self.scene.update() 添加到 setCoordText 的末尾,但没有成功。

4

1 回答 1

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()
于 2012-04-29T06:47:43.697 回答