QPixmap: Must construct a QGuiApplication before a QPixmap after copying a picture to clipboard
-
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_())
-
@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)
-
Hi,
How did you install PySide6 ?
On which version of macOS ?
Can you try with a more recent version of PySide6 ? -
@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.
-
Do you have the same issue if you change your pixmap to a QImage and use setImage in place ?
-
@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)
-
W WolleKette has marked this topic as solved on