简体   繁体   English

如何在 QtWidgets 应用程序中集成 cursor

[英]How to integrate a cursor in a QtWidgets app

I am new to QtWidgets and trying to build an app in QtWidgets and Python (3.x).我是 QtWidgets 的新手,正在尝试在 QtWidgets 和 Python (3.x) 中构建应用程序。 the end goal of the app is to show images and a superposed cursor (to be exact, a "plus" sign of 2cm) that can be moved along the image reacting to mouse events.该应用程序的最终目标是显示图像和叠加的 cursor(准确地说,是一个 2 厘米的“加号”),它可以响应鼠标事件沿着图像移动。 I concentrate now first on this cursor. So far, I read examples on how to do it on matplotlib. however, i have trouble to understand how to integrate matplotlib on my code.我现在首先关注这个 cursor。到目前为止,我阅读了有关如何在 matplotlib 上执行此操作的示例。但是,我很难理解如何在我的代码中集成 matplotlib。 Also, is matplotlib the easiest way to do it on this code.此外,matplotlib 是在此代码上执行此操作的最简单方法吗? or there might be a better way to do it.或者可能有更好的方法来做到这一点。 any hint would be helpful thank you in advance.任何提示都会有所帮助提前谢谢你。

here is my desired output and the code of my app这是我想要的 output 和我的应用程序代码在此处输入图像描述

import sys
from PySide2 import QtWidgets
from vispy import scene
from PySide2.QtCore import QMetaObject
from PySide2.QtWidgets import *

class SimpleItem(QtWidgets.QGraphicsItem):
    def __init__(self):
       QtWidgets.QGraphicsItem.__init__(self)
       self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)

       def boundingRect(self):
          penWidth = 1.0
          return QRectF(-10 - penWidth / 2, -10 - penWidth / 2,
                         20 + penWidth, 20 + penWidth)

       def paint(self, painter, option, widget):
          rect = self.boundingRect()
          painter.drawRect(rect)

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)

        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")

        self.groupBox = QGroupBox(self.centralwidget)
        self.groupBox.setObjectName("groupBox")

        self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)

        MainWindow.setCentralWidget(self.centralwidget)

        QMetaObject.connectSlotsByName(MainWindow)


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # OpenGL drawing surface
        self.canvas = scene.SceneCanvas(keys='interactive')
        self.canvas.create_native()
        self.canvas.native.setParent(self)

        self.view = self.canvas.central_widget.add_view()

        self.view.bgcolor = '#ffffff'   # set the canva to a white background

        scene2 = QGraphicsScene()
        item = SimpleItem()
        scene2.addItem(item)
        self.setWindowTitle('MyApp')


def main():
    import ctypes
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('my_gui')

    app = QtWidgets.QApplication([])

    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

edit: I added a class (here it is a rectangle just as an example) to illustrate my problem.编辑:我添加了一个 class (这里是一个矩形作为示例)来说明我的问题。 i have trouble integrating that snippet of the code (with SimpleItem) to OpenGL canvas我无法将该代码片段(使用 SimpleItem)集成到 OpenGL canvas

You can use the QApplication.setOverrideCursor method to assign a.png image file as your cursor when it appears inside of the Qt program.当它出现在 Qt 程序中时,您可以使用QApplication.setOverrideCursor方法将 .png 图像文件分配为您的 cursor。

Here is an example that is mostly based on the code in your question.这是一个主要基于您问题中的代码的示例。 And below is a gif that demonstrates the example.下面是演示示例的 gif。 And the last image is the image I used in the code as cursor.png最后一张图片是我在代码中使用的图片cursor.png

Hope this helps希望这可以帮助

import sys
from PySide2.QtCore import *
from PySide2.QtWidgets import *
from PySide2.QtGui import *

class SimpleItem(QtWidgets.QGraphicsItem):
    def __init__(self):
       QtWidgets.QGraphicsItem.__init__(self)
       self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, True)
       self._brush = QBrush(Qt.black)

    def boundingRect(self):
        penWidth = 1.0
        return QRectF(-50 - penWidth / 2, -50 - penWidth / 2,
                     50 + penWidth, 50 + penWidth)

    def paint(self, painter, option, widget):
        rect = self.boundingRect()
        painter.drawRect(rect)
        painter.fillRect(rect, self._brush)


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.resize(800, 600)
        self.scene = QGraphicsScene()
        self.canvas = scene.SceneCanvas(keys='interactive')
        self.view = QGraphicsView(self.scene)
        item = SimpleItem()
        self.scene.addItem(item)
        self.setCentralWidget(self.view)
        self.setWindowTitle('MyApp')


def main():
    import ctypes
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('my_gui')
    app = QtWidgets.QApplication([])
    app.setOverrideCursor(QCursor(QPixmap('cursor.png')))
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

在此处输入图像描述

在此处输入图像描述

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

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