Important: Please read the Qt Code of Conduct -

Possible to apply stylesheet to substring?

  • Hi folks,

    I am curious if it is possible to apply a stylesheet or something similar to a part of a substring. I'll give an example to try to clarify my overall goal.

    I am building a menu system for an embedded product which will allow a user to modify some configuration parameters. I will not have a mouse/keyboard, but rather something like a simple keypad with L, R, Select. When I enter a sub-menu I may have up to 5 fields to modify. For example, changing the static IP Address would be 4 fields - 1 for each byte.

    I've got my hardware in place to allow me navigate everything just fine. Pressing select once will 'focus' that field and let you modify, and pressing again will de-focus. When not focused, you can scroll left and right to select which field you wish to modify next.

    The problem I am facing is how I can place some sort of visual emphasis on the focused field. Currently I have 5 QStrings which may or may not be used depending on which menu I'm in. I populate these based on the current saved configuration and concatentate them into a single QString which is then placed into my QLabel via setText().

    That works fine, and I can use an underline as the cursor, but as soon as I select a field on which to focus I cannot figure out a way to emphasize that focus. I'd be fine with a different color text, or inverted colors or anything like that, but I can't see a way to apply this to a substring within a single QLabel.

    Sorry for my rambling. Hopefully my thoughts are coherent enough to make some sense!

  • I think I understand what you're asking, but I apologize if this isn't what you're looking for. Note, I have not tested this so I don't know if this will work, but:

    QLabel *label = new QLabel();
         label->setText("<font color='red'>My text</font>");

    Is that what you're trying to achieve?

  • Well, I feel silly.

    I was inserting the <u> tag without thinking I might be able to use other html-like tags such as <font>. I guess my new question is whether or not there is any sort of list of what I can or cannot use within a QString?

    edit Thanks Carmoneer! You wrote this up while I was smacking myself in the head for not thinking of doing this very thing earlier :-) That is exactly what I wanted to do.

  • Splendid! Glad to hear you were able to resolve your issue! As for that 'list' that you're seeking, one may exist, but I don't know of one. I just know of the good ol' handy-dandy QString Documentation :-) Cheers!

  • Lifetime Qt Champion

    What you are looking for is not related to QString. QString is just a string.
    Look here:

  • @jsulm said:

    What you are looking for is not related to QString. QString is just a string.
    Look here:

    Ah hah! Thank you, this will be a big help for me!

Log in to reply