Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    QTextEdit and setStyle

    General and Desktop
    3
    20
    10821
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • G
      GrahamL last edited by

      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

      1 Reply Last reply Reply Quote 0
      • A
        adnan last edited by

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

        1 Reply Last reply Reply Quote 0
        • G
          GrahamL last edited by

          textEdit is of type QTextEdit not QTextLineEdit

          1 Reply Last reply Reply Quote 0
          • K
            KA51O last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • G
              GrahamL last edited by

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

              1 Reply Last reply Reply Quote 0
              • A
                adnan last edited by

                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.

                1 Reply Last reply Reply Quote 0
                • K
                  KA51O last edited by

                  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?

                  1 Reply Last reply Reply Quote 0
                  • G
                    GrahamL last edited by

                    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();
                    

                    }@

                    1 Reply Last reply Reply Quote 0
                    • A
                      adnan last edited by

                      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

                      1 Reply Last reply Reply Quote 0
                      • G
                        GrahamL last edited by

                        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

                        1 Reply Last reply Reply Quote 0
                        • A
                          adnan last edited by

                          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.

                          1 Reply Last reply Reply Quote 0
                          • G
                            GrahamL last edited by

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

                            1 Reply Last reply Reply Quote 0
                            • A
                              adnan last edited by

                              I recieved your project , i will look into it

                              1 Reply Last reply Reply Quote 0
                              • G
                                GrahamL last edited by

                                Message sent

                                Thanks

                                1 Reply Last reply Reply Quote 0
                                • A
                                  adnan last edited by

                                  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"

                                  1 Reply Last reply Reply Quote 0
                                  • G
                                    GrahamL last edited by

                                    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?

                                    1 Reply Last reply Reply Quote 0
                                    • A
                                      adnan last edited by

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

                                      1 Reply Last reply Reply Quote 0
                                      • G
                                        GrahamL last edited by

                                        I take it that you are using Linux then?

                                        1 Reply Last reply Reply Quote 0
                                        • A
                                          adnan last edited by

                                          Yes i use ubuntu

                                          1 Reply Last reply Reply Quote 0
                                          • A
                                            adnan last edited by

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

                                            1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post