Qt 6.11 is out! See what's new in the release
blog
QWidgets Pixmap not appearing
-
I'm trying to add an image viewer to my Python Qt by creating a Pixmap widget but my current code doesn't seem to do anything. Here is the relevant portion of the code:
from PySide6 import QtCore, QtWidgets, QtGui from PySide6.QtUiTools import QUiLoader from PySide6.QtCore import QFile from ui.ui_mainWindow import Ui_MainWindow import classes from PIL import Image, ImageQt renderedImages = [] allImages = [] class mainWindow(QtWidgets.QMainWindow): def __init__(self): super(mainWindow, self).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(self) def openFile(): global allImages getFile = QtWidgets.QFileDialog() openedFile, t = getFile.getOpenFileNames(None, "Open Image", "~", "Image Files (*.png *.jpg *.jpeg *.gif *.webp);;Project Files (*.ifx)") # Get file to open if openedFile.count(".ifx") == 0 and openedFile: for i in range(len(openedFile)): allImages.append(openedFile[i]) # Pass new images to be rendered # Add opened files to recent file cache renderImage(allImages[0]) def renderImage(imageR): global drawn drawn = QtWidgets.QLabel(window) im = Image.open(imageR) qIm = ImageQt.ImageQt(im) pixmap = QtGui.QPixmap.fromImage(qIm) drawn.setPixmap(pixmap) drawn.resize(pixmap.width(), pixmap.height()) global renderedImages renderedImages.append(imageR) if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) window = mainWindow() refreshWindow() window.show() # Load main window window.ui.actionOpen.triggered.connect(openFile)I have checked to make sure that it does correctly fetch the path of the opened images and pass it to the render function and this is the case, the code in its current form also does not throw any errors.
-
Hi,
Your
drawnwidget is never shown.That said, please stop using globals for everything you are doing.
Take the time to learn how to build proper widgets and use signals and slots cleanly.
Take a look at the image viewer example for a start.There are many others.
-
S SamuraiDestroy has marked this topic as solved