Merge two pieces of code
-
Below is the original code (source: https://stackoverflow.com/questions/34220275/how-to-select-a-region-with-qrubberband-on-a-qlabel-like-in-ksnapshot). I changed "PyQt4" -> "PyQt5" and "QtGui" -> "QtWidgets". Should I change anything else? Any ideas??
from PyQt4 import QtGui, QtCore class RubberbandEnhancedLabel(QtGui.QLabel): def __init__(self, parent=None): QtGui.QLabel.__init__(self, parent) self.selection = QtGui.QRubberBand(QtGui.QRubberBand.Rectangle, self) def mousePressEvent(self, event): ''' Mouse is pressed. If selection is visible either set dragging mode (if close to border) or hide selection. If selection is not visible make it visible and start at this point. ''' if event.button() == QtCore.Qt.LeftButton: position = QtCore.QPoint(event.pos()) if self.selection.isVisible(): # visible selection if (self.upper_left - position).manhattanLength() < 20: # close to upper left corner, drag it self.mode = "drag_upper_left" elif (self.lower_right - position).manhattanLength() < 20: # close to lower right corner, drag it self.mode = "drag_lower_right" else: # clicked somewhere else, hide selection self.selection.hide() else: # no visible selection, start new selection self.upper_left = position self.lower_right = position self.mode = "drag_lower_right" self.selection.show() def mouseMoveEvent(self, event): ''' Mouse moved. If selection is visible, drag it according to drag mode. ''' if self.selection.isVisible(): # visible selection if self.mode is "drag_lower_right": self.lower_right = QtCore.QPoint(event.pos()) elif self.mode is "drag_upper_left": self.upper_left = QtCore.QPoint(event.pos()) # update geometry self.selection.setGeometry(QtCore.QRect(self.upper_left, self.lower_right).normalized()) app = QtGui.QApplication([]) screen_pixmap = QtGui.QPixmap.grabWindow(app.desktop().winId()) window = QtGui.QWidget() layout = QtGui.QVBoxLayout(window) label = RubberbandEnhancedLabel() label.setPixmap(screen_pixmap) layout.addWidget(label) geometry = app.desktop().availableGeometry() window.setFixedSize(geometry.width(), geometry.height()) window.show() app.exec_()
-
@john_hobbyist said in Merge two pieces of code:
File "code_2.py", line 333, in <module> screen_pixmap = QtWidgets.QPixmap.grabWidget(app.desktop()) AttributeError: module 'PyQt5.QtWidgets' has no attribute 'QPixmap'
QPixMap
is not a widget so it won't be inQtWidgets
. It will be inQtGui
. As per https://doc.qt.io/qt-5/qpixmap.html.I changed "PyQt4" -> "PyQt5" and "QtGui" -> "QtWidgets".
You are supposed to be selective about that. Nobody said everything in
QtGui
changed toQtWidgets
between 4 & 5. Just widgetty stuff! -
@JonB: Hi, as I posted previously:
Traceback (most recent call last): File "code_2.py", line 333, in <module> screen_pixmap = QtGui.QPixmap.grabWidget(app.desktop()) NameError: name 'QtGui' is not defined Segmentation fault (core dumped)
I have also tried this....
-
You did not import the module.
-
@SGaist: Something else than these?
import os import sys import cv2 from matplotlib.figure import Figure import numpy as np from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import (QMainWindow, QApplication, QWidget,QAction, QFileDialog, QMenu, QToolBar, QHBoxLayout, QTreeView, QFileSystemModel, QSizePolicy, QMessageBox) from PyQt5.QtCore import Qt, QDir, QStandardPaths, QFileInfo import matplotlib.backends.backend_qt5agg as mpl_qt from PyQt5 import QtWidgets, QtCore from PyQt5.QtGui import QPixmap
-
You import QPixmap directly so use it directly as well.
-
@SGaist: I used this:
screen_pixmap = QPixmap.grabWidget(app.desktop())
I get this:
Traceback (most recent call last): File "code_2.py", line 335, in <module> screen_pixmap = QPixmap.grabWidget(app.desktop()) AttributeError: type object 'QPixmap' has no attribute 'grabWidget' Segmentation fault (core dumped)
I followed this: https://stackoverflow.com/questions/29260686/qpixmap-has-no-attribute-grabwindow
and I changed to this:
screen_pixmap = QScreen.grabWidget(app.desktop())
and I get this:
Traceback (most recent call last): File "code_2.py", line 336, in <module> screen_pixmap = QScreen.grabWidget(app.desktop()) AttributeError: type object 'QScreen' has no attribute 'grabWidget' Segmentation fault (core dumped)
When I changed to this:
screen_pixmap = QScreen.grabWindow(app.desktop().winId())
I get this:
Must construct a QApplication first. Traceback (most recent call last): File "code_2.py", line 337, in <module> screen_pixmap = QScreen.grabWindow(app.desktop().winId()) AttributeError: 'NoneType' object has no attribute 'winId' Segmentation fault (core dumped)
When I changed to this:
screen_pixmap = QScreen.grabWindow(app.desktop())
I get this:
Must construct a QApplication first. Traceback (most recent call last): File "code_2.py", line 338, in <module> screen_pixmap = QScreen.grabWindow(app.desktop())#.winId()) TypeError: grabWindow(self, PyQt5.sip.voidptr, x: int = 0, y: int = 0, width: int = -1, height: int = -1): first argument of unbound method must have type 'QScreen' Segmentation fault (core dumped)
I didn't find something about this...
-
@john_hobbyist
The last answer in the stackoverflow link you quoted claims to have "Full example for PyQt5". Did you at least try that? Not your own version of it, just exactly the example given there. -
@JonB Yes, it works, but I try different rubberband code...please look at my first posts up...
-
This post is deleted!
-
What modifications should I do in the command?
-
I am trying to modify/incorporate from the correct running code from the stackoverflow that @JonB suggested. With this:
screen_pixmap = QScreen.grabWindow(app.primaryScreen(),QApplication.desktop().winId()).save(filename, 'png')
I get this:
Must construct a QApplication first. Traceback (most recent call last): File "code_2.py", line 340, in <module> screen_pixmap = QScreen.grabWindow(app.primaryScreen(),QApplication.desktop().winId()).save(filename, 'png') AttributeError: 'NoneType' object has no attribute 'winId' Segmentation fault (core dumped)
And with this
screen_pixmap = QScreen.grabWindow(app.primaryScreen(),QApplication.desktop()).save(filename, 'png')
I get again this:
construct a QApplication first. Traceback (most recent call last): File "code_2.py", line 341, in <module> screen_pixmap = QScreen.grabWindow(app.primaryScreen(),QApplication.desktop()).save(filename, 'png') TypeError: grabWindow(self, PyQt5.sip.voidptr, x: int = 0, y: int = 0, width: int = -1, height: int = -1): first argument of unbound method must have type 'QScreen' Segmentation fault (core dumped)
in which I cannot find a reliable solution from google/stackoverflow...
-
@john_hobbyist said in Merge two pieces of code:
grabWindow
This is not a class method, you need an instance of QScreen.
In what way is that related to your original QRubberBand question ?
-
I already answered to the errors you are getting.
@SGaist said in Merge two pieces of code:
This is not a class method, you need an instance of QScreen.
You really should read the documentation of the classes you are using rather than just blindingly copy paste.
-
I have been making many changes all day. The rest of the application runs, but nowhere I can see a rubberband rectangle in order to select/printscreen an area. I get these warnings though...I am searching it, but any idea would help...
QWidget::paintEngine: Should no longer be called QPainter::begin: Paint device returned engine == 0, type: 1 QPainter::end: Painter not active, aborted
-
Where exactly are you doing custom painting ?
-
I don't know! This is the modified last part of my code:
if __name__ == "__main__": app = QApplication(sys.argv) icon = QIcon(os.path.join(selfDir, '_static', 'orthoview.ico')) app.setWindowIcon(icon) from PyQt5.QtWidgets import * data = take_screenshot() window = QWidget() layout = QtWidgets.QVBoxLayout(window) pixmap = QPixmap() pixmap.loadFromData(data) label = QtWidgets.QLabel() label.setPixmap(pixmap) layout.addWidget(label) geometry = app.desktop().availableGeometry() window.setFixedSize(geometry.width(), geometry.height()) window = OrthoView() window.show() sys.exit(app.exec_())
I used/modified this:
import sys from PySide2 import QtCore, QtGui, QtWidgets def take_screenshot(): from PyQt5 import QtCore as pyqt5c from PyQt5 import QtWidgets as pyqt5w screen = pyqt5w.QApplication.primaryScreen() winid = pyqt5w.QApplication.desktop().winId() pixmap = screen.grabWindow(winid) ba = pyqt5c.QByteArray() buff = pyqt5c.QBuffer(ba) pixmap.save(buff, "PNG") return ba.data() if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) data = take_screenshot() pixmap = QtGui.QPixmap() pixmap.loadFromData(data) label = QtWidgets.QLabel() label.setPixmap(pixmap) label.show() sys.exit(app.exec_())
(source: https://stackoverflow.com/questions/59118938/type-error-when-calling-qscreen-grabwindow)
-
@john_hobbyist
Maybe I misunderstand, but you calltake_screenshot()
as the very first thing you do, before you have created or shown anything at all? What would you expect from that, logically?EDIT
OK, you're trying to screenshot what's already on the screen? Sorry, I misunderstood. Still seems very early but may be correct. Ignore this post if it is :)I'm not sure I see anything here to do with custom painting. Do you use a debugger? Do you step through your code to find out where your problems come from? We need you to (learn to) do that, people here don't know where the errors come in your code.