[英]PYQT Graphics View Pixel Value of an image
我有一个关于如何在移动鼠标的同时从图片中获取 RGB 值的问题。 我有几line edit
框,当我将鼠标移到图片上时,我希望这些值发生变化。 我很接近,但它还没有工作。 我明白为什么这不能按照我想要的方式工作,但我不确定如何解决它。
这是如何设置的
我使用 Qt 设计器制作 GUI,然后将其翻译成我引用的 Python 代码。在我的主窗口中,我有一个 MDI 区域和一些line edit
框。 当我打开一张照片时,它会创建一个新的 MDI 窗口,该窗口具有与之关联的graphic view
和scroll area
。 为了跟踪鼠标移动,我在graphic view
的视口上使用了installEventFilter
。 所以任何在视图中的东西都会给我那个视图中的坐标,而不是整个图片本身。
我的问题的例子
如果我加载的图片比我的屏幕大,它会将滚动条放在一边。 只要我在顶部,一切似乎都可以正常工作,但是如果我向下滚动或向一侧或另一侧滚动,现在图片已经移动但视口没有。
我想要什么
我的最终目标是能够获取鼠标当前所在图片中每个像素的坐标,然后从中提取 RGB 值。 如果我移动鼠标,那么坐标就会改变。 从那里我只想在几line edit
框中的值随着我移动鼠标而改变。
主窗口
这是我的 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_())
打开照片的主窗口的屏幕截图
这是我称为 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)
滚动滚动条时会得到错误的值,因为坐标与窗口相关,而不是图像,要以简单的方式获取正确的坐标是覆盖QGraphicsPixmapItem
的hoverMoveEvent
方法,因此我们创建了一个继承的类。 问题是QGraphicsItem
不是从QObject
继承的,因此它们不能发出信号,一个简单的解决方案是创建一个发出信号的类并将其传递给自定义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())
然后在必要时创建对象,我们连接必要的元素:
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.setUpMainUiFunction()
def setUpMainUiFunction(self):
self.actionOpen.triggered.connect(self.OpenDialog)
self.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.TopToolBar.addAction(open)
self.TopToolBar.addAction(save)
self.LeftToolBar.addAction(NormalCursor)
self.LeftToolBar.addAction(CrosshairCursor)
# self.TopToolBar.actionTriggered[QAction].connect(self.toolbtnpressed)
def OpenDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
PicturePath = QStandardPaths.standardLocations(QStandardPaths.PicturesLocation)[0]
filenames, _ = QFileDialog.getOpenFileNames(self, "Open File", PicturePath, "JPEG File (*.png)", options=options)
for filename in filenames:
pixmap = QPixmap(filename)
self.showPicture(pixmap)
self.statusbar.showMessage("Successfully Loaded: {}".format(filename))
def showPicture(self, picture):
sub = QMdiSubWindow(self)
loadPicture = LoadPicture(picture, sub)
sub.setWidget(loadPicture)
sub.setObjectName("Load_Picture_window")
sub.setWindowTitle("New Photo")
self.mdiArea.addSubWindow(sub)
sub.show()
sub.resize(picture.size())
loadPicture.log.MousePixmapSignal.connect(self.updatePixel)
def updatePixel(self, point, color):
self.UserInput_PixelValue_X.setText("{}".format(point.x()))
self.UserInput_PixelValue_Y.setText("{}".format(point.y()))
self.UserInput_PixelValue_R.setText("{}".format(color.red()))
self.UserInput_PixelValue_G.setText("{}".format(color.green()))
self.UserInput_PixelValue_B.setText("{}".format(color.blue()))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.