[SOLVED] Adding image to a layout



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



  • you can use stylesheets for that , eg

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

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



  • 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



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



  • 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("");
    @



  • 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).



  • 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



  • @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()@



  • Hi ManasQt!

         I'm sorry but I'm using C++ and I suppose your code is Python. 


  • 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();
    

    }@



  • Thank you! I will try it out today :)


Log in to reply
 

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