Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Solved How do I draw a triangle?

    QML and Qt Quick
    2
    2
    578
    Loading More Posts
    • 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
      Mikeeeeee last edited by

      Hi!
      How do I draw a triangle?
      It is not work:

      Canvas{
                              contextType: "2d"
                              anchors.fill: parent
                              onPaint: {
                                  context.reset();
                                  context.moveTo(0, 0);
                                  context.lineTo(width, 0);
                                  context.lineTo(width / 2, height);
                                  context.closePath();
                                  context.fillStyle = control.pressed ? "green" : "red";
                                  context.fill();
                              }
                          }
      

      And https://doc.qt.io/qtdesignstudio/qml-qtquick-studio-components-triangle.html
      QML not found QtQuick.Studio.Components 1.0

      Markkyboy 1 Reply Last reply Reply Quote 0
      • Markkyboy
        Markkyboy @Mikeeeeee last edited by

        @Mikeeeeee - Nest Canvas in Window

        import QtQuick 2.12
        import QtQuick.Window 2.12
        
        Window {
            visible: true
            width: 320
            height: 240
            title: qsTr("Triangle")
        
            Canvas {
                id: yellowTriangle
                width: 110; height: 110
                anchors.centerIn: parent
                onPaint: {
                    var ctx = getContext("2d")
        
                    // the equliteral triangle
                    ctx.beginPath();
                    ctx.moveTo(55, 10);
                    ctx.lineTo(10, 90);
                    ctx.lineTo(100, 90);
                    ctx.closePath();
        
                    // outline
                    ctx.lineWidth = 10;
        
                    // outline color
                    ctx.strokeStyle = '#000000';
                    ctx.stroke();
        
                    // fill color
                    ctx.fillStyle = "#FFCC00";
                    ctx.fill();
                }
            }
        }
        

        yellow-triangle.JPG

        Don't just sit there standing around, pick up a shovel and sweep up!

        I live by the sea, not in it.

        1 Reply Last reply Reply Quote 1
        • First post
          Last post