Search code examples
c++qtstylesheetqtstylesheets

Editing QSS Stylesheet programmatically


What I want to do is that I have a QSS stylesheet in specific folder. And I want to edit selected elements from GUI, programmatically. Since QT doesn't support SaSS or LeSS, I need to do this "manually". What I have until now is read a qss file and with few if statments I can get the value of each element that placed on qss file.

QTextStream in(qssFile);
line = in.readLine();
if(line.startsWith("QDialog"))
{
     int start = line.indexOf("{") + 1;
     int end = line.indexOf("}", start);
     qDebug() << "QDialog" << line.mid(start, end - start); //"background-color: #404040;"
}

I read the line and if user changes the values on the gui, I save the new ones. of course this one isn't the best/right solution but how I can make a better solution to this issue I have?

Update 1: the sample code I have added it may work, but seems like a "junk" code with 7-8 if statements for each line. What I need is if I can set variables to each elements in stylesheet such as:

QDialog{ 
   background-color: @dialogBackgroundColor; 
   min-width: @dialogMinWidth;
}

by that I can set the values into the temp stylesheet and apply it.


Solution

  • There is a terrible asymmetry between the dynamic C++ setters and getters for several (but far from all) properties that can be set via QSS.

    To make the problem worse, as you noticed, there is no decent way to edit the active stylesheet other than refreshing the whole thing, which means reloading everything. To top it all off, there is no easy programmatic way to actually edit the stylesheet once loaded. It's a structured string, and for parsing and modifying it, you need to resort to boilerplate code, written by you.

    Not using stylesheets isn't an option either, as various properties cannot be set without them.