1

I'd like to get raw text without style information when copying from a QPlainTextEdit. Currently when pasting into excel from a QPlainTextEdit there is style information included. I'm using PySide2.

Here's an image of excel illustrating the issue

I initially set the background color on column A to yellow and pasted on top of this column.

I've tried re-implementing createMimeDataFromSelection but if I create a new QMimeData, set the text and return it the program crashes with no error. After some further testing this method does work, and does not crash when using PyQt5 rather than PySide2. Maybe this is a bug with PySide2.

    def createMimeDataFromSelection(self):
        mime = QMimeData()
        text = self.textCursor().selectedText()
        mime.setText(text)
        return mime #Crashes

If I use super() to get the parent's QMimeData and use setText() and setHtml() to override the text, when I copy I still get the original text with styling:

    def createMimeDataFromSelection(self):
        mime = super().createMimeDataFromSelection()

        mime.clear()
        mime.setData("text/plain", b"")
        mime.setText(self.textCursor().selectedText())
        mime.setHtml("")

        return mime #Copied string still contains styling information

Here's a full example showing the standard a QPlainTextEdit, one that tries to replace the mime data provided by the parent class, and one that tries to create it's own mime data.

import sys
from PySide2.QtWidgets import QWidget, QPlainTextEdit, QApplication, QVBoxLayout
from PySide2.QtCore import QMimeData


class TextEditRemoveMime1(QPlainTextEdit):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def createMimeDataFromSelection(self):
        mime = super().createMimeDataFromSelection()
        mime.clear()
        mime.setData("text/plain", b"")
        mime.setText(self.textCursor().selectedText())
        mime.setHtml("")
        return mime


class TextEditRemoveMime2(QPlainTextEdit):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def createMimeDataFromSelection(self):
        mime = QMimeData()
        text = self.textCursor().selectedText()
        mime.setText(text)
        return mime


class Example(QWidget):
    def __init__(self):
        super().__init__()
        vbox = QVBoxLayout()

        # Has styling information
        plainTextEdit = QPlainTextEdit(self)
        vbox.addWidget(plainTextEdit)

        # Still has styling information
        plainTextEdit = TextEditRemoveMime1(self)
        vbox.addWidget(plainTextEdit)

        # Crashes in PySide2, but does remove styling and does not crash in PyQt5
        plainTextEdit = TextEditRemoveMime2(self)
        vbox.addWidget(plainTextEdit)

        self.setLayout(vbox)
        self.show()


if __name__ == "__main__":

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
DGriffin
  • 11
  • 1
  • 2
  • Provide an [MRE], I want to understand what kind of styling content you mean – eyllanesc Aug 29 '19 at 01:08
  • what is *styling content*? – eyllanesc Aug 29 '19 at 16:54
  • @eyllanesc Just added a minimal reproducible example. The styling content is mime data that is dictating the font, color, etc... It turns out that this can be made to work in PyQt5. Perhaps it's a bug with PySide2. I added info about this to the question. – DGriffin Aug 29 '19 at 16:55
  • You can add to your code the part that adds the style and an image of what you get when it is pasted in the excel document – eyllanesc Aug 29 '19 at 16:57
  • I'm only trying to remove the style content from the Mime data not add any. I'll add an image illustrating the style content. – DGriffin Aug 29 '19 at 16:59
  • I want to know how you place the styling, for example you can set the font in HTML or used QFont, depending on it maybe the solution changes. – eyllanesc Aug 29 '19 at 17:01
  • You can show how you set the style in QPlainTextEdit – eyllanesc Aug 29 '19 at 17:16
  • I am not using HTML or QFont to add style information in the QPlainTextEdit. The style information is already associated by default. My example fully illustrates this issue as it pertains to my application: That when text is copied out of a vanilla QPlainTextEdit the mime data has style information associated with it. This style information will overwrite any existing style information in excel (unlike copying from notepad, sublime, this comment editor in chrome, etc...). I believe that the mime data in the clipboard is using HTML to dictate the style. – DGriffin Aug 29 '19 at 17:22
  • Okay, so you never change the default style of QPlaintextEdit and hitting it in excel breaks your style. I am right? – eyllanesc Aug 29 '19 at 17:25
  • @eyllanesc Correct. – DGriffin Aug 29 '19 at 17:27
  • I think it's a PySide2 bug, report it – eyllanesc Aug 30 '19 at 18:23
  • 1
    Done. https://bugreports.qt.io/browse/PYSIDE-1082 – DGriffin Aug 30 '19 at 19:13

0 Answers0