Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Is it possible to color parts of "QGraphicsTextItem" individually?

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

Scheduled Pinned Locked Moved Solved Qt for Python
5 Posts 2 Posters 790 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MS_GX
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • M MS_GX

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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.

      1 Reply Last reply
      2
      • M Offline
        M Offline
        MS_GX
        wrote on last edited by
        #3

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

        JonBJ 1 Reply Last reply
        0
        • M MS_GX

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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • M Offline
            M Offline
            MS_GX
            wrote on last edited by
            #5

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

            1 Reply Last reply
            1

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved