PDA

View Full Version : Set QLabel text color via customizable script



coproc
26th February 2021, 10:32
We want to customize the display of a dialog using QUiLoader Class and customer specific ui-files. Within the text of the QLabel-s we are thinking of allowing certain placeholders like $(serial), $(type) that can be replaced by the program with current data. But how can we change the style (color) of the text inside QLabel based on customer specific rules like "{ color : [$(defectCount) > 3 ? 'red' : 'black']; }"? We could use QJSEngine and parse for defined delimiters to spot the script parts in the QLabel text, pass them to a QJSEngine and replace them with the result. Is there a simpler way?

Thanks for any hints.

d_stranz
26th February 2021, 15:39
QLabel::setText() supports strings formatted using Rich Text Format (https://doc.qt.io/qt-5/richtext.html), a subset of HTML. Use that to customize the label text.

But you ask your customers to be programmers and enter things in some arcane script instead of providing them a nice UI-driven procedure that writes the arcane script behind the scenes?

coproc
9th March 2021, 14:13
The customization stays with us (no need for the customer to program the color logic). We want to keep the core logic (data generation) separate from the presentation, which looks different for different customers. This presentation/customization part is only a small part in our (huge) customer projects (involving hardware etc.). Implementing a GUI for customization does not pay off since our total number of customers is relatively small. And we want one executable for all customers. So dynamically loading the ui-file seems the way to go. Since we have a predefined set of data that shall be presentable in a generic way, we want to use a not so arcane script language to implement the color logic.

d_stranz
9th March 2021, 16:13
So I guess if I were to do this, I would create an XML file that contained an element for each customizable label, and each of those elements would have the unique object name for the label as an attribute so you could use QObject::findChild() to locate it after the UI has been built but before it has been shown. The content for each element is the literal Rich Text HTML that you would then set on the label once you had found it. You would provide a customer-specific XML file to each customer.

Using a customizable XML file has big advantages: 1) you don't need to invent a language, 2) you don't need to load the UI file at run time, 3) you can use Qt's excellent XML libraries to read and process it rather than having to write your own parser, 4) you can probably use QXmlQuery to handle the customization substitutions made to the Rich Text string, 5) providing an external XML file rather than the actual UI file is much less risky. If the customer breaks the XML file, the customization doesn't work. If the customer breaks the UI file, the entire application breaks.

I am a big XML fan, I guess you can tell.

coproc
10th March 2021, 08:55
Thank you fo sharing your thoughts on this. One ui-file for all customers does not give us enough flexibility for choosing and positioning the needed information in the way expected by the customer. And for the "new langauge": currently I do not see any other way for putting conditions into Rich Text HTML than by inserting JavaScript marked by special delimiters. Defining an XML structure like the one you propose is also a bit like defining a new language ... (in the sense that you need your own code to interpret and apply the configuration)

> I am a big XML fan, I guess you can tell.
I was, too. But often XML is an overkill (see the complexity of the parsers! and all these escape sequences ...). Currently we use JSON whenever possible.

d_stranz
10th March 2021, 15:36
Well, you know your needs better than the rest of us. I thought what you were asking for was a simple way to customize text labels, but it sounds like you need to customize major parts of the UI.

djokovich
11th March 2021, 08:58
Thanks guys, found a lot of valuable advices!