Search code examples
pythonpyqtqpainterqtextedit

QtGui.QPainter overlay on top of QtGui.QTextEdit


I created a little Texteditor where users are able to increase font-size by hovering over a text-block and spinning their mousewheel. So far that part is working. My question is about visualizing the changes of the font-size.

I want to draw a circle at mouse-position and display the current font-size in it. So far I'm unsure how to overlay (Text is still readable) this on top of my QTextEdit.

I'm receiving a "QPainter::begin: Widget painting can only begin as a result of a paintEvent" Error.

import sys
from PyQt4 import QtGui, QtCore
import re
from itertools import chain

class Painter(QtGui.QWidget):
    def __init__(self, parent=None):
        super(Painter, self).__init__(parent)
        self.paintEvent()
        #the line above probably triggers the error

    def paintEvent(self):
        qp = QtGui.QPainter(self)
        self.drawCircle(qp)
        self.drawText(qp)

    def drawText(self, qp):
        qp.setPen(QtGui.QColor(0, 0, 0))
        qp.setFont(QtGui.QFont('Decorative', 32))
        st = SuperText()
        x = st.mouseX - 55
        y = st.mouseY + 16
        qp.drawText(x, y, "TEST")

    def drawCircle(self, qp):
        st = SuperText()
        x = st.mouseX
        y = st.mouseY
        qp.setBrush(QtGui.QColor(0, 0, 255))
        self.center = QtCore.QPoint(x, y)
        qp.drawEllipse(self.center, 80, 80)


class SuperText(QtGui.QTextEdit):

    def __init__(self):
        super(SuperText, self).__init__()
        #....

    def initUI(self):
        self.setGeometry(0, 0, 640, 480)
        self.setWindowTitle('SuperText')
        self.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.setMouseTracking(True)
        self.show()

    def wheelEvent(self, ev):
        super(SuperText, self).wheelEvent(ev)
        self.generateTemplate()
        self.renderTemplate()
        self.mouseX = ev.x()
        self.mouseY = ev.y()
        anc = self.anchorAt(ev.pos())
        if (anc):
            self.changeSize(anc, ev.delta())
            painter = Painter(self)
            painter.update()

    def onTextChanged(self):
        # update text when user inputs text
        self.text = self.toPlainText()
        self.readText()

def readText(self):
    data = self.text
    # split textfile at linebreak
    self.paragraphs = data.split("\n")
    for i in range(len(self.paragraphs)):
        # set standard size for each paragraph
        self.sizes.append(14)

def generateTemplate(self):
    p = self.paragraphs
    content = "".join(str(i) for i in chain(*p))
    if len(p) == 0:
        self.template = p
        return
    for i in range(len(p)):
        size = str(self.sizes[i])
        content = re.sub(str(p[i]),
                         "<a class='paragraph' href='%d' style='color:#000; text-decoration:none;'><p style='font-size:%spx'>$%d$</p></a>" % (i, size, i),
                         content, count=1)
    content = content + "<style>a:hover { background: #f00; }</style>"
    self.template = content

def renderTemplate(self):
    cur = self.textCursor()
    doc = self.template
    for i, paragraph in enumerate(self.paragraphs):
        doc = doc.replace('$' + str(i) + '$', '%s' % (paragraph))
    self.setHtml(doc)
    self.setTextCursor(cur)

def changeSize(self, paragraphId, amount):
    i = int(paragraphId)
    size = self.sizes[i]
    newSize = self.sizes[i] + (amount / 120)
    self.sizes[i] = newSize
    htmlCheck = self.toHtml()
    self.generateTemplate()
    self.renderTemplate()


def main():
    app = QtGui.QApplication(sys.argv)
    super_text = SuperText()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Can somebody explain what I am doing wrong or point me in the right direction?


Solution

  • I eliminated the Painter class and put it's code inside the SuperText class; editing the paintEvent() and the wheelEvent() i ended up with this:

    def paintEvent(self, event):
        qp = QtGui.QPainter()
        qp.begin(self.viewport())
        self.drawCircle(qp)
        self.drawText(qp)
        qp.end()
        super(SuperText, self).paintEvent(event)
        #the last line ensures the visiblity of the QTextedit
    

    and

    def wheelEvent(self, ev):
        self.generateTemplate()
        self.renderTemplate()
        self.mouseX = ev.x()
        self.mouseY = ev.y()
        anc = self.anchorAt(ev.pos())
        if (anc):
            self.changeSize(anc, ev.delta())
            self.update()