PYQT图形查看图像的像素值

时间:2017-11-01 21:43:40

标签: python python-3.x graphics pyqt pyqt5

我有一个关于如何在移动鼠标时从图片中获取RGB值的问题。我有几个line edit框,我希望在将鼠标移到图片上时更改值。我很接近,但还没有工作。我明白为什么这不按照我想要的方式工作,但我不确定如何解决它。

如何设置

我使用Qt设计器制作GUI,然后将其翻译成我引用的python代码。在我的主窗口中,我有一个MDI区域和一些line edit框。当我打开照片时,它会创建一个新的MDI窗口,其中包含与其关联的graphic viewscroll area。要跟踪鼠标移动,我在installEventFilter的视口上使用了graphic view。所以在视野中的任何东西都会给我在该视图中的坐标而不是整个画面的自我。

我的问题示例

如果我加载的图片大于我的屏幕,则会在侧面放置滚动条。只要我在顶部,一切似乎都能正常工作,但如果我向下滚动或向侧面向一侧或另一侧移动现在图片已移动但视口没有。

我想要什么

我的最终目标是能够获取鼠标当前所在图片中每个像素的坐标,然后从中提取RGB值。如果我移动鼠标,坐标将会改变。从那里我只想在几个line edit框中改变值,以便在我移动鼠标时进行更改。

主窗口的屏幕截图 Screen shot of the main window

主窗口

