In my projet, I can't find how to determine which image and pixel coordonate when I clicking image in 2 different ScrollArea.
The result was showing in statusBar() area. (See screenshot)
I put a code in txt file with 2 jpg use with it.
Thanks in advance.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import os, sys
class MainWindow(QMainWindow, QWidget):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.pLabel = None
self.Img = QImage()
self.ImgPt = [0,0]
self.GlobPt = [0,0]
self.ImgName = ['1.jpg', '2.jpg']
self.ImgActive = 0
self.imageLabel1 = QLabel()
self.imageLabel1.setMinimumSize(800, 800)
self.imageLabel1.setAlignment(Qt.AlignCenter)
self.scrollArea1 = QScrollArea()
self.scrollArea1.setBackgroundRole(QPalette.Dark)
self.scrollArea1.setWidget(self.imageLabel1)
self.imageLabel2 = QLabel()
self.imageLabel2.setMinimumSize(800, 800)
self.imageLabel2.setAlignment(Qt.AlignCenter)
self.scrollArea2 = QScrollArea()
self.scrollArea2.setBackgroundRole(QPalette.Dark)
self.scrollArea2.setWidget(self.imageLabel2)
self.imageSplitter = QSplitter(Qt.Horizontal)
self.imageSplitter.addWidget(self.scrollArea1)
self.imageSplitter.addWidget(self.scrollArea2)
imgSplitter = self.imageSplitter
self.mainSplitter = QSplitter(Qt.Vertical)
self.mainSplitter.addWidget(self.imageSplitter)
self.setCentralWidget(self.mainSplitter)
self.sizeLabel1 = QLabel()
self.sizeLabel2 = QLabel()
self.sizeLabel3 = QLabel()
status = self.statusBar()
status.addPermanentWidget(self.sizeLabel1)
status.addPermanentWidget(self.sizeLabel2)
status.addPermanentWidget(self.sizeLabel3)
status.showMessage("Ready", 5000)
self.imageOpen1()
self.imageOpen2()
def imageOpen1(self):
self.pLabel = self.imageLabel1
self.imageLoad(os.getcwd() + '/' + '1.jpg')
def imageOpen2(self):
self.pLabel = self.imageLabel2
self.imageLoad(os.getcwd() + '/' + '2.jpg')
def imageLoad(self, fname):
image = QImage(fname)
if image.isNull():
message = "Failed to read {0}".format(fname)
else:
self.img = image
self.imageShow()
message = "Loaded {0}".format(os.path.basename(fname))
def imageShow(self, percent=None):
if self.img.isNull():
return
factor = 1
width = self.img.width() * factor
height = self.img.height() * factor
image = self.img.scaled(width, height, Qt.KeepAspectRatio)
self.pLabel.setMinimumSize(width, height)
self.pLabel.setPixmap(QPixmap.fromImage(image))
def mouseReleaseEvent(self, event):
if self.imageLabel1 :
self.ImgActive = 0
else:
self.ImgActive = 1
self.ImgPt = [event.pos().x(), event.pos().y()]
self.GlobPt = [event.globalPos().x(), event.globalPos().y()]
self.update()
def paintEvent(self, event=None):
self.sizeLabel1.setText(" Image Name: {0} ".format(self.ImgName[self.ImgActive]))
self.sizeLabel2.setText(" Image Pixel x:{0} y:{1} ".format(self.ImgPt[0], self.ImgPt[1]))
self.sizeLabel3.setText(" Global Pixel x:{0} y:{1} ".format(self.GlobPt[0], self.GlobPt[1]))
def main():
app = QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()
main()