从PySide移动到PyQt5后图像不显示

时间:2017-11-30 14:37:23

标签: python pyqt pyside pyqt5 pyside2

我从PySide到PyQt5,因为我想使用我编写的一些旧代码,Python 3.5不再支持PySide,PySide2或Python 3.4也不适用于我。

下面代码中的最后一行用于显示Example.JPG。现在它似乎没有为我做任何事情与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)

提供一套完整的代码工作有点困难,因为我不能再使用PySide来确认。

有没有办法让图片再次出现?

2 个答案:

答案 0 :(得分:1)

我找到了解决方案,而不是这个(与PySide合作):

self.pixmap_item = QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap(self.fpimage), None, self.scene) 
我现在有:

self.pixmap_item = QtWidgets.QGraphicsPixmapItem(QtGui.QPixmap(self.fpimage)) 
self.scene.addItem(self.pixmap_item) 

它正在使用PyQt5在我的QGraphicsScene中显示图片。

答案 1 :(得分:0)

在您的情况下,您在代码行下方遗漏了标签中的图片:

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)

由于您的代码不完整,我已经为您添加了Qt设计师Mark Summers计算(更新的pyqt5样式)以学习(阅读:apyqt5 gui的骨架结构)并应用上述标签代码的示例。享受。

#!/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_())