Results 1 to 2 of 2

Thread: Free Hand Paint on Canvas

  1. #1
    Join Date
    Jan 2015
    Posts
    35
    Thanks
    20
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Free Hand Paint on Canvas

    Hi,

    I develop a project that can draw as freehand style on canvas.
    program is successfully run but it does not paint any stroke on canvas.


    main.cpp
    Qt Code:
    1. #include <QGuiApplication>
    2. #include <QtQuick/qquickview.h>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char ** argv)
    6. {
    7. QGuiApplication app(argc, argv);
    8.  
    9. QQuickView view;
    10. view.setSource(QUrl("main.qml"));
    11. view.setWidth(768);
    12. view.setHeight(263);
    13. view.show();
    14.  
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 


    main.qml
    Qt Code:
    1. import QtQuick 2.4
    2.  
    3. Item {
    4. id:root
    5. width:768
    6. height:263
    7. anchors.margins: 4
    8.  
    9. Writing {
    10. id:canvas
    11. width: 768;
    12. height:263;
    13. x: 0
    14. y: 0
    15. }
    16.  
    17. Text {
    18. id: text;
    19. width: 140;
    20. font.family: "Arial"
    21. font.pointSize: 20
    22. wrapMode: Text.Wrap;
    23. anchors.top: parent.top;
    24. anchors.right: parent.right;
    25. anchors.rightMargin: 20;
    26. anchors.topMargin: -10;
    27. text: ""
    28. }
    29.  
    30. Item {
    31. id:clearbutton
    32. width:100
    33. height:65
    34. anchors.left: parent.left
    35. anchors.top: parent.top
    36. anchors.leftMargin: 100
    37.  
    38. MouseArea {
    39. anchors.fill : parent
    40. onClicked: canvas.clear();
    41. }
    42. }
    43. }
    To copy to clipboard, switch view to plain text mode 


    Writing.qml
    Qt Code:
    1. import QtQuick 2.4
    2.  
    3.  
    4. Canvas {
    5. id:canvas
    6. property int paintX
    7. property int paintY
    8. property int count: 0
    9. property int lineWidth: 5
    10. property color drawColor: "black"
    11. property int strokes: 0
    12.  
    13. MouseArea {
    14. id:mousearea
    15. hoverEnabled:true
    16. anchors.fill: parent
    17. onClicked: drawPoint();
    18.  
    19. onPositionChanged: {
    20. if (mousearea.pressed) {
    21. drawLineSegment();
    22. }
    23. paintX = mouseX;
    24. paintY = mouseY;
    25. }
    26.  
    27. onReleased: {
    28. var ctx = canvas.getContext('2d');
    29. ctx.beginPath();
    30. ctx.strokeStyle = 'red';
    31. ctx.lineWidth = lineWidth
    32. ctx.moveTo(paintX, paintY);
    33. ctx.lineTo(mousearea.mouseX, mousearea.mouseY);
    34. console.log("line X:"+ mousearea.mouseX +"Y: " + mousearea.mouseY );
    35. ctx.stroke();
    36. ctx.closePath();
    37. strokes++;
    38. }
    39.  
    40. }
    41.  
    42. function drawLineSegment() {
    43. var ctx = canvas.getContext('2d');
    44. ctx.beginPath();
    45. ctx.strokeStyle = "blue"
    46. ctx.lineWidth = lineWidth
    47. ctx.moveTo(paintX, paintY);
    48. ctx.lineTo(mousearea.mouseX, mousearea.mouseY);
    49. console.log("line X:"+ mousearea.mouseX +"Y: " + mousearea.mouseY );
    50. ctx.stroke();
    51. ctx.closePath();
    52. }
    53.  
    54. function drawPoint() {
    55. var ctx = canvas.getContext('2d');
    56. ctx.lineWidth = lineWidth
    57. ctx.fillStyle = drawColor
    58. ctx.fillRect(mousearea.mouseX, mousearea.mouseY, 2, 2);
    59. console.log("line X:"+ mousearea.mouseX +"Y: " + mousearea.mouseY );
    60. }
    61.  
    62. function clear() {
    63. var ctx = canvas.getContext('2d');
    64. strokes=0;
    65. text.text = "";
    66. ctx.clearRect(0, 0, width, height);
    67. }
    68.  
    69.  
    70. }
    To copy to clipboard, switch view to plain text mode 

    How to fix it?

    Thanks.
    Last edited by anda_skoa; 23rd March 2015 at 08:49. Reason: changed [quote] to [code]

  2. #2
    Join Date
    Dec 2015
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Free Hand Paint on Canvas

    Replace following code snippet in your Writing.qml. This should get you going. The key is to implement onPaint() handler
    Qt Code:
    1. import QtQuick 2.4
    2.  
    3.  
    4. Canvas {
    5. id:canvas
    6. property int paintX
    7. property int paintY
    8. property int count: 0
    9. property int lineWidth: 5
    10. property color drawColor: "black"
    11. property int strokes: 0
    12.  
    13. onPaint: drawPoint();
    14.  
    15. MouseArea {
    16. id:mousearea
    17. hoverEnabled:true
    18. anchors.fill: parent
    19. onClicked: {
    20. paintX = mouseX;
    21. paintY = mouseY;
    22. requestPaint();
    23. }
    24.  
    25. onPositionChanged: {
    26. if (mousearea.pressed) {
    27. drawLineSegment();
    28. }
    29. paintX = mouseX;
    30. paintY = mouseY;
    31. }
    32.  
    33. onReleased: {
    34. var ctx = canvas.getContext('2d');
    35. ctx.beginPath();
    36. ctx.strokeStyle = 'red';
    37. ctx.lineWidth = lineWidth
    38. ctx.moveTo(paintX, paintY);
    39. ctx.lineTo(mousearea.mouseX, mousearea.mouseY);
    40. console.log("line X:"+ mousearea.mouseX +"Y: " + mousearea.mouseY );
    41. ctx.stroke();
    42. ctx.closePath();
    43. strokes++;
    44. }
    45.  
    46. }
    47.  
    48. function drawLineSegment() {
    49. var ctx = canvas.getContext('2d');
    50. ctx.beginPath();
    51. ctx.strokeStyle = "blue"
    52. ctx.lineWidth = lineWidth
    53. ctx.moveTo(paintX, paintY);
    54. ctx.lineTo(mousearea.mouseX, mousearea.mouseY);
    55. console.log("line X:"+ mousearea.mouseX +"Y: " + mousearea.mouseY );
    56. ctx.stroke();
    57. ctx.closePath();
    58. }
    59.  
    60. function drawPoint() {
    61. var ctx = canvas.getContext('2d');
    62. ctx.lineWidth = lineWidth
    63. ctx.fillStyle = drawColor
    64. ctx.fillRect(paintX, paintY, 2, 2);
    65. console.log("line X:"+ paintX +"Y: " + paintY );
    66. }
    67.  
    68. function clear() {
    69. var ctx = canvas.getContext('2d');
    70. strokes=0;
    71. text.text = "";
    72. ctx.clearRect(0, 0, width, height);
    73. }
    74. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How Hand the close button in MainWindow
    By phuong_90 in forum Qt Programming
    Replies: 4
    Last Post: 25th October 2011, 10:25
  2. Hand Drawing in Qt
    By Nasrudin in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2010, 08:19
  3. What to free or not to free, that is the question
    By bruccutler in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2007, 05:04
  4. QT Designer? Coding by hand?
    By lewis in forum Qt Tools
    Replies: 47
    Last Post: 2nd April 2007, 16:30
  5. Qt Designer or writing by hand
    By luf in forum Qt Tools
    Replies: 3
    Last Post: 12th March 2007, 12:31

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.