Home > OS >  Is it possible to add background image on QWidget without using QtCreator?
Is it possible to add background image on QWidget without using QtCreator?

Time:09-22

Having trouble adding background image on the widget, even though I referenced the recent codes online.

This is my current code for main.cpp:

#include <QApplication>
#include <QWidget>

int main (int argc, char **argv){
    QApplication app (argc,argv);
    QWidget *w=new QWidget();
    w->setStyleSheet("background-image: url(:/cover.jpg);");
    w->setWindowTitle("Test");
    w->show();
    return app.exec();
}

After executing the code, how come the widget remains blank? Thanks in advance!

CodePudding user response:

"Is it possible to add background image on QWidget without using QtCreator?"

Yes, of course it is.

QtCreator is just an IDE. You don't need to use it at all to write code using the Qt library. Just as you can use it to write code that does not use Qt at all.

CodePudding user response:

QtCreator is an IDE designed by Qt. It's just an interface.

I checked your implementation and I don't see anything wrong. It also work well on my pc. Can you check your image url or try with another image ?

Btw, if you're on linux, try removing : character after url ;

w->setStyleSheet("background-image: url(/cover.jpg);");

EDİT: If jpg is in the same directory with your application, it should be ;

w->setStyleSheet("background-image: url(./cover.jpg);");

You can give a full path to avoid this kind of errors.

  • Related