简体   繁体   English

PyQt-关于QSlider和QGraphicsTextItem的问题

[英]PyQt - questions about QSlider and QGraphicsTextItem

I'm totally stuck with 2 problems: 我完全陷入两个问题:

1) i'm using QSlider to set some values (they're float ~0.5, so i'm using *1000). 1)我正在使用QSlider设置一些值(它们的浮点值为〜0.5,所以我使用的是* 1000)。 SingleStep and PageStep work fine for keyboard input and mouse wheel, ticks're all set... But when i'm using mouse to drag the slider - it ignores all those ticks, steps, etc, and i want it to move only from one tick to another. 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)

is something missing here in my code (maybe smth like setMouseStep)? 我的代码中这里缺少什么(也许像setMouseStep这样)?

2) That QSlider's connected to a function 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))

the problem is that setCoordText func doesn't redraw coordText. 问题是setCoordText函数不会重绘coordText。 If i'll use print(coordText.toPlainText()) - i'll get the right output, but on the screen there'll be still "123" from __init__ 如果我将使用print(coordText.toPlainText())-我将获得正确的输出,但是在屏幕上,__ init__仍为“ 123”

I already tried adding self.scene.update() to the end of setCoordText, with no luck. 我已经尝试将self.scene.update()添加到setCoordText的末尾,但是没有运气。

Ugh... Solved. gh ...解决了。 Logic, where're u, my dear friend??? 逻辑,亲爱的朋友,你在哪里???

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM