Python 如何制作小部件的屏幕截图并将其粘贴到QGraphicsView中?

Python 如何制作小部件的屏幕截图并将其粘贴到QGraphicsView中?,python,pyqt5,qt5,qgraphicsview,Python,Pyqt5,Qt5,Qgraphicsview,我在表单上有两个QGraphicsView和一个QWebEngineView小部件,我需要*(点击按钮1)*对QWebEngineView中的内容制作一个屏幕截图,并将该屏幕截图保存为图像,同时将其保留为对象,并将此对象粘贴到第一个QGraphicsView和*(单击按钮2)*将保存的图像插入第二个QGraphicsView 编辑: (我想在QtWebEngineWidgets中创建一个区域的屏幕截图,并将该区域作为一个对象。第二个问题是,我需要知道如何以两种不同的方式将该区域粘贴到QGraph

我在表单上有两个
QGraphicsView
和一个
QWebEngineView
小部件,我需要*(点击按钮1)*对
QWebEngineView
中的内容制作一个屏幕截图,并将该屏幕截图保存为图像,同时将其保留为对象,并将此对象粘贴到第一个
QGraphicsView
和*(单击按钮2)*将保存的图像插入第二个
QGraphicsView

编辑: (我想在QtWebEngineWidgets中创建一个区域的屏幕截图,并将该区域作为一个对象。第二个问题是,我需要知道如何以两种不同的方式将该区域粘贴到QGraphicsView中:(从文件中)并将其显示为对象而不保存。2 QGraphicsView仅用于研究目的,它可能只是一个QGraphicsView,点击按钮1屏幕截图正在制作并粘贴为QGraphicsView的对象,点击按钮2-屏幕截图正在制作(保存为png并加载到QGraphicsView))

以下是我的代码:

import sys, os
from PyQt5.QtCore    import Qt
from PyQt5.QtGui     import QBrush, QPen, QScreen, QPixmap
from PyQt5.QtWidgets import QApplication, QStyleFactory, QMainWindow, QWidget, QVBoxLayout, QLabel
from PyQt5.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsItem, QPushButton
from pyqtgraph.Qt    import QtCore, QtGui
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication, QDialog

class Geometry(QGraphicsView):
    def __init__(self):
        QGraphicsView.__init__(self)


class CentralPanel(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.lblCoords = QLabel('MAP SELECTOR (object image):  ')
        self.lblCoords2 = QLabel('MAP SELECTOR (loaded from file image):  ')
        self.gvwShapes = Geometry()
        self.gvwShapes2 = Geometry()
        
        vbxDsply = QVBoxLayout()
        vbxDsply.addWidget(self.lblCoords)      # Capture coordinates of drawn line at window 1
        vbxDsply.addWidget(self.gvwShapes)      # add QGraphicsView #1
        
        vbxDsply.addWidget(self.lblCoords2)     # Capture coordinates of drawn line at window 2
        vbxDsply.addWidget(self.gvwShapes2)     # add QGraphicsView #2
        
        self.webEngineView = QWebEngineView()   # Add Google maps web window
        self.webEngineView.load(QtCore.QUrl("https://www.google.com/maps/@36.797966,-97.1413048,3464a,35y,0.92h/data=!3m1!1e3"))
        vbxDsply.addWidget(self.webEngineView)
        
        self.Button1 = QPushButton('Do screenshot of webEngineView save it and paste it into QGraphicsView2', self) # Button to load image to graphics view
        vbxDsply.addWidget(self.Button1)
        self.Button1.clicked.connect(self.button_screenshot)
        
        self.Button2 = QPushButton('Do screenshot of webEngineView and paste it into QGraphicsView1 ', self)
        vbxDsply.addWidget(self.Button2)
        self.Button2.clicked.connect(self.button_load_image)
        
        self.setLayout(vbxDsply)
        
        self.filename = "image.jpg"

    def button_screenshot(self):
        print('Screenshot is taken and saved as an image, Image loaded and inserted into the gvwShapes QGraphicsView1 ')
        app = QApplication(sys.argv)
        QScreen.grabWindow(app.primaryScreen(), QApplication.desktop().winId()).save(self.filename, 'png')

    def button_load_image(self):
        print('Screenshot is taken and inserted into the gvwShapes2 QGraphicsView2')
        
        # pix = QPixmap()
        # pix.load(self.filename)
        # pix = QPixmap(self.filename)
        # item = QGraphicsPixmapItem(pix)
        
        # scene = QGraphicsScence(self)
        # scene.addItem(item)
        # self.graphicsView.setScene(scene)
        
        scene = QtWidgets.QGraphicsScene(self)
        pixmap = QPixmap(self.filename)
        item = QtWidgets.QGraphicsPixmapItem(pixmap)
        scene.addItem(item)
        self.gvwShapes.setScene(scene)
        
        
        # scene = self.gvwShapes
        # self.image = QtGui.QPixmap(self.filename)
        # self.gvwShapes.add
        # scene.addItem(QtGui.QGraphicsPixmapItem(self.image))
        # self.view = self.QGraphicsView 
        # self.view.setScene(self.image)
        # self.view.show()
        
        
class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.setGeometry(200, 50, 700, 900) 
        self.setWindowTitle('MAP Selector REV01')
        self.setStyle(QStyleFactory.create('Cleanlooks'))
        self.CenterPane = CentralPanel()
        self.setCentralWidget(self.CenterPane)




# Catching exceptions and running a main loop
#
import traceback
def except_hook(exc_type, exc_value, exc_tb):
    tb = "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
    print("error cached")
    print("error message:\n", tb)

if __name__ == "__main__":
    MainEventThred = QApplication([])
    os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--enable-logging --log-level=3"
    sys.excepthook = except_hook
    MainApp = MainWindow()
    MainApp.show()
    MainEventThred.exec()

为QGraphicsView设置场景

self.gvwShapes = Geometry()
self.gvwShapes2 = Geometry()
self.gvwShapes.setScene(QGraphicsScene())
self.gvwShapes2.setScene(QGraphicsScene())
使用
QWidget.grab()
将其渲染为QPixmap,并使用
qgraphicscene.addPixmap()
将其添加到场景中。可以使用QRect指定区域

def button_screenshot(self):
    pixmap = self.webEngineView.grab(QRect(180, 100, 300, 280))
    pixmap.save(self.filename)
    self.gvwShapes2.scene().addPixmap(pixmap)

def button_load_image(self):
    self.gvwShapes.scene().addPixmap(QPixmap(self.filename))
QWebEngineView中的裁剪图像 如果要实现QWebEngineView屏幕截图的裁剪,则必须使用QWebEngineView的focusProxy()上的QRubberBand(它是呈现网页并接收显示视图后创建的鼠标事件的小部件)

从functools导入缓存的\u属性
导入系统
从PyQt5.QtCore导入pyqtSignal、QEvent、QObject、QPoint、QRect、QSize、QUrl
从PyQt5.QtGui导入QPixmap
从PyQt5.qtwidts导入QApplication、QLabel、QRubberBand
从PyQt5.QtWebEngineWidgets导入QWebEngineView
类RubberBandManager(QObject):
pixmap_changed=pyqtSignal(QPixmap,name=“pixmapChanged”)
定义初始化(自我,小部件):
super()。\uuuu初始化(小部件)
self._origin=QPoint()
self.\u widget=widget
self.widget.installEventFilter(self)
@财产
def小部件(自身):
返回self.\u小部件
@缓存的不动产
def橡胶带(自身):
返回QRubberBand(QRubberBand.Rectangle,self.widget)
def eventFilter(自身、源、事件):
如果self.widget是源:
如果event.type()==QEvent.mousebutton按下:
self.\u origin=event.pos()
self.rubberband.setGeometry(QRect(self.\u origin,QSize()))
self.rubberband.show()
elif event.type()==QEvent.MouseMove:
self.rubberband.setGeometry(
QRect(self._origin,event.pos()).normalized()
)
elif event.type()==QEvent.MouseButtonRelease:
rect=self.rubberband.geometry()
pixmap=self.widget.grab(rect)
self.pixmap_changed.emit(pixmap)
self.rubberband.hide()
return super().eventFilter(源,事件)
如果名称=“\uuuuu main\uuuuuuuu”:
app=QApplication(sys.argv)
视图=QWebEngineView()
view.load(
库尔(
"https://www.google.com/maps/@36.797966,-97.14130483464A,35岁,0.92小时/数据=!3m1!1e3“
)
)
view.show()
rubberband\u manager=rubberband管理器(view.focusProxy())
label=QLabel()
label.hide()
pixmap上的def已更改(pixmap):
label.setPixmap(pixmap)
label.adjustSize()
label.show()
rubberband\u manager.pixmap\u已更改。连接(在pixmap\u已更改上)
ret=app.exec()
系统退出(ret)
在QGraphicsView中显示图像 若要显示图像,则必须使用QGraphicsPixMap加载QPixmap的QGraphicsPixMap

导入系统 从PyQt5.QtCore导入Qt 从PyQt5.QtGui导入QPixmap、QPainter、Qpalete 从PyQt5.QtWidgets导入( QApplication, QZ视图, Qsscene, QX资本, ) 类图像查看器(QGraphicsView): def uuu init uuu(self,parent=None): super()。\uuuu init\uuuu(父级) self.setRenderInts(qPaint.Antialiasing | qPaint.SmoothPixmapTransform) self.setAlignment(Qt.AlignCenter) 自我挫折背景角色(Qpalete.Dark) 场景=qgraphicscene() self.setScene(场景) self.\u pixmap\u item=QGraphicsPixmapItem() 场景.附加项(自映射项) def load_pixmap(自身,pixmap): self.\u pixmap\u item.setPixmap(pixmap) self.fitToWindow() def fitToWindow(自身): self.fitInView(self.scen直立(),Qt.KeepAspectRatio) def resizeEvent(自我,事件): super().resizeEvent(事件) self.fitToWindow() 如果名称=“\uuuuu main\uuuuuuuu”: app=QApplication(sys.argv) 视图=图像查看器() 查看。调整大小(640480) view.show() pixmap=QPixmap(“image.jpg”) view.load_pixmap(pixmap) ret=app.exec() 系统退出(ret)
前面的小部件用于从文件加载图像:
pixmap=QPixmap(“/path/of/image”)
或使用pixmap提供的QPixmap\u changed signal of
RubberBandManager

我不明白第一个和第二个QGraphicsView的目的是什么,那么您当前的问题是什么?@eyllanesc的问题是:我不知道如何在QtWebEngineWidgets中对该区域进行截图,并将该区域作为对象。还有第二个问题,我需要知道如何以两种不同的方式将此区域粘贴到QGraphicsView中:(从文件中)并将其显示为对象而不保存。2 QGraphicsView仅用于研究目的它可能只是一个QGraphicsView,点击按钮1屏幕截图正在制作并粘贴为QGraphicsView的对象,点击按钮2-屏幕截图正在制作(保存为png并加载到QGraphicsView),我只需要知道