Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QWidgets Pixmap not appearing
Qt 6.11 is out! See what's new in the release blog

QWidgets Pixmap not appearing

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 2 Posters 175 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SamuraiDestroy
    wrote last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote last edited by
      #2

      Hi,

      Your drawn widget 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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • S SamuraiDestroy has marked this topic as solved

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved