PDA

View Full Version : Style Sheets



yakuzan
28th May 2010, 19:08
Hey guys, I'm currently writing an application using only QtCreator to write every line by myself, anyways, I'm wondering if there's a more efficient way to set style sheets because currently I'm editing widgets with the "setStyleSheets" method but when I change a few properties it becomes unreadable, I'd like to load them from a file or something.

Like being able to set a style to a DockWidget with this:

QDockWidget{
background-color: rgb(0, 0, 0);
border-right-color: rgb(170, 255, 127);
}

QDockWidget::title{
background-color:pink;
background-image: url(:/newPrefix/dockTitleTile.png);
}

Oximoron
28th May 2010, 19:44
I'd like to load them from a file or something.
Save your style sheet in to a text file, and load it using QApplication::setStyleSheet().
Then you can just change the file, and you application will look different, that is the clean way to do it.

Lykurg
28th May 2010, 20:43
...and put the file in the Qt resource system, then the application will always find the file.:

QFile file(":/yourCss.qss");
file.open(QIODevice::ReadOnly | QIODevice::Text);
setStyleSheet(file.readAll());
file.close();

yakuzan
28th May 2010, 20:47
Here's the method provided inside the Qt Doc's for anyone interested.


void MainWindow::loadStyleSheet(const QString &sheetName)
{
QFile file(":/" + sheetName.toLower() + ".qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());

setStyleSheet(styleSheet);
}


Thanks again.

yakuzan
29th May 2010, 04:23
Sorry about the double post, felt like I could just re-use this thread instead.

So I have a custom button class and I've created buttons and added them inside a regular CustomButton array:

for(int x=0 ; x < size_x ; x++){
for(int y=0 ; y < size_y ; y++){

buttonGrid[x][y] = new OthelloButton();
centralGrid->addWidget(buttonGrid[x][y],x,y);
buttonGrid[x][y]->setObjectName("gridButton");
}
}

For some reason, it's not listening to any styles sheet, for example the following code doesn't change anything while it should:

#gridButton{
background-color:orange;
color:white;
}

I've been stuck on this for some time, couldn't find any answer.

Edit: Fixed, when I took some example code on the Qt doc's there was un-commented text between two declarations

Lykurg
29th May 2010, 09:11
It is not the best idea to have a widget with objects which have all the same name. So if all your custom buttons should have that look, better write: OthelloButton {/*...*/}