Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How do I draw a triangle?



  • 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



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


Log in to reply