Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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?

    Thanks



  • You need to add this line:
    textEdit->setFrame(false);



  • 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":http://qt-project.org/doc/qt-4.8/stylesheet-examples.html#customizing-specific-widgets 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)");
    
     edit->show();
     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 =
    DEPENDPATH += .
    INCLUDEPATH += .

    Input

    SOURCES += main.cpp

    RESOURCES +=
    res.qrc
    @
    I used QCreator to add this image to the resource file
    but still no joy

    frustrated



  • 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

    Thanks



  • 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