Results 1 to 7 of 7

Thread: Set QLabel text color via customizable script

  1. #1
    Join Date
    Feb 2021
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Set QLabel text color via customizable script

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,885
    Thanks
    275
    Thanked 794 Times in 784 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Set QLabel text color via customizable script

    QLabel::setText() supports strings formatted using Rich Text Format, 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?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2021
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Set QLabel text color via customizable script

    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.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,885
    Thanks
    275
    Thanked 794 Times in 784 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Set QLabel text color via customizable script

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Feb 2021
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Set QLabel text color via customizable script

    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.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,885
    Thanks
    275
    Thanked 794 Times in 784 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Set QLabel text color via customizable script

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Mar 2021
    Posts
    2
    Qt products
    Qt Jambi
    Platforms
    Windows

    Default Re: Set QLabel text color via customizable script

    Thanks guys, found a lot of valuable advices!

Similar Threads

  1. Replies: 5
    Last Post: 18th August 2014, 21:48
  2. Replies: 6
    Last Post: 16th April 2013, 08:30
  3. How to install text color of an QLabel?
    By tumanovalex in forum Newbie
    Replies: 2
    Last Post: 24th September 2010, 11:41
  4. QPushbutton + QLabel text color
    By Ashish in forum Qt Programming
    Replies: 5
    Last Post: 3rd February 2007, 08:13
  5. Setting text color on QLabel
    By Yorma in forum Newbie
    Replies: 11
    Last Post: 15th June 2006, 07:25

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.