Important: Please read the Qt Code of Conduct -

QTextEdit and setStyle

  • Hi
    I am trying to set a background image on a QTextEdit using setStyleSheet -

    @textEdit->setStyleSheet(QString::fromUtf8("background-image: url(:/images/Chrysanthemum.png);background-attachment: fixed"));@

    However, this doe not work.

    Am i misunderstanding the documentation or is it not possible?


  • You need to add this line:

  • textEdit is of type QTextEdit not QTextLineEdit

  • Just out of curiousity have you tried this:
    textEdit->setStyleSheet("background-color: red);

    and if that works fine try (of course your image file needs to be in the same dir as your sources then)
    textEdit->setStyleSheet("background-image: url(Chrysanthemum.png); background-attachment: fixed");

    if that works too I'd guess something is wrong with your way of setting the file location.

  • Hi
    @textEdit->setStyleSheet("background-color: red);@
    works fine
    @edit->setStyleSheet("background-image: url(bg.png); background-attachment: fixed;");@
    Does not!
    What can be wrong

  • It is working perfectly well on my computer:
    @ui->textEdit->setStyleSheet("background-image: url(:/nedge.png)");@

    Perhaps, you have not added your image to the resource file. Add "bg.png" to resource file and it should work.

  • Thats very strange indeed, because an example from the "Qt Style Sheets Examples": is pretty much the same
    QTextEdit, QListView {
    background-color: white;
    background-image: url(draft.png);
    background-attachment: fixed;

    My only guess is that somehow the file location is not correct. The bg.png file is located inside your source folder?

  • OK
    Here is my complete code, the image bg.png exists in the directory where the app is being run but is not set as the background
    @#include <QtGui>

    int main(int argc, char **argv)
    QApplication app(argc, argv);

     QTextEdit* edit = new QTextEdit();
     edit->setWindowTitle("QTextEdit Background Image");
     edit->setStyleSheet("background-image: url(bg.png)");
     return app.exec();


  • There is a mistake in the syntax:
    @edit->setStyleSheet("background-image: url(bg.png)");@

    It should be:
    @edit->setStyleSheet("background-image: url(:/bg.png)");@
    and "bg.png "must be" added to resource file and should in source directory

  • Hi
    I still cannot get this working!
    This is my .pro file
    @TEMPLATE = app
    TARGET =


    SOURCES += main.cpp

    I used QCreator to add this image to the resource file
    but still no joy


  • I suppose there is only one way I can help you now. Make a zip file of your project and upload the download link so that i can download the project and look for the problem.

  • Hi Adnam
    Thanks for your help!
    Forgive my ignorance (im new here) but how do I send you my project?

  • I recieved your project , i will look into it

  • Message sent


  • You had committed a silly mistake in adding the image to resource file. The prefix should had been "/" but it was "/images" in your case (it is also correct but) your folder containing the image was also named as "image", so the address of image should have been "/images/images/bn.jpg". It is working fine now. I have changed the prefix to "/" from "/images". So now the valid address is "/images/bn.jpg"

  • Hi
    Thanks, but it still does not work for me
    There must be something different in our systems, but I dont know what!
    do you have any ideas?

  • As you said it is not working under windows but working under linux perhaps It’s the windows style that prevents it then.

  • I take it that you are using Linux then?

  • Yes i use ubuntu

  • I also have similar problems like GTK+ style of linux does not allow for changing background color of progress bar

Log in to reply