0

我从 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 来确认。

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

4

2 回答 2

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 中显示图片。

于 2017-12-04T08:30:55.587 回答
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_())
于 2017-11-30T17:15:13.367 回答