Page 2 of 2 FirstFirst 12
Results 21 to 30 of 30

Thread: Draw line on image using QPainter

  1. #21
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Draw line on image using QPainter

    It's already showing DoorControl1 Dialog with Plane's image...

    still as you said I added ODoorControl1->show(); in Bifold constructor ,but no line still drawn..

  2. #22
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Draw line on image using QPainter

    you have to simplify and go step at a time.
    Put your dialog in main, and show it.
    See if you get the line drawn then.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #23
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Draw line on image using QPainter

    I have added draw line code in main.cpp itself but still it only shows image and no line is drawn

    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2.  
    3. #include <QtCore>
    4. #include <QtGui>
    5. #include <QLabel>
    6. //#include "bifold.h"
    7.  
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication a(argc, argv);
    12.  
    13. // BiFold w;
    14. // w.show();
    15. QLabel l;
    16. QPixmap pixmap(500,500);
    17. pixmap.load(QString::fromUtf8(":/BiFold/Images/Plane.PNG"));
    18.  
    19. QPainter painter(&pixmap);
    20. l.resize(pixmap.size());
    21.  
    22.  
    23. QPen Red((QColor::QColor(255,0,0)),1);
    24. painter.setPen(Red);
    25. painter.drawLine(250,300,500,300);
    26. painter.end();
    27. l.setPixmap(pixmap);
    28. l.show();
    29. return a.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

  4. #24
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Draw line on image using QPainter

    Is this a fault of my image,but I tried changing the image still no line is drawn..

    I read in documentation of QPainter "Painting on a QImage with the format QImage::Format_Indexed8 is not supported."


    But my image format is QImage::Format_RGB32.


    I m not able to upload image here,it says "upload failed"


    pls show me right direction ,I cannot proceed my work if this issues is not solved.

  5. #25
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,546
    Thanks
    13
    Thanked 1,578 Times in 1,507 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Draw line on image using QPainter

    Are you sure that your resource file path is good?

    Your code with some minor tweaks and a local file:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3. #include <QDebug>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8.  
    9. QLabel l;
    10. QPixmap pixmap;
    11.  
    12. bool goodLoad = pixmap.load(QString::fromUtf8("./test.png"));
    13. qDebug() << goodLoad << pixmap.isNull() << pixmap.size() << pixmap.depth();
    14.  
    15. QPainter painter(&pixmap);
    16. QPen Red((QColor::QColor(255,0,0)),1);
    17. painter.setPen(Red);
    18. painter.drawLine(40, 40, 200, 280);
    19. painter.end();
    20.  
    21. pixmap.save("./test2.png");
    22.  
    23. l.setPixmap(pixmap);
    24. l.show();
    25.  
    26. return app.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 
    Input is:
    test.jpg
    Output is:
    Qt Code:
    1. true false QSize(240, 320) 24
    To copy to clipboard, switch view to plain text mode 
    and
    test2.jpg

    Edit: the forum converted the PNG files to JPG

  6. #26
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Draw line on image using QPainter

    Finally the line is visible over the image

    The co-ordinates I used for drawing line were main culprit here
    painter.drawLine(250,300,500,300);

    Qt Code:
    1. also
    2.  
    3. qDebug() << goodLoad << pixmap.isNull() << pixmap.size() << pixmap.depth();
    4.  
    5. shows
    6.  
    7. true false QSize(533, 292) 32
    To copy to clipboard, switch view to plain text mode 
    if I use ur co-ordinate,

    painter.drawLine(40, 40, 200, 280);

    line is drawn.....

  7. #27
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Draw line on image using QPainter

    My next query is

    Can we directly paint QDialog ?

    I need to draw some arcs, squares and lines on QDialog

  8. #28
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,709
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Draw line on image using QPainter

    You can paint on any widget, but only in a paintEvent().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #29
    Join Date
    Jul 2015
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw line on image using QPainter

    hai to every one am new to qt actually am trying to draw line on image (qpixmap),i had tried all coeds u have given above but still i didnt get the result plz can any one give full code to me thanking you and sorry for my bad english

  10. #30
    Join Date
    Aug 2015
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Draw line on image using QPainter

    How to drag that line over the image?
    I tried to drag. But it does not update.

Similar Threads

  1. Image reading and writing line by line
    By Astrologer in forum Qt Programming
    Replies: 7
    Last Post: 29th April 2010, 08:15
  2. Replies: 6
    Last Post: 21st September 2009, 10:55
  3. Draw a line
    By Daan in forum KDE Forum
    Replies: 1
    Last Post: 27th August 2009, 17:29
  4. Draw Line
    By aloysiusjegan in forum Qwt
    Replies: 4
    Last Post: 12th February 2009, 11:02
  5. Best way to draw a Line
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 19th January 2008, 09:57

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.