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_()
img.save("img.jpg"). Another problem may be that for some reason image plugins were not found - gil9red