The image on QScene is not displayed on QGraphicsView. Also tried using QLabel and failed.

Without using the widget, the picture is displayed, but with its use QGraphicScene or QLabel turns out to be empty, but the text is displayed there. Tell me, please, what could be the problem?

Non-working option using QScene, QGraphicsView, QWidget:

from PyQt5.QtGui import * from PyQt5.QtCore import QUrl from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout, QGraphicsScene, QGraphicsView import sys class Widget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(50,50,500,500) url = "http://pp.vk.me/c627626/v627626428/be07/wbpWha0RqZ4.jpg" self.scene = QGraphicsScene() self.mainImage = QGraphicsView() self.nam = QNetworkAccessManager() self.nam.finished.connect(self.finishRequest) self.nam.get(QNetworkRequest(QUrl(url))) layout = QVBoxLayout() layout.addWidget(self.mainImage) self.setLayout(layout) self.show() def finishRequest(self, reply): img = QImage() img.loadFromData(reply.readAll()) self.item = QGraphicsPixmapItem(QPixmap.fromImage(img)) self.scene.addItem(self.item) self.scene.setSceneRect(img.rect()) self.mainImage.setScene(self.scene) if __name__ == '__main__': app = QApplication(sys.argv) ex = Widget() ex.show() sys.exit(app.exec_()) 

Non-working option using QLabel, QWidget:

 from PyQt5.QtGui import * from PyQt5.QtCore import QUrl from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout import sys class Widget(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): url = "http://pp.vk.me/c627626/v627626428/be07/wbpWha0RqZ4.jpg" self.lbl = QLabel("loading...") self.lbl.setAutoFillBackground(True) nam = QNetworkAccessManager() nam.finished.connect(self.finishRequest) nam.get(QNetworkRequest(QUrl(url))) self.layout = QVBoxLayout() self.layout.addWidget(self.lbl) self.setLayout(self.layout) self.show() def finishRequest(reply): img = QImage() img.loadFromData(reply.readAll()) self.lbl.setPixmap(QPixmap.fromImage(img)) if __name__ == '__main__': app = QApplication(sys.argv) ex = Widget() sys.exit(app.exec_()) 

Working version with QLabel, but without QWidget:

 from PyQt5.QtGui import * from PyQt5.QtCore import QUrl from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest from PyQt5.QtWidgets import QApplication, QLabel app = QApplication([]) url = "http://pp.vk.me/c627626/v627626428/be07/wbpWha0RqZ4.jpg" lbl = QLabel("loading...") nam = QNetworkAccessManager() def finishRequest(reply): img = QImage() img.loadFromData(reply.readAll()) lbl.setPixmap(QPixmap(img)) nam.finished.connect(finishRequest) nam.get(QNetworkRequest(QUrl(url))) lbl.show() app.exec_() 
  • one
    Is there a picture? Try to save it and make sure that it is ok: img.save("img.jpg") . Another problem may be that for some reason image plugins were not found - gil9red
  • With regards to QLabel, setAutoFillBackground tried to set true? - free_ze
  • @ gil9red, tried to save - does not work, check print (b = img.save ("img.jpg")) displays nothing. - Ramil
  • @free_ze, tried, did not help - Ramil
  • Code listing of a variant with QLabel: pp.vk.me/c836227/v836227428/159ea/w2lUtiWQfvQ.jpg I suppose the problem is to pass the reply parameter to the finishRequest method, but I don’t understand where the error is. Working code listing without widget: pp.vk.me/c836227/v836227428/159f1/MYwZhAKWKos.jpg - Ramil

1 answer 1

I saw the problem in the finish_request function, there you passed one parameter - reply , but the methods of objects in python always take self first parameter - a pointer to a class object, its analog is this in c ++

When I inserted your code into the IDE (PyCharm), it immediately highlighted in that function that it doesn’t know where the self variable came from, which was actually correct.

 from PyQt5.QtGui import QPixmap from PyQt5.QtCore import QUrl from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest from PyQt5.QtWidgets import QApplication, QLabel, QWidget, QVBoxLayout class Widget(QWidget): def __init__(self): super().__init__() self.lbl = QLabel("loading...") self.lbl.setAutoFillBackground(True) self.nam = QNetworkAccessManager() self.nam.finished.connect(self.finish_request) url = "http://pp.vk.me/c627626/v627626428/be07/wbpWha0RqZ4.jpg" self.nam.get(QNetworkRequest(QUrl(url))) layout = QVBoxLayout() layout.addWidget(self.lbl) self.setLayout(layout) def finish_request(self, reply): img = QPixmap() img.loadFromData(reply.readAll()) self.lbl.setPixmap(img) if __name__ == '__main__': app = QApplication([]) w = Widget() w.show() app.exec() 

With stage:

 from PyQt5.QtGui import QPixmap from PyQt5.QtCore import QUrl from PyQt5.QtNetwork import QNetworkAccessManager, QNetworkRequest from PyQt5.QtWidgets import ( QApplication, QWidget, QVBoxLayout, QGraphicsScene, QGraphicsView, QGraphicsPixmapItem, QGraphicsTextItem ) class Widget(QWidget): def __init__(self): super().__init__() self.scene = QGraphicsScene() self.scene.addItem(QGraphicsTextItem('loading...')) self.view = QGraphicsView() self.view.setScene(self.scene) self.nam = QNetworkAccessManager() self.nam.finished.connect(self.finish_request) url = "http://pp.vk.me/c627626/v627626428/be07/wbpWha0RqZ4.jpg" self.nam.get(QNetworkRequest(QUrl(url))) layout = QVBoxLayout() layout.addWidget(self.view) self.setLayout(layout) def finish_request(self, reply): self.scene.clear() img = QPixmap() img.loadFromData(reply.readAll()) item = QGraphicsPixmapItem(img) self.scene.addItem(item) if __name__ == '__main__': app = QApplication([]) w = Widget() w.show() app.exec() 
  • Thanks for the answer, now it works, only the QScene version, QGraphicsView is still not working, I fixed it according to your corrections. - Ramil
  • I don’t know what you have there may be a problem :) it seems that the code is still small and transparent If you are looking for an error, then: log in the code, use the debugger - gil9red
  • thank you very much, later I will sort out what the problem was, I just don’t have a debugger, while I’m too lazy to install and there’s no time. - Ramil