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. How to design a triangle in qml?
Forum Update on Monday, May 27th 2025

How to design a triangle in qml?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 4 Posters 10.6k 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
    JasmineSethi
    wrote on last edited by
    #1

    I want to design a triangle in qml, how this can be done?
    Below is a simple example:
    0_1560854107812_08bc5914-e7a2-474c-80f6-40169163225c-image.png

    Note: I don't want to use image.

    J.HilkJ 1 Reply Last reply
    0
    • J JasmineSethi

      I want to design a triangle in qml, how this can be done?
      Below is a simple example:
      0_1560854107812_08bc5914-e7a2-474c-80f6-40169163225c-image.png

      Note: I don't want to use image.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      hi @JasmineSethi

      seems like a QML Canvas is what you're looking for

      https://doc.qt.io/qt-5/qml-qtquick-canvas.html


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • J Offline
        J Offline
        JasmineSethi
        wrote on last edited by
        #3

        Hi @J-Hilk ,

        Thanks for your response but m not getting how to draw a triangle?

        J.HilkJ KroMignonK 2 Replies Last reply
        0
        • J JasmineSethi

          Hi @J-Hilk ,

          Thanks for your response but m not getting how to draw a triangle?

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by J.Hilk
          #4

          @JasmineSethi
          you define a Canvas element, let it fill the parent

          and inside the onPaint signal you draw your rectangle using HTML5 syntax

          Rectangle{
            width: 400
            height:400
            color:"black"
          Canvas{
           anchors.fill:parent
          
          onPaint:{
           var context = getContext("2d");
           
          // the triangle
          context.beginPath();
          context.moveTo(100, 100);
          context.lineTo(100, 300);
          context.lineTo(300, 300);
          context.closePath();   
          
          // the fill color
          context.fillStyle = "#FFCC00";
          context.fill();
          }
          }
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          J 1 Reply Last reply
          5
          • J JasmineSethi

            Hi @J-Hilk ,

            Thanks for your response but m not getting how to draw a triangle?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @JasmineSethi said in How to design a triangle in qml?:

            Thanks for your response but m not getting how to draw a triangle?

            A good tutorial (in my eyes) for Canvas with QML can be found here: https://qmlbook.github.io/ch08-canvas/canvas.html

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            4
            • J.HilkJ J.Hilk

              @JasmineSethi
              you define a Canvas element, let it fill the parent

              and inside the onPaint signal you draw your rectangle using HTML5 syntax

              Rectangle{
                width: 400
                height:400
                color:"black"
              Canvas{
               anchors.fill:parent
              
              onPaint:{
               var context = getContext("2d");
               
              // the triangle
              context.beginPath();
              context.moveTo(100, 100);
              context.lineTo(100, 300);
              context.lineTo(300, 300);
              context.closePath();   
              
              // the fill color
              context.fillStyle = "#FFCC00";
              context.fill();
              }
              }
              
              J Offline
              J Offline
              JasmineSethi
              wrote on last edited by
              #6

              @J.Hilk

              Can we rotate it to the position as shown in image?

              J.HilkJ 1 Reply Last reply
              0
              • J JasmineSethi

                @J.Hilk

                Can we rotate it to the position as shown in image?

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #7

                @JasmineSethi said in How to design a triangle in qml?:

                @J.Hilk

                Can we rotate it to the position as shown in image?

                you have to modify the x and y values to fit your needs

                these

                context.moveTo(100, 100);
                context.lineTo(100, 300);
                context.lineTo(300, 300);
                

                are just a basic example


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                1 Reply Last reply
                0
                • JaredTaoJ Offline
                  JaredTaoJ Offline
                  JaredTao
                  wrote on last edited by
                  #8

                  canvas is slow, recommend use QtQuick.Shape

                  
                    Shape {
                        width: 200
                        height: 150
                        anchors.centerIn: parent
                        ShapePath {
                            strokeWidth: 4
                            strokeColor: "red"
                            fillGradient: LinearGradient {
                                x1: 20; y1: 20
                                x2: 180; y2: 130
                                GradientStop { position: 0; color: "blue" }
                                GradientStop { position: 0.2; color: "green" }
                                GradientStop { position: 0.4; color: "red" }
                                GradientStop { position: 0.6; color: "yellow" }
                                GradientStop { position: 1; color: "cyan" }
                            }
                            strokeStyle: ShapePath.DashLine
                            dashPattern: [ 1, 4 ]
                            startX: 20; startY: 20
                            PathLine { x: 180; y: 130 }
                            PathLine { x: 20; y: 130 }
                            PathLine { x: 20; y: 20 }
                        }
                    }
                  
                  

                  0_1560913625710_Triangle.png

                  cycling lover, anime lover

                  1 Reply Last reply
                  3

                  • Login

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