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. QPixmap: Must construct a QGuiApplication before a QPixmap after copying a picture to clipboard
Forum Updated to NodeBB v4.3 + New Features

QPixmap: Must construct a QGuiApplication before a QPixmap after copying a picture to clipboard

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 2.5k Views 3 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.
  • WolleKetteW Offline
    WolleKetteW Offline
    WolleKette
    wrote on last edited by WolleKette
    #1

    Hi at all,

    I want to take a screenshot in my application and copy it to the clipboard. This also works. But when I've done that at least once, I get an error and python crashes when I close the application.

    QPixmap: Must construct a QGuiApplication before a QPixmap
    

    I have already found many posts about the above error message on the Internet, but unfortunately no solution yet. I use PySide6 - 6.2.3 on a Mac.

    Here is a small example to reproduce the problem.

    import sys
    from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
    
    class MainWindow(QWidget):
        def __init__(self):
            super().__init__()
    
            btn_screenshot = QPushButton('Take screenshot to clipboard', self)
            btn_screenshot.clicked.connect(self.take_screenshot)
            btn_screenshot.move(50, 50)
    
            self.setGeometry(100, 100, 300, 150)
            self.setWindowTitle('Window Example')
    
            self.show()
    
    
        def take_screenshot(self):
            p = self.grab()
            clipboard = QApplication.clipboard()
            clipboard.setPixmap(p)
    
            QMessageBox.information(self, 'Message', 'Screenshot is copied to clipboard', QMessageBox.Ok)
    
    
    app = QApplication(sys.argv)
    ex = MainWindow()
    sys.exit(app.exec_())
    
    1 Reply Last reply
    0
    • SGaistS SGaist

      Do you have the same issue if you change your pixmap to a QImage and use setImage in place ?

      WolleKetteW Offline
      WolleKetteW Offline
      WolleKette
      wrote on last edited by
      #5

      @SGaist
      No, if I convert the QPixmap to a QImage, then it works without problems. Also under the older PySide6 version. Here is the code if someone has the same problem...

              pixmap = self.grab()
              image = pixmap.toImage()
      
              clipboard = QApplication.clipboard()
              clipboard.setImage(image)
      
      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        How did you install PySide6 ?
        On which version of macOS ?
        Can you try with a more recent version of PySide6 ?

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

        WolleKetteW 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          How did you install PySide6 ?
          On which version of macOS ?
          Can you try with a more recent version of PySide6 ?

          WolleKetteW Offline
          WolleKetteW Offline
          WolleKette
          wrote on last edited by
          #3

          @SGaist said in QPixmap: Must construct a QGuiApplication before a QPixmap after copying a picture to clipboard:

          Can you try with a more recent version of PySide6 ?

          No problem thanks to virtual environments. In Version 6.5.1.1, the error message has changed to

          qt.qpa.clipboard: Cannot keep promise, data contains QPixmap and requires livining QGuiApplication
          

          I use macOS Ventura 13.4.1. While I'm at it, I also tested it on Windows (10). There it runs without error message.

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

            Do you have the same issue if you change your pixmap to a QImage and use setImage in place ?

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

            WolleKetteW 1 Reply Last reply
            0
            • SGaistS SGaist

              Do you have the same issue if you change your pixmap to a QImage and use setImage in place ?

              WolleKetteW Offline
              WolleKetteW Offline
              WolleKette
              wrote on last edited by
              #5

              @SGaist
              No, if I convert the QPixmap to a QImage, then it works without problems. Also under the older PySide6 version. Here is the code if someone has the same problem...

                      pixmap = self.grab()
                      image = pixmap.toImage()
              
                      clipboard = QApplication.clipboard()
                      clipboard.setImage(image)
              
              1 Reply Last reply
              1
              • WolleKetteW WolleKette has marked this topic as solved on

              • Login

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