Page 1 of 2 12 LastLast
Results 1 to 20 of 30

Thread: Draw line on image using QPainter

  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.

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

    Default Re: Draw line on image using QPainter

    It works for me also ...

    But only if I dont add image to pixmap.

    as soon as I add image
    Qt Code:
    1. pixmap.load(QString::fromUtf8(":/BiFold/Images/Plane.PNG"));
    To copy to clipboard, switch view to plain text mode 
    it shows only image no red line..

  14. #14
    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

    show your full code.
    ==========================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.

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

    Default Re: Draw line on image using QPainter

    Doorcontrol1 is a QDialog in which I need to show that image and lines.


    main.cpp
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QtCore>
    3. #include "bifold.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8.  
    9. BiFold w;
    10. w.show();
    11. return a.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    bifold.h

    Qt Code:
    1. #ifndef BIFOLD_H
    2. #define BIFOLD_H
    3. #include <QMainWindow>
    4.  
    5. namespace Ui {
    6. class BiFold;
    7. }
    8.  
    9. class DoorControl1;
    10.  
    11. class BiFold : public QMainWindow {
    12. Q_OBJECT
    13. public:
    14. BiFold(QWidget *parent = 0);
    15. ~BiFold();
    16.  
    17. DoorControl1 *ODoorControl1;
    18.  
    19. protected:
    20. void changeEvent(QEvent *e);
    21.  
    22. private:
    23. Ui::BiFold *ui;
    24. };
    25.  
    26. #endif // BIFOLD_H
    To copy to clipboard, switch view to plain text mode 

    bifold.cpp

    Qt Code:
    1. #include "bifold.h"
    2. #include "ui_bifold.h"
    3. #include "doorcontrol1.h"
    4. #include <QMessageBox>
    5.  
    6. BiFold::BiFold(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::BiFold)
    9. {
    10. ui->setupUi(this);
    11. removeToolBar(ui->mainToolBar); //Removes tool bar from main window
    12.  
    13. ODoorControl1 = new DoorControl1(this);
    14. ODoorControl1->setWindowFlags( Qt::FramelessWindowHint );
    15.  
    16.  
    17. }
    18.  
    19. BiFold::~BiFold()
    20. {
    21. delete ui;
    22. }
    23.  
    24. void BiFold::changeEvent(QEvent *e)
    25. {
    26. QMainWindow::changeEvent(e);
    27. switch (e->type()) {
    28. case QEvent::LanguageChange:
    29. ui->retranslateUi(this);
    30. break;
    31. default:
    32. break;
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    doorcontrol1.h
    Qt Code:
    1. #ifndef DOORCONTROL1_H
    2. #define DOORCONTROL1_H
    3.  
    4. #include <QDialog>
    5.  
    6. namespace Ui {
    7. class DoorControl1;
    8. }
    9.  
    10. class DoorControl1 : public QDialog {
    11. Q_OBJECT
    12. public:
    13. DoorControl1(QWidget *parent = 0);
    14. ~DoorControl1();
    15. void DrawStuff();
    16.  
    17.  
    18. protected:
    19. void changeEvent(QEvent *e);
    20. void paintEvent(QPaintEvent *event);
    21.  
    22. private:
    23. Ui::DoorControl1 *ui;
    24. };
    25.  
    26. #endif // DOORCONTROL1_H
    To copy to clipboard, switch view to plain text mode 

    doorcontrol1.cpp
    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include <QMessageBox>
    4. #include "doorcontrol1.h"
    5. #include "ui_doorcontrol1.h"
    6.  
    7. DoorControl1::DoorControl1(QWidget *parent) :
    8. QDialog(parent),
    9. ui(new Ui::DoorControl1)
    10. {
    11. ui->setupUi(this);
    12. DrawStuff();
    13. }
    14.  
    15. DoorControl1::~DoorControl1()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void DoorControl1::changeEvent(QEvent *e)
    21. {
    22. QDialog::changeEvent(e);
    23. switch (e->type()) {
    24. case QEvent::LanguageChange:
    25. ui->retranslateUi(this);
    26. break;
    27. default:
    28. break;
    29. }
    30. }
    31.  
    32. void DoorControl1::paintEvent(QPaintEvent *event )
    33. {
    34.  
    35. }
    36.  
    37. void DoorControl1::DrawStuff()
    38. {
    39.  
    40. ui->lbl_plane->setGeometry(QRect(100,150,611,301));
    41.  
    42. QPixmap pixmap(500,500);
    43. pixmap.load(QString::fromUtf8(":/BiFold/Images/Plane.PNG"));
    44.  
    45. QPainter painter(&pixmap);
    46. ui->lbl_plane->resize(pixmap.size());
    47.  
    48. QPen Red((QColor::QColor(255,0,0)),1);
    49. painter.setPen(Red);
    50. painter.drawLine(250,300,500,300);
    51. painter.end();
    52. ui->lbl_plane->setPixmap(pixmap);
    53. ui->lbl_plane->show();
    54.  
    55.  
    56.  
    57. }
    To copy to clipboard, switch view to plain text mode 

  16. #16
    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

    Can't you put your code into something simpler? Just one class, and without an UI-File, that you didn't provide!

    Reducing complexity often helps to find problems in the first place, so you might even solve it yourself!

    Johannes

    PS: "Cross posting" meant, that you posted, while I was writing my message. After I posted mine.. I saw your new message and changed mine .. to "cross posting". After I read your message, I edited again :->
    Last edited by JohannesMunk; 24th June 2010 at 11:10.

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

    Default Re: Draw line on image using QPainter

    what is mean by "Cross posting :-> " ?????

  18. #18
    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 reimplemented the paintEvent() in your dialog, and left it blank.
    So your dialog does not do any painting,I am not sure if this will cause its children not to draw as well.
    Delete the paintEvent() re implementation and see if this helps.
    ==========================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.

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

    Default Re: Draw line on image using QPainter

    I have removed reimplementation of paintEvent () from header as well as source file

    but still no line is drawn on image .....

  20. #20
    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

    in BiFold construcotr add:
    ODoorControl1->show();
    ==========================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.