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 do I draw a triangle?
QtWS25 Last Chance

How do I draw a triangle?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 1.0k 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.
  • M Offline
    M Offline
    Mikeeeeee
    wrote on 1 Aug 2020, 16:19 last edited by
    #1

    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

    M 1 Reply Last reply 1 Aug 2020, 16:57
    0
    • M Mikeeeeee
      1 Aug 2020, 16:19

      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

      M Offline
      M Offline
      Markkyboy
      wrote on 1 Aug 2020, 16:57 last edited by
      #2

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

      2/2

      1 Aug 2020, 16:57

      • Login

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