3

I'm a beginner in coding and python. I read that tkinter is a bit to "basic" if you want to develop an application which is a a bit more complicated and PyQt is problematic for licencing. This is why I chose PySide2 in order to develop this kind of project, but found so far the documentation relatively scarce. Is this the right choice?

My present coding problem is the following: I'm Trying to load an image with PySide2 without success. Here is the code of my 2 attempts:

Attempt N°1:

import sys
import PySide2
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
img = PySide2.QtGui.QImage("/Users/mymac/Downloads/ecg_measure.png")
pixmap=PySide2.QtGui.QPixmap(img)
label = PySide2.QtWidgets.QLabel.setPixmap(pixmap)
label.show()
app.exec_()

I get the following message:

Traceback (most recent call last):

File "<ipython-input-1-86961df4959d>", line 8, in <module>
label = PySide2.QtWidgets.QLabel.setPixmap(pixmap)

TypeError: descriptor 'setPixmap' requires a 'PySide2.QtWidgets.QLabel' object but received a 'PySide2.QtGui.QPixmap'

Using the information in the Traceback, I tried the following Attempt N°2:

import sys
import PySide2
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
img = PySide2.QtGui.QImage("/Users/mymac/Downloads/ecg_measure.png")
pixmap=PySide2.QtGui.QPixmap(img)
lab=PySide2.QtWidgets.QLabel(pixmap)
PySide2.QtWidgets.QLabel.setPixmap(lab)
app.exec_()

I get the following errror message:

Traceback (most recent call last):

File "<ipython-input-1-61b41e4b2f63>", line 8, in <module>
lab=PySide2.QtWidgets.QLabel(pixmap)

TypeError: 'PySide2.QtWidgets.QLabel' called with wrong argument types:
PySide2.QtWidgets.QLabel(PySide2.QtGui.QPixmap) Supported signatures:
PySide2.QtWidgets.QLabel(PySide2.QtWidgets.QWidget = None, 
PySide2.QtCore.Qt.WindowFlags = Qt.WindowFlags())
PySide2.QtWidgets.QLabel(unicode, PySide2.QtWidgets.QWidget = None, PySide2.QtCore.Qt.WindowFlags = Qt.WindowFlags())
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
ecjb
  • 5,169
  • 12
  • 43
  • 79

1 Answers1

7

Here is what your code should look like:

import sys
from PySide2 import QtCore, QtGui, QtWidgets

app = QtWidgets.QApplication(sys.argv)

pixmap = QtGui.QPixmap('/Users/mymac/Downloads/ecg_measure.png')
label = QtWidgets.QLabel()
label.setPixmap(pixmap)    
label.show()

app.exec_()

However, a QLabel is probably not suitable for the project you want to develop. You will most likely need to use the Graphics View Framework. See this image viewer example for a basic demo that provides pan and zoom. It is written using PyQt5, but you can easily convert it to PySide2 - just replace PyQt5 with PySide2 in the imports at the top of the file, and replace pyqtSignal with Signal on line 4. The example also requires an image file named "image.jpg" in the current directory (or you can just edit the default path in the loadImage method).

If you do not have much experience with PySide/PyQt, I would recommend that you work through this tutorial. It is written for PyQt5, but the code will be 99% identical for PySide2. As suggested above, you will usually only need to change the imports and remove a few pyqt prefixes for APIs such as pyqtSignal and pyqtSlot. The full PySide2 documentation can be found here, and the full Qt documentation is here.

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Many many thanks @ekhumoro for your extensive and very precise answer with many useful ressources! In the meantime, I could load an image in a `Qlabel`, but struggled indeed in integrating `Qlabel` with a main window. I'm going to read the ressources! Many thanks again – ecjb May 07 '18 at 14:03
  • If I may ask you @ekhumoro, when I zoom in on the image with the mouse, the image itself is zoomed but when clicking with the mouse to get the coordinate (the `mousePressEvent` method I guess), I get coordinate as if the image wasn't zoomed or as if the scaled was changed for the image but not for mouse events? Did you have that with running the app? if you want me to ask another question, I can. If you don't have time, I totally understand – ecjb May 07 '18 at 22:20
  • @ecjb. Well, it's a very basic example, so it just gets the mouse position relative to the viewport - it does not take the image itself into account at all. – ekhumoro May 07 '18 at 22:26
  • @ecjb. PS: there are methods like [mapToScene](https://doc.qt.io/qt-5/qgraphicsview.html#mapToScene) and [mapFromScene](https://doc.qt.io/qt-5/qgraphicsview.html#mapFromScene) that can translate between viewport and scene coordinates. – ekhumoro May 07 '18 at 22:32
  • Well I have to say I have been trying for a week now. I changed the line `self.photoClicked.emit(QtCore.QPoint(event.pos()))` to `self.photoClicked.emit(QtWidgets.QGraphicsView.mapToScene(QtCore.QPoint(event.pos())))` but I get `TypeError: descriptor 'mapToScene' requires a 'PySide2.QtWidgets.QGraphicsView' object but received a 'PySide2.QtCore.QPoint'`. Any idea in which direction I could go? (I can ask a new question if needed) – ecjb May 13 '18 at 22:16