Reputation: 69
I went from PySide to PyQt5 because I want to use some older code I have written and Python 3.5 does not support PySide anymore and PySide2 or Python 3.4 does not work for me either.
The last line in the code below used to get me the Example.JPG displayed. Now it does not seem to do anything for me with PyQt5
self.scene = QtWidgets.QGraphicsScene()
self.view = QtWidgets.QGraphicsView(self.scene)
layout.addWidget(self.view, 1, 0, 1, 0)
self.view.scale(0.15,0.15)
self.view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.view.setTransformationAnchor(self.view.AnchorUnderMouse)
self.view.wheelEvent = self.scrollSelect
self.view.keyPressEvent = self.keypressed
self.fpimage = 'Example.JPG'
self.pixmap_item = QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap(self.fpimage), None, self.scene)
Giving a complete working set of code is a bit difficult since I can't use PySide anymore to confirm.
Is there a way to get the image show up again?
Upvotes: 0
Views: 757
Reputation: 69
I found the solution, in stead of this (worked with PySide):
self.pixmap_item = QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap(self.fpimage), None, self.scene)
i now have:
self.pixmap_item = QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap(self.fpimage))
self.scene.addItem(self.pixmap_item)
And it is displaying the picture in my QGraphicsScene with PyQt5.
Upvotes: 1
Reputation: 3731
In your case you're missing below codelines for showing a picture in a label:
pixmap = QtGui.QPixmap(self.mainwindow_image).scaled(main_width, main_height, aspectRatioMode = 1)
self.label = QtWidgets.QLabel(self.widget_1)
self.label.setMinimumSize(QtCore.QSize(225, 200))
self.label.setMaximumSize(QtCore.QSize(225, 200))
self.label.setText("")
self.label.setObjectName("label")
self.label.setPixmap(pixmap)
As your code is incomplete I've added you an example from the Qt designer Mark Summers calculate (updated pyqt5 style) to learn from (read: skeleton structure of apyqt5 gui) and apply the above label code. Enjoy.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys, os
from math import *
from PyQt5 import *
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QAction, QApplication, QDialog, QLineEdit, QTextBrowser, QVBoxLayout, QWidget
#from PyQt5.QtGui import
class Form(QDialog):
def __init__(self, parent = None):
super(Form, self).__init__(parent)
self.browser = QTextBrowser()
self.lineedit = QLineEdit("Lots of text here... type something and press Enter")
self.lineedit.selectAll()
layout = QVBoxLayout()
layout.addWidget(self.browser)
layout.addWidget(self.lineedit)
# starts at the lineEdit for the user to type straight away.
self.setLayout(layout)
self.lineedit.setFocus()
self.lineedit.returnPressed.connect(self.updateUi)
self.setWindowTitle("Calculate the shit out of your CPU")
def updateUi(self):
try:
text = unicode(self.lineedit.text())
self.browser.append("%s = <b>%s<b/>" % (text, eval(text)))
except:
self.browser.append("<font color=red> %s is invalid!</font>" % text)
pass
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Form()
window.show()
sys.exit(app.exec_())
Upvotes: 0