Important: Please read the Qt Code of Conduct -

style sheets with centralWidget?

  • Inside my MainWindow constructor I have:

    setCentralWidget(new mainCont(this)); //a dummy widget to serve as a container to put other widgets into
    centralWidget()->setStyleSheet("#mainContName {background-image: url(./myBG.bmp)}");

    I am trying to set myBG.bmp as the background image of the centralWidget().

    This code does not work. If I were to take out "centralWidget()" and replace it with "this" to target the MainWindow, I can see that the code does work if I did want to apply it to MainWindow (but I don't). So I know my syntax is correct, it's just that I can't get it to work on centralWidget().

    If I take out "#mainContName" and the {}, it successfully applies the image to centralWidget() (yay!), however... it also applies it to every child within, which I do not want (hence why I wanted to specifically target an object my name so it only applies to that one object).

    Any ideas?

    I've checked the inheritance of the QWidget class to make sure it allows the usage of setObjectName() and setStyleSheet(), so no problems there. The documentation at suggests exactly what I'm doing from what I can see, so I'm not sure where I'm going wrong.

  • @Darkware When you say "replace it with this" do you mean for the setObjectName too or just for the setStyleSheet ?

  • For both of them. It demonstrates that setStyleSheet() successfully targets MainWindow via the object name I give it and applies the style sheet, which leads to the conclusion that my syntax appears to work for MainWindow, but not for centralWidget()

  • @Darkware Try just replacing it just for setStyleSheet, the css might be looking only among the children of centralWidget for the name.

  • Doesn't work.

  • Moderators

    The code from your first post works as expected for me (except I used a QWidget instead of mainCont).

    Can you tell us which Qt version you're using and what is mainCont?

  • Qt 5.5.1
    Qt Creator 3.5.1

    class mainCont : public QWidget

        mainCont(QWidget *parent);


  • I put
    trace = new QLineEdit(objectName());
    inside the mainCont() constructor to see if the setObjectName() was executing properly. If I do setObjectName("mainContName"); within mainCont(), trace prints the name out correctly. However, if I try to set the object name by going centralWidget()->setObjectName("mainContName"); from my MainWindow constructor, trace is empty. So something is wrong with how I'm doing centralWidget->.

    setStyleSheet("QWidget#mainContName {background-image: url(./myBG.bmp)}");
    inside mainCont() doesn't work though, despite setObjectName("mainContName"); working inside of it.

  • Moderators

    What is lay->? Sounds like you've got some problem with how you setup your widgets, unrelated to stylesheets.
    Could you post the contents of your main window constructor? You're probably setting the content and then overwriting it with something else.

    Btw. please surround code blocks with ```. It formats a lot better.

  • mainWinClass::mainWinClass()

    mainCont *myMainCont = new mainCont(this);
    myMainCont->setStyleSheet("#mainContName {background-image: url(./myBG.bmp)}");


    mainCont::mainCont(QWidget *parent)

    QVBoxLayout *lay = new QVBoxLayout();
    trace = new QLineEdit(objectName()); //prints "mainContName" correctly.
    //other code here


Log in to reply