Best way to insert background image in Form



  • Dear

    I want to know what is the best way (most optimal) to insert a background image on a form.

    1. Use stylesheet in Form (border-image property)? Then I need to change the border-image property of all widgets containing the form.

    2. Inserted on a label with the image (background-image)

    3. other?

    Tanks All!!!!



  • What about setting a custom QPalette to that widget? You could set a custom QBrush created with one of the constructors that takes a QPixmap or QImage using QPalette::setBrush.


  • Qt Champions 2016

    Just a note:

    • Use stylesheet in Form (border-image property)?
    • Then I need to change the border-image property of all widgets containing the form.

    Style sheets are cascading so if you apply to parent, all childs can be affected.



  • @mrjj said in Best way to insert background image in Form:

    are cascading

    Tanks for your reply. I know that Style sheets are cascading so if you apply to parent, all childs can be affected. This is the optimal way? 1,2 or 3? Tanks



  • @raspe88 Hi, Tanks for your reply. Could you give me an example?



  • @MarceloGomez Here's some short example for the QBrush-constructor using QImage:

    #include <QApplication>
    #include <QBrush>
    #include <QHBoxLayout>
    #include <QPalette>
    #include <QPushButton>
    #include <QWidget>
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
    
        QWidget widget;
        QPalette palette = widget.palette();
        const QBrush brush(QImage(":/ImageFromResources"));
        palette.setBrush(QPalette::Background, brush);
        widget.setPalette(palette);
    
        QHBoxLayout *layout = new QHBoxLayout(&widget);
        widget.setLayout(layout);
        layout->addWidget(new QPushButton(&widget), 0);
        layout->addWidget(new QWidget(&widget), 1);
    
        widget.setVisible(true);
    
        return app.exec();
    }
    

    As you can see, even if the QPalette is set before adding/parenting new widgets, it's only applied to the widget where you set it. That's because the global default QPalette is used when creating a new instance of a QWidget. Changing the global QPalette would be done using QApplication::setPalette .


Log in to reply
 

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