【发布时间】:2019-06-08 12:52:29
【问题描述】:
我想在我的 GUI 上显示一些图像,首先当我单击按钮时,选择第一张图像,一切正常,我的图像显示 fitInView,但是当我想选择下一张图片,黑魔法发生了..我的第二张图片不像第一张那样在中心,在 QGraphicsView 的顶部,而且看起来旧图片的 QScenes 并没有消失,我有来自 risg 和底部的 ScrollBar,我可以滚动直到我在 QGraphicsView 上有一个空白,有什么问题?想不通。。
我的代码:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
# from PyQt4 import QtCore, QtWidgets
class Graph(QtWidgets.QGraphicsView):
def __init__(self, parent=None):
super(Graph, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
self.setScene(scene)
self.pixmap = QtGui.QPixmap()
self.m_pixmap_item = self.scene().addPixmap(self.pixmap)
def setPixmap(self, _pixmap):
self.pixmap = _pixmap
# self.m_pixmap_item = self.scene().addPixmap(self.pixmap)
self.m_pixmap_item.setPixmap(self.pixmap)
self.fitInView(self.m_pixmap_item, QtCore.Qt.KeepAspectRatio)
def resizeEvent(self, event):
if not self.m_pixmap_item.pixmap().isNull():
self.fitInView(self.m_pixmap_item, QtCore.Qt.KeepAspectRatio)
super(Graph, self).resizeEvent(event)
主窗口部分:
class Window(QtWidgets.QWidget):
def __init__(self):
super(Window, self).__init__()
self.setMinimumSize(640, 480)
# --> Create Class ResizbleRubberBand <-> Parent: QLabel
self.band = Graph(self)
# --> Create buttons & functions
self.fileOpen_btn = QtWidgets.QPushButton()
self.create_and_format_btn(self.fileOpen_btn, self.fileOpen, 'GUI/icons/file.png', 32)
btn_Layout = QtWidgets.QVBoxLayout()
btn_layout_1 = QtWidgets.QHBoxLayout()
btn_layout_1.addWidget(self.fileOpen_btn)
btn_Layout.addLayout(btn_layout_1)
layout = QtWidgets.QHBoxLayout(self)
layout.setAlignment(QtCore.Qt.AlignCenter)
layout.addWidget(self.band)
layout.addLayout(btn_Layout)
def create_and_format_btn(self, btn_name, btn_function, btn_icon, btn_iconsize):
btn_name.clicked.connect(btn_function)
btn_name.setIcon(QtGui.QIcon(btn_icon))
btn_name.setIconSize(QtCore.QSize(btn_iconsize, btn_iconsize))
def fileOpen(self):
path = QtWidgets.QFileDialog.getOpenFileName(self, 'Open File')
filename = path[0]
self.loadImage_On_Pixmap(filename)
def loadImage_On_Pixmap(self, file):
f = QtGui.QPixmap(file)
self.band.setPixmap(f)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = Window()
# window.setGeometry(800, 100, 600, 500)
window.show()
sys.exit(app.exec_())
第一张图片(正常):
第二张图片(奇怪):
【问题讨论】:
标签: python pyqt5 qgraphicsscene qpixmap