Function to set Text as superscript or subscript or change the text color of particular alphabet



  • I would like to set my Text as superscript or subscript or change the text color of particular alphabet. Is there any in-built function in QT. I found examples to change the background color of particular alphabet, But it didn't help me
    Note: I am not using any form or qml for design.



  • Hi!

    I am not using any form or qml for design

    So, how do you render your text?



  • @Wieland here is my code

        textWin = new QLabel(parent);
        textWin->setGeometry(xPos, yPos, widthWin, heightWin);
        textWin->setText("Welcome");// here i pass text
    

    I use this QLabel in many places and change the text according to my need. In some places I need subscript example: O2



  • QLabel supports a subset of HTML. You can use sub and sup tags:

    ui->label->setText("<sub>Low</sub>Normal<sup>High</sup>");
    



  • @Wieland
    But how should I declare ui



  • I just copy & pasted that from my editor, you don't need a ui object:

    textWin = new QLabel(parent);
    textWin->setGeometry(xPos, yPos, widthWin, heightWin);
    textWin->setText("<sub>Low</sub>Normal<sup>High</sup>");// here i pass text
    


  • @Wieland Thanks it worked. But I also need for color change for any particular alphabet. Could you help me



  • Same, you can use HTML:

    setText("<sub><font color='purple'>Low</font></sub>Normal<sup><font color='orange'>High</font></sup>")
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.