I want to display some images on my GUI, first when I click on the button, to select first image, everything act normal, my image is displayed fitInView, but when I want to select the next image, dark magic happens.. my second image is not in center like the first one, is on top of the QGraphicsView, and also it seem like old image's QScenes doesn't disappear, I have the ScrollBar from risg and bottom and I can scrool until I have al blank on QGraphicsView, what is the problem? I can't figured out..
My code:
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)
mainwindow part:
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_())
First Image (Normal):
Second Image (Strange):