Victor Aprodu
Victor Aprodu

Reputation: 33

how can I oveload paste on a QtextEdit

I want to paste in a QTextEdit an text with an certain font size, ex. 14

I made an app that replace a paraghaph sign with a blank space, like in PyQt QLineEdit and 'paste' event?

On def __init__(self) I code:

self.textEdit.textChanged.connect(self.valueChanged)

then

def valueChanged(self, text):
    if QtGui.QApplication.clipboard().text() == text:
        self.pasteEvent(text)

and then

def pasteEvent(self, text):
    text.toUpper()
TypeError: valueChanged() takes exactly 2 arguments (1 given)

Upvotes: 3

Views: 1078

Answers (1)

eyllanesc
eyllanesc

Reputation: 244003

In the previous question that links you use a QLineEdit that has the void QLineEdit::textChanged(const QString &text) signal that carries the text, but in the case of QTextEdit there is a signal with the same name void textChanged() but it does not carry the text so that is the cause of the error. The solution for that case is to obtain the text using the object and not through the signal.

def valueChanged(self):
    if QtGui.QApplication.clipboard().text() == self.textEdit.text():
        self.pasteEvent(text)

Although if your goal is to change the size of the font then your previous logic does not work since you are detecting the event after the text is pasted, if you want to modify something during the paste then you must override the insertFromMimeData() method:

from PyQt4 import QtCore, QtGui


class TextEdit(QtGui.QTextEdit):
    def insertFromMimeData(self, source):
        last_font = self.currentFont()

        new_font = QtGui.QFont(last_font)
        new_font.setPointSize(14)
        self.setCurrentFont(new_font)
        super(TextEdit, self).insertFromMimeData(source)
        self.setCurrentFont(last_font)


if __name__ == "__main__":
    import sys

    app = QtGui.QApplication(sys.argv)

    w = TextEdit()
    w.show()
    sys.exit(app.exec_())

Upvotes: 1

Related Questions