Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Creating a dynamic spline in QML
Forum Updated to NodeBB v4.3 + New Features

Creating a dynamic spline in QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
4 Posts 2 Posters 40 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.
  • J Offline
    J Offline
    Jay_emissary
    wrote last edited by Jay_emissary
    #1

    Hello friends,
    I'm trying to simulate this "note slide" effect as shown in the following image:
    a24c5995-467b-4bc8-a5dd-355362838878-image.png

    The idea is that I have two rectangles that represent the head and tail of the object, and I will draw a shape that follows a "spline" as the body. This puts me in unfamiliar territory, as I'm unsure of which QML functions work best for creating effects like this. However, I've already made an attempt!

    Qt's ShapePath seemed like a sound solution, but every new point I create deforms the shape to said point, which brings me further away from my objective. Below I have recreated this in an example project.

    import QtQuick
    import Qt5Compat.GraphicalEffects
    import QtQuick.Shapes
    Window {
        width: 640
        height: 480
        visible: true
        title: qsTr("Hello World")
        color: "black"
    
        Rectangle{
            id:rect1
            height: 40
            width: 40
            color: "red"
            x:400
            y:450
        }
        Rectangle{
            id: rect2
            height: 40
            width: 40
            color: "red"
            x:600
            y:0
        }
    
        Shape{
            anchors.fill: parent
    
            ShapePath{
                id: trailPath
                strokeWidth: 2
                strokeColor: "white"
                fillColor: "white"
                capStyle: ShapePath.FlatCap
                startX:  rect1.x+rect1.width/2; startY: rect1.y+rect1.height/2
                PathLine { id: pathline; x: trailPath.startX; y:(rect1.y+rect1.height/2)-rect1.height/2 } //line upwards (begins at rectangle's middle and stops at the rectangle's top
                PathCurve { x:rect2.x+rect2.width/2; y: (rect2.y+rect2.height/2) + rect2.height/2 } //line that curves to the next rectangle
                PathLine { x: rect2.x+rect2.width/2; y:rect2.y+rect2.height/2 } //line upwards (stops at the recangle's middle)
    
    
            }
        }
    
    
    }
    
    

    89a801a3-52e9-47ed-b591-a9926ddfc855-image.png

    This is close! But because the shapepath is a rectangle, I'd have to edit more points to make this look "rectangular," which increases the margin of error. Is there a simpler way of drawing lines on a path? The best case scenario would be where I have a singular line, and it follows the same path as before, but I just increase the stroke to manage the size.

    1 Reply Last reply
    0
    • GrecKoG Offline
      GrecKoG Offline
      GrecKo
      Qt Champions 2018
      wrote last edited by
      #3

      Use a transparent fillColor, you only care about the stroke.

      J 1 Reply Last reply
      0
      • J Offline
        J Offline
        Jay_emissary
        wrote last edited by
        #2

        Doesn't path only draw "lines" in between each point? I would use that, but it doesn't provide any visuals... I imagine that I could draw a shape following the path, but just like before, I'm not sure how to keep it relatively uniform throughout.

        1 Reply Last reply
        0
        • GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote last edited by
          #3

          Use a transparent fillColor, you only care about the stroke.

          J 1 Reply Last reply
          0
          • GrecKoG GrecKo

            Use a transparent fillColor, you only care about the stroke.

            J Offline
            J Offline
            Jay_emissary
            wrote last edited by
            #4

            @GrecKo Ah that was a little painfully obvious haha! Thank you for pointing that out. I've been banging my head against the wall learning about shapepaths for hours now, and thought that there was a more explicit object type for the job. Turns out it was right in front of me all along!

            1 Reply Last reply
            0
            • J Jay_emissary has marked this topic as solved

            • Login

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