Reputation: 760
I have a question about how to get the RGB values from a picture while moving the mouse around. I have a few line edit
boxes that I would like the values to change as I move my mouse over the picture. I'm close but it is not working yet. I understand why this is not working the way I would like but I am not exactly sure how to fix it.
How this is set up
I used Qt designer to make the GUI's and then I translated that into python code which I am referencing in. In my main window, I have an MDI area and some line edit
boxes. When I open a photo it creates a new MDI window that has a graphic view
and scroll area
that is associated with it. To track the mouse movements I am using an installEventFilter
on the viewport of the graphic view
. So anything that is in view it will give me the coordinates within that view and not the whole picture its self.
Example of my problem
If I load in a picture bigger than my screen it would put scroll bars on the side. As long as I'm at the top everything seems to work correctly but if I scroll down or to the side to one side or the other now the picture has moved but the viewport has not.
What I would like
My end goal is to be able to get the coordinates of each pixel in the picture that my mouse is currently on and then from that extract the RGB values. If I move the mouse then the coordinates will change. From there I just want to values in a few line edit
boxes to change as I move the mouse around.
Main Window
This is my Main_GUI.py file
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_())
Functions for main window
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_())
Screenshot of the main window with photo open
Here is the code for the widget I called 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 window that will display the photo
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)
Upvotes: 1
Views: 2874
Reputation: 243993
You are getting wrong values when you scroll the scrollbar because the coordinates are in relation to the window and not the image, to get the correct coordinates in a simple way is to overwrite the hoverMoveEvent
method of the QGraphicsPixmapItem
so we create a class that inherits. The problem is that the QGraphicsItem
do not inherit from QObject
so they can not emit signals, a simple solution is to create a class that emits the signals and pass it an instance to the custom QGraphicsPixmapItem
class.
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())
Then the objects are created whenever necessary and we connect the necessary elements:
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()))
Upvotes: 1