Results 1 to 20 of 30

Thread: Draw line on image using QPainter

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Posts
    116
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Draw line on image using QPainter

    I m using Qt 4.6.2 on windows xp

    I need to draw horizontal ,vertical lines and an arc over the image .


    My code is
    Qt Code:
    1. #include <QtGui>
    2.  
    3. QPixmap pixmap;
    4. QPainter painter;
    5. QPen DarkGray((QColor::QColor(69,86,96)),1);
    6.  
    7. pixmap.load(QString::fromUtf8(":/BiFold/Images/Plane.PNG"));
    8. painter.begin(&pixmap);
    9. painter.setPen(DarkGray);
    10. painter.drawLine(250,300,500,300);
    11. painter.end();
    12. ui->lbl_plane->setGeometry(QRect(100,150,611,301));
    13. ui->lbl_plane->setPixmap(pixmap);
    To copy to clipboard, switch view to plain text mode 

    But only image is displayed ,no line is drawn....

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    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 only draw 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.

  3. #3
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw line on image using QPainter

    You just need to setup your pixmap before you assign the painter:

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7.  
    8. QLabel l;
    9.  
    10. QPixmap pixmap;
    11. pixmap.load(QString::fromUtf8("E:/2010-02-25_112406.png"));
    12.  
    13. QPainter painter(&pixmap);
    14. QPen Red((QColor::QColor(255,0,0)),1);
    15. painter.setPen(Red);
    16. painter.drawLine(50,50,250,250);
    17. l.setPixmap(pixmap);
    18. l.show();
    19.  
    20. return a.exec();
    21. }
    To copy to clipboard, switch view to plain text mode 

    Joh

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    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

    @Joh,
    did you try the code you posted?
    This code should not work.
    At the very least in runtime a Qt warning should be printed, saying that QPainter can not be used outside the 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.

  5. #5
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw line on image using QPainter

    Sure I tried it! That's why I posted it as a complete application.

    It doesn't show any warnings on the console, either. Why should it not be possible to use a QPainter on an 'offline' QPixMap?

    Joh

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    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 are right, QPixmap is not a widget.
    Sorry.
    ==========================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.

  7. #7
    Join Date
    Feb 2007
    Location
    Karlsruhe, Germany
    Posts
    469
    Thanks
    17
    Thanked 90 Times in 88 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw line on image using QPainter

    Aah! Allright! That was the source of the confusion!

    Yes, and not beeing able to draw onto a widget outside the paintEvent makes perfect sense.

    Hope we didn't confuse the OP :->

    Joh

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

    Default Re: Draw line on image using QPainter

    I m still not able to draw line on image

    My code is

    Qt Code:
    1. QPixmap pixmap;
    2.  
    3. QPen GreenPen((QColor::QColor (0,255,0,255)),1);
    4.  
    5. ui->lbl_plane->setGeometry(QRect(100,150,611,301));
    6. pixmap.load(QString::fromUtf8(":/BiFold/Images/Plane.PNG"));
    7. ui->lbl_plane->resize(pixmap.size());
    8.  
    9. QPainter painter(&pixmap);
    10. painter.setPen(GreenPen);
    11. painter.drawLine(250,300,500,300);
    12. ui->lbl_plane->setPixmap(pixmap);
    13. ui->lbl_plane->show();
    To copy to clipboard, switch view to plain text mode 

    The image format is "Format_RGB32"

    what I m missing?

  9. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    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

    give your pixmap a size.
    ==========================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.

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

    Default Re: Draw line on image using QPainter

    I have added line
    Qt Code:
    1. QPixmap pixmap(533,292);
    To copy to clipboard, switch view to plain text mode 
    which is my image size to above code but still no line is drawn on image....

  11. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    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

    try adding painter.end() after the drawLine().
    ==========================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.

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    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

    Try the following code.
    Its works I tested it.
    Once it works for you see what it is you are doing wrong:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. QLabel l;
    6. QPixmap pixmap(500,500);
    7. QPainter painter(&pixmap);
    8. QPen Red((QColor::QColor(255,0,0)),1);
    9. painter.setPen(Red);
    10. painter.drawLine(50,50,250,250);
    11. l.setPixmap(pixmap);
    12. l.show();
    13.  
    14. return a.exec();
    15. }
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

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.