将QMimeData粘贴到另一个窗口的QTextEdit

时间:2013-03-23 22:00:38

标签: python qt pyqt pyqt4 pyside

我还是Python和PyQt的新手,所以我有一个非常基本的问题。我在QTextEdit小部件的父窗口中有一些文本和图像,我试图将所有内容复制到子窗口的QTextEdit。但由于某种原因,我无法复制图像 - 只复制文本而不复制图像。这是代码片段给我带来的麻烦:

self.textEdit.selectAll()
data = self.textEdit.createMimeDataFromSelection()
self.child_window.textEdit.insertFromMimeData(data) # doesn't work with images

这是我尝试运行的小程序:

import sys
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyWindow(QtGui.QWidget):

    def __init__(self,parent=None):
        super(MyWindow,self).__init__(parent)

        self.textEdit = QtGui.QTextEdit(self)
        self.textEdit.setText("Hello World\n")

        self.pushButton = QtGui.QPushButton(self)
        self.pushButton.setText("Copy and paste to Child Window")
        self.pushButton.clicked.connect(self.click_copy_data)

        self.pushButton2 = QtGui.QPushButton(self)
        self.pushButton2.setText("Insert Image")
        self.pushButton2.clicked.connect(self.click_file_dialog)

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.textEdit)
        self.layoutVertical.addWidget(self.pushButton2)
        self.layoutVertical.addWidget(self.pushButton)

        self.setGeometry(150, 150,640, 480)
        self.child_window = CustomWindow(self)
        self.child_window.show()

    def click_copy_data(self):

        self.textEdit.selectAll()
        data = self.textEdit.createMimeDataFromSelection()
        self.child_window.textEdit.insertFromMimeData(data)

    def click_file_dialog(self):
        filePath = QtGui.QFileDialog.getOpenFileName(
            self,
            "Select an image",
            ".",
            "Image Files(*.png *.gif *.jpg *jpeg *.bmp)"
        )

        if not filePath.isEmpty():
            self.insertImage(filePath)


    def insertImage(self,filePath):

        imageUri = QtCore.QUrl(QtCore.QString("file://{0}".format(filePath)))
        image    = QtGui.QImage(QtGui.QImageReader(filePath).read())

        self.textEdit.document().addResource(
            QtGui.QTextDocument.ImageResource,
            imageUri,
            QtCore.QVariant(image)
        )

        imageFormat = QtGui.QTextImageFormat()
        imageFormat.setWidth(image.width())
        imageFormat.setHeight(image.height())
        imageFormat.setName(imageUri.toString())

        textCursor = self.textEdit.textCursor()
        textCursor.movePosition(
            QtGui.QTextCursor.End,
            QtGui.QTextCursor.MoveAnchor
        )
        textCursor.insertImage(imageFormat)

        # This will hide the cursor
        blankCursor = QtGui.QCursor(QtCore.Qt.BlankCursor)
        self.textEdit.setCursor(blankCursor)

class CustomWindow(QtGui.QDialog):
    def __init__(self,parent=None):
        super(CustomWindow,self).__init__(parent)

        self.textEdit = QtGui.QTextEdit(self)

        self.layoutVertical = QtGui.QVBoxLayout(self)
        self.layoutVertical.addWidget(self.textEdit)


if __name__ == "__main__":

    app = QtGui.QApplication(sys.argv)
    app.setApplicationName('MyWindow')
    main = MyWindow()
    main.show()

    sys.exit(app.exec_())

程序的工作方式是在主窗口中有一些文本,然后插入图像。然后单击"复制并粘贴到子窗口"按钮,它应该将所有内容粘贴到孩子,包括图像 - 但它没有按照它应该的方式工作 - 文本被复制但我得到一个小文件图标,图像应该是

我很感激你的帮助。

2 个答案:

答案 0 :(得分:1)

QTextEdit默认情况下不解码图片MIME类型,因此只需将其子类化以添加支持,您需要重新实现canInsertFromMimeDatainsertFromMimeData,同时尝试QTextBrowser而是。只需将其添加到您的脚本中:

class MyTextBrowser(QtGui.QTextBrowser):
    def __init__(self, parent=None):
        super(MyTextBrowser, self).__init__(parent)

        self.setReadOnly(False)

    def canInsertFromMimeData(self, source):
        if source.hasImage():
            return True

        else:
            return super(MyTextBrowser, self).canInsertFromMimeData(source)

    def insertFromMimeData(self, source):
        if source.hasImage():
            image = QtCore.QVariant(source.imageData())

            document = self.document()
            document.addResource(
                QtGui.QTextDocument.ImageResource,
                QtCore.QUrl("image"),
                image
            )

            cursor = self.textCursor()
            cursor.insertImage("image")

        super(MyTextBrowser, self).insertFromMimeData(source)

并在两个小部件上将self.textEdit = QtGui.QTextEdit(self)更改为self.textEdit = MyTextBrowser(self)

答案 1 :(得分:0)

这是我按照X.Jacobs建议使用的解决方案。

html = parent_textEdit.toHtml()
child_textEdit.setHtml(html)

我让事情变得更复杂。当我意识到QTextEdit跟踪图像存储在由toHtml()生成的html内部的URL时,那么这一切都是有意义的。