Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Best way to insert background image in Form
Forum Updated to NodeBB v4.3 + New Features

Best way to insert background image in Form

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 3.4k Views 1 Watching
  • 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.
  • MarceloGomezM Offline
    MarceloGomezM Offline
    MarceloGomez
    wrote on last edited by
    #1

    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!!!!

    1 Reply Last reply
    0
    • R Offline
      R Offline
      raspe88
      wrote on last edited by raspe88
      #2

      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.

      MarceloGomezM 1 Reply Last reply
      1
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        MarceloGomezM 1 Reply Last reply
        1
        • mrjjM mrjj

          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.

          MarceloGomezM Offline
          MarceloGomezM Offline
          MarceloGomez
          wrote on last edited by
          #4

          @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

          1 Reply Last reply
          0
          • R raspe88

            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.

            MarceloGomezM Offline
            MarceloGomezM Offline
            MarceloGomez
            wrote on last edited by
            #5

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

            R 1 Reply Last reply
            0
            • MarceloGomezM MarceloGomez

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

              R Offline
              R Offline
              raspe88
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved