Create a clickable QTextEdit with a pixmap in a label as background

542 Views Asked by At

I need my QTextEdit (or correct: QTextBrowser) to have a background image.

    self.background=QLabel()
    self.background.move(0,0)
    self.background.setPixmap(QPixmap("/Icons/bg-text.png"))
    self.background.show()

    self.textedit = QTextBrowser(self)

    self.background.setParent(self.textedit)

The problem is: The image is above the text. So I can't click on the text to set the cursor position, nor can I select the text. How can i solve this or make a workaround?

I know that this problem occurs with the image, because i have a similar one, where i make a QLabel (also with an image) that has pushButtons, and if I create the pushButtons after the image, it works (= I can click the buttons). But here with the textedit, the QTextBrowser can't be added to itself like the buttons to the QLabel. I tried to solve this by setting the parent after creating the background, but it doesn't work.

Edit:

My ideas right now: 1: If the QLabel would be click-through-able, it would probably work. Don't know how to do that. 2: Passing the click-event to the self.textedit, same here.

Maybe those are good points to start from?

1

There are 1 best solutions below

0
On

Oh yes I got it.

You can set the background image with a stylesheet, and even with a QLinearGradient over it. Then you can click the text correctly.

    self.textedit.setStyleSheet("""QTextBrowser{background: QLinearGradient(x1:0,y1:0,x2:0,y2:1,stop:0 #fff, stop: 0.9 #123);
                                                background-image: url("/home/Icons/bg-text.png");
                                                color: #3159b3}""")