Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Arrow label positioning code, label shoots off into hyperspace, has anyone done this before?
Qt 6.11 is out! See what's new in the release blog

Arrow label positioning code, label shoots off into hyperspace, has anyone done this before?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 1 Posters 496 Views 1 Watching
  • 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.
  • enjoysmathE Offline
    enjoysmathE Offline
    enjoysmath
    wrote on last edited by enjoysmath
    #1

    Here is some explanation behind the theoretical math.

    Here is the relevant code, short and sweet:

        def updateTextPosition(self):
            from geom_tools import mag2D
            from PyQt5.QtGui import QTransform
            src = self.sourcePoint()
            tar = self.targetPoint()
            if src and tar:
                dx = src.posDelta()
                dy = tar.posDelta()
                #c = self.pointCenter()
                x = src.pos() #- c
                y = tar.pos() # - c
                #dc = c - self.lastPointCenter()
                if x != y:
                    x0 = x - dx 
                    y0 = y - dy
                    magx = mag2D(x0)
                    magy = mag2D(y0)
                    T = QTransform(x.x(), y.x(), x.y(), y.y(), 0, 0) #dc.x(), dc.y())
                    for label in self.allLabels():
                        p = label.pos() #- c
                        u = QPointF.dotProduct(x0, p)
                        v = QPointF.dotProduct(y0, p)
                        if abs(magx) > 0:
                            u /= magx
                        if abs(magy) > 0:
                            v /= magy
                        p = QPointF(u, v)
                        label.setPos(T.map(p))
    

    As you can see I've also tried keeping the positioning relative to the arrow's control-point center.

    self.sourcePoint() returns the controlpoint that touches the node at the source end of the arrow and likewise for self.targetPoint().

    So all I'm trying to do is do a properly proportioned transform of each labels text.

    Before I did it by storing text-positioning info for each of the labels, very inelegant and that is now buggy, so I'm coming up with a simpler solution.

    Both the labels and the control points are parented by self (Arrow).

    "Hyperspace" is a good description and it whitesout the QGraphicsScene.


        def updateTextPosition(self):
            line = self.labelPosLine()
            line0 = self.lastLabelPosLine()
            p = line.p1()
            q = line.p2()
            p0 = line0.p1()
            q0 = line0.p2()
            dv = ((p-p0) + (q - q0))/2
            for label in self.allLabels():
                label.setPos(label.pos() + dv)
    

    Averaging looks okay but is not quite right! The arrow will sometimes swap sides when you rotate an arrow around one of its ends 180 degrees.

    https://github.com/enjoysmath
    https://math.stackexchange.com/users/26327/exercisingmathematician

    1 Reply Last reply
    0
    • enjoysmathE Offline
      enjoysmathE Offline
      enjoysmath
      wrote on last edited by
      #2

      Yeah, I know I'm doing it wrong. Just now discovered. I will post a solution to this.

      https://github.com/enjoysmath
      https://math.stackexchange.com/users/26327/exercisingmathematician

      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