March 12, 2012

GrahamL GrahamL
Hobby Entomologist
218 posts

QTextEdit and setStyle

Page  
1

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

  1. 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

19 replies

March 12, 2012

adnan adnan
Lab Rat
276 posts

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

March 12, 2012

GrahamL GrahamL
Hobby Entomologist
218 posts

textEdit is of type QTextEdit not QTextLineEdit

March 12, 2012

KA51O KA51O
Hobby Entomologist
478 posts

Just out of curiousity have you tried this:

  1. 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)

  1. 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.

March 12, 2012

GrahamL GrahamL
Hobby Entomologist
218 posts

Hi

  1. textEdit->setStyleSheet("background-color: red);

works fine
  1. edit->setStyleSheet("background-image: url(bg.png); background-attachment: fixed;");

Does not!
What can be wrong

March 12, 2012

adnan adnan
Lab Rat
276 posts

It is working perfectly well on my computer:

  1. 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.

March 12, 2012

KA51O KA51O
Hobby Entomologist
478 posts

Thats very strange indeed, because an example from the Qt Style Sheets Examples [qt-project.org] is pretty much the same

  1.      background-color: white;
  2.      background-image: url(draft.png);
  3.      background-attachment: fixed;
  4.  }

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

March 12, 2012

GrahamL GrahamL
Hobby Entomologist
218 posts

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

  1. #include <QtGui>
  2.  
  3. int main(int argc, char **argv)
  4. {
  5.      QApplication app(argc, argv);
  6.      
  7.      QTextEdit* edit = new QTextEdit();
  8.      edit->setWindowTitle("QTextEdit Background Image");
  9.      
  10.      edit->setStyleSheet("background-image: url(bg.png)");
  11.  
  12.      edit->show();
  13.      return app.exec();
  14. }

March 12, 2012

adnan adnan
Lab Rat
276 posts

There is a mistake in the syntax:

  1. edit->setStyleSheet("background-image: url(bg.png)");

It should be:

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

March 12, 2012

GrahamL GrahamL
Hobby Entomologist
218 posts

Hi
I still cannot get this working!
This is my .pro file

  1. TEMPLATE = app
  2. TARGET =
  3. DEPENDPATH += .
  4. INCLUDEPATH += .
  5.  
  6. # Input
  7. SOURCES += main.cpp
  8.  
  9. RESOURCES += \
  10.     res.qrc

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

frustrated

March 12, 2012

adnan adnan
Lab Rat
276 posts

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.

March 12, 2012

GrahamL GrahamL
Hobby Entomologist
218 posts

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

March 12, 2012

adnan adnan
Lab Rat
276 posts

I recieved your project , i will look into it

March 12, 2012

GrahamL GrahamL
Hobby Entomologist
218 posts

Message sent

Thanks

March 12, 2012

adnan adnan
Lab Rat
276 posts

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”

March 13, 2012

GrahamL GrahamL
Hobby Entomologist
218 posts

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?

Page  
1

  ‹‹ Problem with QAudioInput::byteReady() and QIODevice::read()      Is QT Quick better than QT Gui for Desktop applications ››

You must log in to post a reply. Not a member yet? Register here!