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. [SOLVED] Adding image to a layout
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Adding image to a layout

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 25.5k 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.
  • H Offline
    H Offline
    holygirl
    wrote on last edited by
    #1

    On my mainwindow form, I have another form as the central widget which acts as a container for several other widgets that are part of a horizontal box layout. I want to add an image as the background for this layout. How do I go about adding a background to a layout (QHBoxLayout or QVBoxLayout)?

    Thanks.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      you can use stylesheets for that , eg

      @QMainWindow
      {
      background-image: url(:/files/Images/img.jpg);
      }

      QWidget#centralWidget
      {
      background-image: url(:/files/Images/img.jpg);
      }@

      1 Reply Last reply
      0
      • H Offline
        H Offline
        holygirl
        wrote on last edited by
        #3

        Thanks for the reply Sam. I am not looking to add a background image for the whole widget. Its only for a layout inside the widget. So the image would look like a ribbon at the bottom of the screen over which the widgets are placed

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tilsitt
          wrote on last edited by
          #4

          As Sam told you, you can use stylesheets for that. If it's only for a layout inside the widget, then just use stylesheet on that layout inside the widget with the correct selector.

          1 Reply Last reply
          0
          • H Offline
            H Offline
            holygirl
            wrote on last edited by
            #5

            But layouts don't have an option to set stylesheets. Do they? I mean, I'm using a horizontal layout inside my widget. I tried using stylesheets as

            @
            QLabel#insertimage
            {
            background-image: url(path.png);
            background-position: top left;
            }
            @

            and my source file has

            @
            mainlayout->setObjectName("insertimage");
            @

            But I'm not able to do something like

            @
            mainlayout.setStyleSheet("");
            @

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tilsitt
              wrote on last edited by
              #6

              Yes, my apologies. QLayout can't have a stylesheet. My english isn't perfect, so I didn't understand if your layout is empty or not. If your layout is not empty, one solution is to encapsulate the contents in a widget, then you can use stylesheet on it. If your layout is empty, add en empty widget to it, then you can use stylesheet (or add a QLabel and fill it with your image).

              1 Reply Last reply
              0
              • H Offline
                H Offline
                holygirl
                wrote on last edited by
                #7

                Thanks tilsitt. I actually have a lot of encapsulation going on in my class. So I was wondering if there was any way to apply the background other than re encapsulating it in a widget. Well, I guess not. Thank you again

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  ManasQt
                  wrote on last edited by
                  #8

                  @Well, you could do set Background image with QFrame,

                  PySide code:

                  class MyMainWindow(QFrame):
                  def init(self,parent=None):
                  QFrame.init(self, parent)

                      palette=QPalette()
                      pixmap=QPixmap("images/apples.gif")
                      brush=QBrush(pixmap)
                      frame_layout=QVBoxLayout()
                  
                      self.setFixedSize(300,300)
                      self.setFrameStyle(QFrame.Box)
                      self.setWindowTitle("QFrame Set Background Image Example")
                  
                      #set QFrame Border Width 4 pixels
                      self.setLineWidth(4)
                      self.setLayout(frame_layout)
                      
                      btn1=QPushButton(self)
                      btn1.setText("Button 1")
                      frame_layout.addWidget(btn1)
                      palette.setBrush(QPalette.Background,brush)
                      self.setPalette(palette)
                  

                  set things up, and run it. :)

                  if name == 'main':
                  app = QApplication(sys.argv)
                  w = MyMainWindow()
                  w.show()
                  app.exec_()
                  sys.exit()@

                  1 Reply Last reply
                  0
                  • H Offline
                    H Offline
                    holygirl
                    wrote on last edited by
                    #9

                    Hi ManasQt!

                         I'm sorry but I'm using C++ and I suppose your code is Python. 
                    
                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      ManasQt
                      wrote on last edited by
                      #10

                      Well , here you go!

                      @#include <QtGui>

                      int main(int argc, char **argv)
                      {
                      QApplication app(argc, argv);

                        QFrame*   frame    = new QFrame();
                        QPalette*  palette   = new QPalette(); 
                        QPixmap*   pixmap    = new QPixmap("images/apples.gif");
                        QBrush*   brush    = new QBrush(*pixmap);
                        QVBoxLayout*  frame_layout    = new QVBoxLayout(); 
                        
                        frame->setFixedSize(300,300);
                        frame->setFrameStyle(QFrame::Box);
                        frame->setWindowTitle("QFrame Set Background Image Example");      
                        //Set QFrame Border Width 4 Pixels
                        frame->setLineWidth(4);
                        frame->setLayout(frame_layout );
                      
                        frame_layout ->addWidget(new QPushButton("Click me!"));
                        
                        palette->setBrush(QPalette::Background,*brush);
                        frame->setPalette(*palette);  
                        
                        frame->show();      
                        
                        return app.exec();
                      

                      }@

                      1 Reply Last reply
                      0
                      • H Offline
                        H Offline
                        holygirl
                        wrote on last edited by
                        #11

                        Thank you! I will try it out today :)

                        1 Reply Last reply
                        0

                        • Login

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