[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.
-
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).
-
@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()@ -
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();
}@