这是我的Main_GUI.py文件

 from PyQt5 import QtCore, QtGui, QtWidgets

    class Ui_MainWindow(object):
        def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(818, 814)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")
        self.Button_LoadPhoto = QtWidgets.QPushButton(self.centralwidget)
        self.Button_LoadPhoto.setObjectName("Button_LoadPhoto")
        self.gridLayout.addWidget(self.Button_LoadPhoto, 1, 0, 1, 1)
        self.splitter = QtWidgets.QSplitter(self.centralwidget)
        self.splitter.setOrientation(QtCore.Qt.Horizontal)
        self.splitter.setObjectName("splitter")
        self.mdiArea = QtWidgets.QMdiArea(self.splitter)
        self.mdiArea.setObjectName("mdiArea")
        self.verticalLayoutWidget = QtWidgets.QWidget(self.splitter)
        self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setContentsMargins(0, 0, 0, 0)
        self.verticalLayout.setObjectName("verticalLayout")
        self.tabWidget_Calb = QtWidgets.QTabWidget(self.verticalLayoutWidget)
        self.tabWidget_Calb.setObjectName("tabWidget_Calb")
        self.tab_calibration = QtWidgets.QWidget()
        self.tab_calibration.setObjectName("tab_calibration")
        self.gridLayout_3 = QtWidgets.QGridLayout(self.tab_calibration)
        self.gridLayout_3.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_3.setObjectName("gridLayout_3")
        self.UserInput_Calibration_ScaleFactor = QtWidgets.QLineEdit(self.tab_calibration)
        self.UserInput_Calibration_ScaleFactor.setObjectName("UserInput_Calibration_ScaleFactor")
        self.gridLayout_3.addWidget(self.UserInput_Calibration_ScaleFactor, 2, 2, 1, 2)
        self.txt_Calibration_ScaleFactor = QtWidgets.QLabel(self.tab_calibration)
        self.txt_Calibration_ScaleFactor.setObjectName("txt_Calibration_ScaleFactor")
        self.gridLayout_3.addWidget(self.txt_Calibration_ScaleFactor, 2, 1, 1, 1)
        self.txt_Calibration_X1 = QtWidgets.QLabel(self.tab_calibration)
        self.txt_Calibration_X1.setObjectName("txt_Calibration_X1")
        self.gridLayout_3.addWidget(self.txt_Calibration_X1, 0, 0, 1, 1)
        self.UserInput_Calibration_X1 = QtWidgets.QLineEdit(self.tab_calibration)
        self.UserInput_Calibration_X1.setObjectName("UserInput_Calibration_X1")
        self.gridLayout_3.addWidget(self.UserInput_Calibration_X1, 0, 1, 1, 1)
        self.txt_Calibration_Y1 = QtWidgets.QLabel(self.tab_calibration)
        self.txt_Calibration_Y1.setObjectName("txt_Calibration_Y1")
        self.gridLayout_3.addWidget(self.txt_Calibration_Y1, 1, 0, 1, 1)
        self.UserInput_Calibration_X2 = QtWidgets.QLineEdit(self.tab_calibration)
        self.UserInput_Calibration_X2.setObjectName("UserInput_Calibration_X2")
        self.gridLayout_3.addWidget(self.UserInput_Calibration_X2, 0, 3, 1, 1)
        self.UserInput_Calibration_Y2 = QtWidgets.QLineEdit(self.tab_calibration)
        self.UserInput_Calibration_Y2.setObjectName("UserInput_Calibration_Y2")
        self.gridLayout_3.addWidget(self.UserInput_Calibration_Y2, 1, 3, 1, 1)
        self.txt_Calibration_X2 = QtWidgets.QLabel(self.tab_calibration)
        self.txt_Calibration_X2.setObjectName("txt_Calibration_X2")
        self.gridLayout_3.addWidget(self.txt_Calibration_X2, 0, 2, 1, 1)
        self.UserInput_Calibration_Y1 = QtWidgets.QLineEdit(self.tab_calibration)
        self.UserInput_Calibration_Y1.setObjectName("UserInput_Calibration_Y1")
        self.gridLayout_3.addWidget(self.UserInput_Calibration_Y1, 1, 1, 1, 1)
        self.txt_Calibration_Y2 = QtWidgets.QLabel(self.tab_calibration)
        self.txt_Calibration_Y2.setObjectName("txt_Calibration_Y2")
        self.gridLayout_3.addWidget(self.txt_Calibration_Y2, 1, 2, 1, 1)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.txt_scale = QtWidgets.QLabel(self.tab_calibration)
        self.txt_scale.setObjectName("txt_scale")
        self.horizontalLayout.addWidget(self.txt_scale)
        self.radioButton_Log = QtWidgets.QRadioButton(self.tab_calibration)
        self.radioButton_Log.setEnabled(True)
        self.radioButton_Log.setChecked(True)
        self.radioButton_Log.setObjectName("radioButton_Log")
        self.horizontalLayout.addWidget(self.radioButton_Log)
        self.radioButton_Linear = QtWidgets.QRadioButton(self.tab_calibration)
        self.radioButton_Linear.setObjectName("radioButton_Linear")
        self.horizontalLayout.addWidget(self.radioButton_Linear)
        self.gridLayout_3.addLayout(self.horizontalLayout, 5, 0, 1, 4)
        self.txt_Calibration_X1.raise_()
        self.UserInput_Calibration_X1.raise_()
        self.txt_Calibration_Y1.raise_()
        self.UserInput_Calibration_Y1.raise_()
        self.txt_Calibration_X2.raise_()
        self.UserInput_Calibration_X2.raise_()
        self.txt_Calibration_Y2.raise_()
        self.UserInput_Calibration_Y2.raise_()
        self.UserInput_Calibration_ScaleFactor.raise_()
        self.txt_Calibration_ScaleFactor.raise_()
        self.tabWidget_Calb.addTab(self.tab_calibration, "")
        self.verticalLayout.addWidget(self.tabWidget_Calb)
        self.tabWidget_2 = QtWidgets.QTabWidget(self.verticalLayoutWidget)
        self.tabWidget_2.setObjectName("tabWidget_2")
        self.tab_PValues = QtWidgets.QWidget()
        self.tab_PValues.setObjectName("tab_PValues")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.tab_PValues)
        self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.txt_PixelValue_G = QtWidgets.QLabel(self.tab_PValues)
        self.txt_PixelValue_G.setObjectName("txt_PixelValue_G")
        self.gridLayout_2.addWidget(self.txt_PixelValue_G, 2, 0, 1, 1)
        self.txt_PixelValue_R = QtWidgets.QLabel(self.tab_PValues)
        self.txt_PixelValue_R.setObjectName("txt_PixelValue_R")
        self.gridLayout_2.addWidget(self.txt_PixelValue_R, 1, 0, 1, 1)
        self.txt_PixelValue_Y = QtWidgets.QLabel(self.tab_PValues)
        self.txt_PixelValue_Y.setObjectName("txt_PixelValue_Y")
        self.gridLayout_2.addWidget(self.txt_PixelValue_Y, 0, 3, 1, 1)
        self.UserInput_PixelValue_Y = QtWidgets.QLineEdit(self.tab_PValues)
        self.UserInput_PixelValue_Y.setObjectName("UserInput_PixelValue_Y")
        self.gridLayout_2.addWidget(self.UserInput_PixelValue_Y, 0, 4, 1, 1)
        self.UserInput_PixelValue_X = QtWidgets.QLineEdit(self.tab_PValues)
        self.UserInput_PixelValue_X.setObjectName("UserInput_PixelValue_X")
        self.gridLayout_2.addWidget(self.UserInput_PixelValue_X, 0, 1, 1, 1)
        self.txt_PixelValue_X = QtWidgets.QLabel(self.tab_PValues)
        self.txt_PixelValue_X.setObjectName("txt_PixelValue_X")
        self.gridLayout_2.addWidget(self.txt_PixelValue_X, 0, 0, 1, 1)
        self.UserInput_PixelValue_R = QtWidgets.QLineEdit(self.tab_PValues)
        self.UserInput_PixelValue_R.setObjectName("UserInput_PixelValue_R")
        self.gridLayout_2.addWidget(self.UserInput_PixelValue_R, 1, 1, 1, 1)
        self.UserInput_PixelValue_G = QtWidgets.QLineEdit(self.tab_PValues)
        self.UserInput_PixelValue_G.setObjectName("UserInput_PixelValue_G")
        self.gridLayout_2.addWidget(self.UserInput_PixelValue_G, 2, 1, 1, 1)
        self.UserInput_PixelValue_B = QtWidgets.QLineEdit(self.tab_PValues)
        self.UserInput_PixelValue_B.setObjectName("UserInput_PixelValue_B")
        self.gridLayout_2.addWidget(self.UserInput_PixelValue_B, 3, 1, 1, 1)
        self.txt_PixelValue_B = QtWidgets.QLabel(self.tab_PValues)
        self.txt_PixelValue_B.setObjectName("txt_PixelValue_B")
        self.gridLayout_2.addWidget(self.txt_PixelValue_B, 3, 0, 1, 1)
        self.tabWidget_2.addTab(self.tab_PValues, "")
        self.tab_Statistics = QtWidgets.QWidget()
        self.tab_Statistics.setObjectName("tab_Statistics")
        self.tabWidget_2.addTab(self.tab_Statistics, "")
        self.verticalLayout.addWidget(self.tabWidget_2)
        self.tabWidget_3 = QtWidgets.QTabWidget(self.verticalLayoutWidget)
        self.tabWidget_3.setObjectName("tabWidget_3")
        self.tab_History = QtWidgets.QWidget()
        self.tab_History.setObjectName("tab_History")
        self.tabWidget_3.addTab(self.tab_History, "")
        self.verticalLayout.addWidget(self.tabWidget_3)
        self.gridLayout.addWidget(self.splitter, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 818, 21))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuEdit = QtWidgets.QMenu(self.menubar)
        self.menuEdit.setObjectName("menuEdit")
        self.menuImages = QtWidgets.QMenu(self.menubar)
        self.menuImages.setObjectName("menuImages")
        self.menuWindow = QtWidgets.QMenu(self.menubar)
        self.menuWindow.setObjectName("menuWindow")
        self.menuHelp = QtWidgets.QMenu(self.menubar)
        self.menuHelp.setObjectName("menuHelp")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.TopToolBar = QtWidgets.QToolBar(MainWindow)
        self.TopToolBar.setObjectName("TopToolBar")
        MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.TopToolBar)
        self.LeftToolBar = QtWidgets.QToolBar(MainWindow)
        self.LeftToolBar.setObjectName("LeftToolBar")
        MainWindow.addToolBar(QtCore.Qt.LeftToolBarArea, self.LeftToolBar)
        self.actionNew = QtWidgets.QAction(MainWindow)
        self.actionNew.setObjectName("actionNew")
        self.actionOpen = QtWidgets.QAction(MainWindow)
        self.actionOpen.setObjectName("actionOpen")
        self.menuFile.addAction(self.actionNew)
        self.menuFile.addAction(self.actionOpen)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuEdit.menuAction())
        self.menubar.addAction(self.menuImages.menuAction())
        self.menubar.addAction(self.menuWindow.menuAction())
        self.menubar.addAction(self.menuHelp.menuAction())

        self.retranslateUi(MainWindow)
        self.tabWidget_Calb.setCurrentIndex(0)
        self.tabWidget_2.setCurrentIndex(0)
        self.tabWidget_3.setCurrentIndex(0)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.Button_LoadPhoto.setText(_translate("MainWindow", "Load Photo"))
        self.txt_Calibration_ScaleFactor.setText(_translate("MainWindow", "Scale Factor:"))
        self.txt_Calibration_X1.setText(_translate("MainWindow", "X1"))
        self.txt_Calibration_Y1.setText(_translate("MainWindow", "Y1:"))
        self.txt_Calibration_X2.setText(_translate("MainWindow", "X2:"))
        self.txt_Calibration_Y2.setText(_translate("MainWindow", "Y2:"))
        self.txt_scale.setText(_translate("MainWindow", "Scale:"))
        self.radioButton_Log.setText(_translate("MainWindow", "Logarithmic"))
        self.radioButton_Linear.setText(_translate("MainWindow", "Linear"))
        self.tabWidget_Calb.setTabText(self.tabWidget_Calb.indexOf(self.tab_calibration), _translate("MainWindow", "Calibration"))
        self.txt_PixelValue_G.setText(_translate("MainWindow", "G"))
        self.txt_PixelValue_R.setText(_translate("MainWindow", "R"))
        self.txt_PixelValue_Y.setText(_translate("MainWindow", "Y:"))
        self.txt_PixelValue_X.setText(_translate("MainWindow", "X:"))
        self.txt_PixelValue_B.setText(_translate("MainWindow", "B"))
        self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.tab_PValues), _translate("MainWindow", "Pixel Values"))
        self.tabWidget_2.setTabText(self.tabWidget_2.indexOf(self.tab_Statistics), _translate("MainWindow", "Statistics"))
        self.tabWidget_3.setTabText(self.tabWidget_3.indexOf(self.tab_History), _translate("MainWindow", "History"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuEdit.setTitle(_translate("MainWindow", "Edit"))
        self.menuImages.setTitle(_translate("MainWindow", "Images"))
        self.menuWindow.setTitle(_translate("MainWindow", "Window"))
        self.menuHelp.setTitle(_translate("MainWindow", "Help"))
        self.TopToolBar.setWindowTitle(_translate("MainWindow", "toolBar_2"))
        self.LeftToolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
        self.actionNew.setText(_translate("MainWindow", "New"))
        self.actionOpen.setText(_translate("MainWindow", "Open"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

主窗口的功能

import sys
import os
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from Main_GUI import *
from GraphicsArea import *

    class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        QWidget.__init__(self,parent)
        self.MainUi = Ui_MainWindow()
        self.MainUi.setupUi(self)
        self.setUpMainUiFunction()


    def setUpMainUiFunction(self):
        self.MainUi.actionOpen.triggered.connect(self.OpenDialog)
        self.MainUi.Button_LoadPhoto.clicked.connect(self.OpenDialog)

        open = QAction(QIcon("icons/open.bmp"),"open",self)
        save = QAction(QIcon("icons/save.bmp"),"save",self)
        NormalCursor = QAction(QIcon("icons/cursor-normal.png"),"NormalCursor",self)
        CrosshairCursor = QAction(QIcon("icons/crosshair.png"),"CrosshairCursor",self)


        self.MainUi.TopToolBar.addAction(open)
        self.MainUi.TopToolBar.addAction(save)
        self.MainUi.LeftToolBar.addAction(NormalCursor)
        self.MainUi.LeftToolBar.addAction(CrosshairCursor)

        self.MainUi.TopToolBar.actionTriggered[QAction].connect(self.toolbtnpressed)




    def OpenDialog(self):
        options = QFileDialog.Options()
        options |= QFileDialog.DontUseNativeDialog


        fileToOpen, _ = QFileDialog.getOpenFileNames(self,"Open File", "","JPEG Files (*.jpeg)", options=options)

        pixmap = QtGui.QPixmap(fileToOpen[0])

        self.showPicture(pixmap,pixmap.size().width(),pixmap.size().height())

        self.MainUi.statusbar.showMessage("Successfully Loaded: "+ fileToOpen[0])



    def showPicture(self,picture,width,height):
        sub = QtWidgets.QMdiSubWindow()
        self.Load_Picture = LoadPicture()

        sub.setWidget(self.Load_Picture)

        sub.setObjectName("Load_Picture_window")
        sub.setWindowTitle("New Photo")
        self.MainUi.mdiArea.addSubWindow(sub)

        self.Load_Picture.setpicture(picture)
        self.Load_Picture.MousePositionSignal.connect(self.PixelValueUpdate)

        self.PictureWidth = width
        self.PictureHeight = height

        sub.resize(width/.92,self.MainUi.mdiArea.geometry().height())

        sub.show()
        self.NewPicture = picture.toImage()


    def PixelValueUpdate(self,Coordinates):
        if  Coordinates.x():
            R,G,B = QColor(self.NewPicture.pixel(Coordinates.x(),Coordinates.y())).getRgb()[:-1] 
            self.MainUi.UserInput_PixelValue_X.setText(str(Coordinates.x()))
            self.MainUi.UserInput_PixelValue_Y.setText(str(Coordinates.y()))

            self.MainUi.UserInput_PixelValue_R.setText(str(R))
            self.MainUi.UserInput_PixelValue_G.setText(str(G))
            self.MainUi.UserInput_PixelValue_B.setText(str(B))

            QApplication.setOverrideCursor(Qt.CrossCursor)
            self.UpdateStatueBar(Coordinates)
        else:
            QApplication.setOverrideCursor(Qt.ArrowCursor)

if __name__ == "__main__":
    os.environ["QT_AUTO_SCREEN_SCALE_FACTOR"] = "1"
    pyqtRemoveInputHook()
    app = QtWidgets.QApplication(sys.argv)
    app.setAttribute(Qt.AA_EnableHighDpiScaling,True)
    screen_resolution = app.desktop().screenGeometry()
    width, height = screen_resolution.width()/1.5, screen_resolution.height()/1.2
    MainWindow = MainWindow()
    MainWindow.resize(width,height)
    MainWindow.setWindowTitle('Figure out name later')
    MainWindow.show()
    sys.exit(app.exec_())

打开照片的主窗口的屏幕截图

Screen Shot of the main window with MDI window open

以下是我称之为GraphicsArea_GUI.py的小部件的代码

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_GraphicsArea(object):
    def setupUi(self, GraphicsArea):
        GraphicsArea.setObjectName("GraphicsArea")
        GraphicsArea.resize(400, 300)
        self.gridLayout = QtWidgets.QGridLayout(GraphicsArea)
        self.gridLayout.setObjectName("gridLayout")
        self.scrollArea = QtWidgets.QScrollArea(GraphicsArea)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents = QtWidgets.QWidget()
        self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 380, 280))
        self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
        self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.PictureArea = QtWidgets.QGraphicsView(self.scrollAreaWidgetContents)
        self.PictureArea.setObjectName("PictureArea")
        self.gridLayout_2.addWidget(self.PictureArea, 0, 0, 1, 1)
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)

        self.retranslateUi(GraphicsArea)
        QtCore.QMetaObject.connectSlotsByName(GraphicsArea)

    def retranslateUi(self, GraphicsArea):
        _translate = QtCore.QCoreApplication.translate
        GraphicsArea.setWindowTitle(_translate("GraphicsArea", "Form"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    GraphicsArea = QtWidgets.QWidget()
    ui = Ui_GraphicsArea()
    ui.setupUi(GraphicsArea)
    GraphicsArea.show()
    sys.exit(app.exec_())

将显示照片的MDI窗口

import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from GraphicsArea_GUI import *

class LoadPicture(QWidget):
    MousePositionSignal = pyqtSignal(object)
    def __init__(self, parent=None):
        QWidget.__init__(self, parent)

        self.GraphicAreaGUI = Ui_GraphicsArea()
        self.GraphicAreaGUI.setupUi(self)
        self.setUpLoadInputUi()
        self.GraphicAreaGUI.PictureArea.viewport().installEventFilter(self)


    def setUpLoadInputUi(self):
        pass
        self.scene=QGraphicsScene()
        self.pixitem=QGraphicsPixmapItem()
        self.grali=[]

    def setpicture(self,pixmap):
        self.pixitem.setPixmap(pixmap)
        self.grali.append(self.pixitem)
        self.scene.addItem(self.grali[-1])
        self.GraphicAreaGUI.PictureArea.setScene(self.scene)
        self.GraphicAreaGUI.PictureArea.show()
        self.GraphicAreaGUI.PictureArea.setMouseTracking(True)



    def eventFilter(self, source, event):
        if (event.type() == QEvent.MouseMove and
            source is self.GraphicAreaGUI.PictureArea.viewport()):
            self.MousePositionSignal.emit(event)
        return QWidget.eventFilter(self, source, event)

1 个答案:

答案 0 :(得分:1)

滚动滚动条时得到的值不正确,因为坐标与窗口有关而不是图像,要以简单的方式获取正确的坐标,就会覆盖hoverMoveEvent方法QGraphicsPixmapItem 1}}所以我们创建一个继承的类。问题是QGraphicsItem不会从QObject继承,因此它们无法发出信号,一个简单的解决方案是创建一个发出信号的类并将实例传递给自定义{{1} } class。

QGraphicsPixmapItem

然后在必要时创建对象,并连接必要的元素:

class LogObject(QObject):
    MousePixmapSignal = pyqtSignal(QPoint, QColor)


class PictureItem(QGraphicsPixmapItem):
    def __init__(self, log, *args, **kwargs):
        QGraphicsPixmapItem.__init__(self, *args, **kwargs)
        self.setAcceptHoverEvents(True)
        self.log = log

    def hoverMoveEvent(self, event):
        point = event.pos().toPoint()
        color = QColor(self.pixmap().toImage().pixel(point.x(), point.y()))
        self.log.MousePixmapSignal.emit(point, color)
        QGraphicsPixmapItem.hoverMoveEvent(self, event)

    def hoverEnterEvent(self, event):
        QApplication.setOverrideCursor(Qt.CrossCursor)
        QGraphicsPixmapItem.hoverMoveEvent(self, event)

    def hoverLeaveEvent(self, event):
        QApplication.setOverrideCursor(Qt.ArrowCursor)
        QGraphicsPixmapItem.hoverLeaveEvent(self, event)


class LoadPicture(QWidget, Ui_GraphicsArea):
    def __init__(self, pixmap, parent=None):
        QWidget.__init__(self, parent)
        self.setupUi(self)
        self.log = LogObject(self)
        self.PictureArea.setScene(QGraphicsScene())
        self.item = PictureItem(self.log, pixmap)
        self.PictureArea.scene().addItem(self.item)
        self.resize(pixmap.size())