Results 1 to 3 of 3

Thread: set an image behind all child widgets

  1. #1
    Join Date
    Sep 2016
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default set an image behind all child widgets

    Hi all,

    I am trying to put a background image to a qmainwindow (or better a widget inheriting from qmainwindow). What I know is

    window->setStyleSheet("backgour-image:url(myimage.jpg);");

    but this somehow tries to "paint" the image on all the widgets which are children to window.

    What I WANT to do is is having each widget paint its own space and then the parent window paint any space not taken up by a child.
    To make it more precise. Assume that in the constructor of window we have the following:

    QPushButton * button1 = new QPushButton("Button 1", this);
    QPushButton * button2 = new QPushButton("Button 2", this);
    QVBoxLayout * layout = new QVBoxLayout(this);

    layout->addWidget(button1);
    layout->addWidget(button2);

    this->setLayout(this->layout);
    /* code to add background image */

    If i write this->setStyleSheet("background-image:url(myimage.jpg);"), it will try to put my image on any child of this. What i want is having button1 and button2 having their classical view and in the space between them that layout gives them, myimage is painted (of course in any other place that window might get.

    I am pretty sure my question is very simple but i hope that someone takes some time to write me

    Thank you very much

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: set an image behind all child widgets

    A QMainWindow has only one widget as its content, the "centralWidget", which then contains everything else.

    So you could e.g. use a QLabel as the type of the centralWidget and set the image as its pixmap.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2016
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: set an image behind all child widgets

    thanks,

    yeah i think i tried sth similar in other occasions and it worked.

    But I found a perhaps better solution using stylesheets by naming the mainwidget with setQObjectName and then giving the #name as directive in QStyleSheets. continuing on the previous example

    this->setObjectName("self");
    this->setStyleSheet("#self { border-image:url(myimage.jpg);");

    (if i m perfectly honest i didnt try it in Qt but with pyqt5, but i think it should work with C++ as well)

    But again thanks

Similar Threads

  1. Child widgets
    By poporacer in forum Newbie
    Replies: 6
    Last Post: 12th August 2010, 03:04
  2. Replies: 5
    Last Post: 18th April 2010, 23:31
  3. How to get the child widgets from a Widget?
    By prykHetQuo in forum Qt Programming
    Replies: 2
    Last Post: 29th January 2009, 13:26
  4. Child Widgets In MainWindow
    By RY in forum Newbie
    Replies: 3
    Last Post: 4th October 2008, 08:39
  5. setClipPath on child widgets.
    By bunjee in forum Qt Programming
    Replies: 9
    Last Post: 27th May 2007, 19:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.