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()->setObjectName("mainContName");
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 http://doc.qt.io/qt-4.8/stylesheet.html suggests exactly what I'm doing from what I can see, so I'm not sure where I'm going wrong.
-
-
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?
-
I put
trace = new QLineEdit(objectName());
lay->addWidget(trace);
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->.Doing
setStyleSheet("QWidget#mainContName {background-image: url(./myBG.bmp)}");
inside mainCont() doesn't work though, despite setObjectName("mainContName"); working inside of it. -
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()
{
//menu
createActions();mainCont *myMainCont = new mainCont(this); setCentralWidget(myMainCont); myMainCont->setStyleSheet("#mainContName {background-image: url(./myBG.bmp)}");
}
mainCont::mainCont(QWidget *parent)
{
setObjectName("mainContName");QVBoxLayout *lay = new QVBoxLayout(); trace = new QLineEdit(objectName()); //prints "mainContName" correctly. lay->addWidget(trace); //other code here
}