鼠标位置的UI响应更快

时间:2018-01-09 08:16:35

标签: python user-interface pyside lag

我正在创建一个UI来显示鼠标在用户屏幕上的位置。但是我有很大的滞后。

任何人都知道我如何能够提高UI响应的速度?

import sys
from PySide import QtGui, QtCore
import lsl_threads

class mouse_position_viewer(QtGui.QWidget):

    def __init__(self):
        super(mouse_position_viewer, self).__init__()

        self.init_ui()

        self.last_mouse_point = [0,0]

        self.thread = lsl_threads.lsl_thread("Position")
        self.thread.start()
        self.thread.signal.sig.connect(self.got_signal)

        print "Running mouse_position_viewer..."

    def init_ui(self):      

        vbox = QtGui.QVBoxLayout(self)

        self.mouse_position_label = QtGui.QLabel("Mouse: 0, 0")

        self.windows_background = QtGui.QPixmap(300,300)
        self.windows_background.load('images/windows_background.png')
        self.windows_background = self.windows_background.scaledToWidth(int(300))

        self.screen_lbl = QtGui.QLabel(self)
        self.screen_lbl.setPixmap(self.windows_background)

        vbox.addWidget(self.mouse_position_label)
        vbox.addWidget(self.screen_lbl)

        self.setLayout(vbox)

        self.show()

    def got_signal(self,data):

        if data is not None:
            if len(data) == 2:
                self.mouse_position_label.setText("Mouse: " + str(data[0]) + ", " + str(data[1]) )
                self.last_mouse_point = data

  def paintEvent(self, event):  

        qp = QtGui.QPainter()

        #find a better way to do thsi, very slow!!
        #image = QtGui.QPixmap(self.windows_background)
        image = QtGui.QPixmap(self.windows_background)

        qp.begin(image)
        self.drawCursor(qp) #gives it The QPainter property 
        qp.end()

        self.screen_lbl.setPixmap(image)

    def drawCursor(self, qp):

        #Send this over in the meta 
        screen_width = 1800
        screen_height = 1200
        image_width = 300
        image_height = 300

        color = QtGui.QColor(0, 0, 0)
        color.setNamedColor('#d4d4d4')
        qp.setPen(color)

        transfomred_x = self.last_mouse_point[0] / screen_width * image_width
        transfomred_y = self.last_mouse_point[1] / screen_height * image_height

        qp.setBrush(QtGui.QColor(200, 0, 0))
        qp.drawRect(transfomred_x, transfomred_y, 10, 10)


def main():
    app = QtGui.QApplication(sys.argv)

    mainWin = QtGui.QMainWindow()
    mainWin.setWindowTitle("Mouse Position Viewer")
    mainWidget =  mouse_position_viewer()
    mainWin.setCentralWidget(mainWidget)
    mainWin.show()
    app.exec_()

    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

0 个答案:

没有答案