Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Is it possible to color parts of "QGraphicsTextItem" individually?



  • I am using Qt5 via PySide2.

    I need to present long stretches of DNA sequences (like "ATTTGCCTACGG...") as text. I present similar sequences aligned to each other (as strings vertically stacked) on a QGraphicsScene using QGraphicsTextItem. This works for me, although already on this step I am not sure if this is the optimal solution.

    Anyway, now I want to colour some parts of the strings with a different colour. So let's say "GCC" within the string above has to be red instead of black. Is it possible to do this while still handling the entire sequence as one QGraphicsTextItem object? I tried to piece together the DNA sequence using several QGraphicsTextItem objects and colouring them accordingly. But this gives me problems regarding properly aligning the subparts. Also, it would be helpful if the entire DNA sequence is selectable and if I stitch it together various QGraphicsTextItem objects this might be harder as well.

    So would you suggest an entirely different solution? Or if I should go with QGraphicsTextItem how do I do this?



  • @MS_GX
    If you were doing this in, say, a QLabel you could & should use HTML markup around each character to change color on each one, e.g. ATT<span style="color:red;">GCC</span>CGG. Since I see that QGraphicsTextItem does indeed accept rich text/HTML (via setHtml()) you should do so here.



  • @MS_GX
    If you were doing this in, say, a QLabel you could & should use HTML markup around each character to change color on each one, e.g. ATT<span style="color:red;">GCC</span>CGG. Since I see that QGraphicsTextItem does indeed accept rich text/HTML (via setHtml()) you should do so here.



  • Awesome! Exactly what I needed. If you have a wish-list anywhere or I can send you a beer let me know how ;D



  • @MS_GX
    Top of my wish list is Kylie Minogue, so if you are an Australian with any influence to arrange that.... ;-)



  • Haha... let me just call my contacts. I'll see what I can do! Thank you again in the meantime ,)


Log in to reply