Problem with uploading an image as background to window



  • Hi, I am doing a very simple setting of the background-image style property of the MainWindow in my app to load a *.png file as follows but when I run the simulator the image is not loaded. The image.png is at the same level as my project directory. Has any one run into this? This is such a basic functionality, and simple instruction, can't see what the problem is?
    By the way, I read also about the *.qrc Resource file, where you need to tell where you image resource is if you want to compile it with your code, but I don't think i need this if I am just using the simulator and loading the image locally right?

    thanks

    -Malena

    @
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    a.setStyleSheet("MainWindow { background-image: url(image.png) }");  
    
    return a.exec()
    

    }
    @

    EDIT: please mark cod by placing @-rags, gerolf



  • Hi,

    To set a background of the main window add the source code to the MainWindow constructor.

    @MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
    {
    setStyleSheet("background:transparent;");
    }@

    The following example makes the background transparent. You can modified it by adding @background-image:url('image.png');@ to set a background image.



  • Yeah, you would think that the setting the "background-image" style property would work but it does not work. I already went that route before I posted my question

    -Malena



  • Hi Malena,

    Although it is possible that "background-image" style property does not work correctly it is very unlikely. Please make sure that the background image exists as a file on the device (or simulator) and that the path is correct.

    Could you share details about Qt version and SDK that you are using? On which platform are you testing it (Symbian, MeeGo or something else)?

    Cheers,
    Leon



  • Hi Leon,

    After debugging some more, and reading about the Resources file *.qrc, I thought that probably the problem was that the simulator was not finding the image file. This is yesterday before you sent your email. So I made several discoveries, after testing several different setups.

    1. When simulating for a "Symbian" target you need to tell the compiler where your resources are by creating a *.qrc file, adding the file name in the *.pro configuration, and also you need to compile the *qrc with the rcc.exe. Without embedding your resources in the binary, the simulator does not find your file, even if you give the path in the C:/ harddisk of the machine.

    2. Stylesheet("background-image: (:path_to_image) does not work even after implementing the *.qrc file, and compiling the rcc.

    3. What works is to to use the Pixmap class to load the image and then the Painter class to display the image. The code i used is as follows, and it has to be inside the paintEvent() method of the widget you are attaching the image as a background. The paintEvent gets called automatically when the widget gets created. The code looks like this:

    @
    void MyWidget::paintEvent(QPaintEvent *pe)
    {
    QPixmap pixmap;
    pixmap.load(":/images/image.png");
    QPainter paint(this);
    paint.drawPixmap(0,0, pixmap);
    }

    @

    1. I found on the web a blog from one person that also said that the "background-image" property does not work for widgets. And this person also used the Pixmap/Painter classes to make the display of an image work.

    -Malena



  • Hi Malena,

    Congratulations for solving the issue by drawing pixmap.

    I have to managed to set a background using background-image too! Indeed your problem was caused by the path to file.

    Using the Qt resource system you embed resource files in the application's executable file. That is why you cannot access the image even if you give the path in the C:/ harddisk of the machine. You should set a path like :/images/image.png just like you did using QPixmap.

    I tested background-image inside my application that has qrc file. I have added the following code to constructor of the class what extends QMainWindow:

    @setStyleSheet("background-image:url(':/arrow.png');");@

    As a result the image arrow.png is displayed on the background as expected:
    !http://anavi.org/images/tests/QtBackgroundImage.png(background-image example)!

    Best regards,
    Leon



  • Hi Leo,

    I tried again using setStylesheet("background-image:..." ") inside the MainWindow constructor to make sure once more that it doesn't work for me (with the *.qrc file), and indeed it doesn't.

    I am using the latest qt sdk 1.1. recently released, are you using the same?

    If you could, could you email me your code to try it on my end because I don't understand why it works for you and not for me. So I want to try your code as is.

    thanks

    -Malena



  • Hi,

    I have tested it using SDK 1.0 with Qt 4.6.3. I cannot sent you my source code as I have tested it inside my application Good Luck with is published to Ovi and at for the moment I prefer to keep the source private. But if you want you can forward me your application and I will have a look.

    Cheers,
    Leon



  • Hi Leo,

    this is weird. I was recreating a new project to send it to you to check it out, and now the StyleSheet("background-image: ") works. I don't understand , but I am glad you asked me to send you a copy. In the process of cleaning up the file, I probably deleted some line that was preventing things to work. Anyways, thank you.

    -Malena



  • Hi Malena,

    Cool :) It is important that you have solved the issue. Glad that I was able to help.

    Best regards,
    Leon


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.