Results 1 to 16 of 16

Thread: drawing on a QPixmap or QImage

  1. #1
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    4
    Qt products
    Qt3 Qt4

    Default drawing on a QPixmap or QImage

    Can you draw on a QPixmap/ QImage?

    I tried the following but it doesn't work:

    Qt Code:
    1. QPixmap pixmap;
    2. if ( pixmap.load("myImage.png") ) // load and draw image
    3. p->drawPixmap( 100, 85, pixmap );
    4. QPainter pixPaint(pixmap); // Error: Can't convert from QPixmap to const QPaintDevice . But doesn't QPixmap inherit QPaintDevice???
    5. pixPaint.drawLine(1,5);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: drawing on a QPixmap or QImage

    Pass a pointer to the pixmap.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: drawing on a QPixmap or QImage

    i.e.
    Qt Code:
    1. ...
    2. QPainter pixPaint(&pixmap);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Feb 2009
    Posts
    45
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: drawing on a QPixmap or QImage

    Try adding a brush like
    Qt Code:
    1. QPainter pixPaint(&pixmap);
    2. QBrush brush2( Qt::blue);
    3. pixPaint.setBrush(brush2);
    4. pixPaint.drawLine(1,5);
    To copy to clipboard, switch view to plain text mode 

    Hope that helps!
    Last edited by qtUser500; 26th February 2009 at 15:50.

  5. #5
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    4
    Qt products
    Qt3 Qt4

    Default Re: drawing on a QPixmap or QImage

    Quote Originally Posted by spirit View Post
    i.e.
    Qt Code:
    1. ...
    2. QPainter pixPaint(&pixmap);
    3. ...
    To copy to clipboard, switch view to plain text mode 
    Thanks for all your inputs.

    I tried added a brush/ pen to QPainter, but I still don't see the line.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: drawing on a QPixmap or QImage

    What is the current code?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    4
    Qt products
    Qt3 Qt4

    Default Re: drawing on a QPixmap or QImage

    I tried the code

    Qt Code:
    1. QPainter pixPaint(&pixmap);
    2. QPen myPen( Qt::green);
    3. pixPaint.setPen(myPen);
    4. pixPaint.drawLine(5,5, 10,10);
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: drawing on a QPixmap or QImage

    This seems to work quite fine for me:
    Qt Code:
    1. #include <QApplication>
    2. #include <QPixmap>
    3. #include <QPainter>
    4. #include <QLabel>
    5.  
    6. int main(int argc, char **argv){
    7. QApplication app(argc, argv);
    8. QPixmap px;
    9. px.load(argv[1]);
    10. QPainter p(&px);
    11. p.setPen(Qt::blue);
    12. p.drawLine(5,5, 40, 40);
    13. p.end();
    14. QLabel label;
    15. label.setPixmap(px);
    16. label.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    4
    Qt products
    Qt3 Qt4

    Default Re: drawing on a QPixmap or QImage

    I am not able to compile, get an error C2512: 'QLabel' : no appropriate default constructor available. Tried using
    Qt Code:
    1. QLabel *label = new QLabel(QWidget*); // but this needs a QWidget
    To copy to clipboard, switch view to plain text mode 

    1. Does a pixmap always need a QLabel, for it to be set to?
    2. I tried on a different widget like canvas and listbox, here I see the pixmap, but not the line drawn over the pixmap.
    Last edited by gren15; 27th February 2009 at 17:57.

  10. #10
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawing on a QPixmap or QImage

    I am not able to compile, get an error C2512: 'QLabel' : no appropriate default constructor available.
    Did you compile the code wysota gave ?? if changed, what did you write ?
    1. Does a pixmap always need a QLabel to be set to?
    Nope, pixmap is a paintdevice, and can be rendered onto any widget. using QLabel for showing pixmap is the simplest way since you dont need to DRAW the pixmap onto the painter, but simply set a pixmap for the label.
    2. I tried on a canvas, where I can get to see the pixmap, but not the line drawn over the pixmap.
    Can we see what you tried ?

  11. The following user says thank you to aamer4yu for this useful post:

    gren15 (3rd March 2009)

  12. #11
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    4
    Qt products
    Qt3 Qt4

    Default Re: drawing on a QPixmap or QImage

    The eventual direction I want to take is to create a listbox with pictures of animals. I want to select a few pictures (Eg grassland animals) and surround them with a green rectangle.

    This is what I tried:

    Qt Code:
    1. QPixmap pxWolf ("wolf.jpg");
    2. QPainter *animalPainter = new QPainter(&pxWolf );
    3.  
    4. QListBoxPixmap *animalItem = new QListBoxPixmap(animalListBox, pxWolf, "wolf"); // displays fine, I see the pixmap and the text
    5.  
    6. // I want to border this animal with a green line
    7. QPainter *borderPainter = new QPainter(animalItem);
    8. borderPainter->setPen(Qt::green);
    9. borderPainter->begin(&pxWolf);
    10. borderPainter->drawRect(QRect(1,1, 10,10)); // I don't get to see this green rectangle
    11. borderPainter->end();
    To copy to clipboard, switch view to plain text mode 

    I will be glad for any help on this. Thanks

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: drawing on a QPixmap or QImage

    Is the question related to Qt3?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    4
    Qt products
    Qt3 Qt4

    Default Re: drawing on a QPixmap or QImage

    Opps! Forgot to mention it .. yes it is using Qt3.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,013 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: drawing on a QPixmap or QImage

    Can you prepare a minimal compilable example reproducing the problem?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. #15
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    4
    Qt products
    Qt3 Qt4

    Default Re: drawing on a QPixmap or QImage

    Tweaking the QT listbox example, I have enclosed an executable version of what I want to do.

    Quick question: To resize the pixmap to a different size from the default size, I tried the resize function but it cuts the image, not resize it. I have also added this in the enclosed file.

    Qt Code:
    1. pixmap.resize(50,80);
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files
    Last edited by gren15; 2nd March 2009 at 16:34.

  17. #16
    Join Date
    Feb 2009
    Posts
    22
    Thanks
    4
    Qt products
    Qt3 Qt4

    Default Re: drawing on a QPixmap or QImage

    Thanks everyone. I was able to resolve it.
    Last edited by gren15; 3rd March 2009 at 19:03.

Similar Threads

  1. Construct QPixmap or QImage from raw data.
    By The Storm in forum Qt Programming
    Replies: 5
    Last Post: 1st December 2008, 08:21
  2. Replies: 4
    Last Post: 28th August 2008, 13:13
  3. DIB to QImage or QPixmap
    By ^NyAw^ in forum Qt Programming
    Replies: 0
    Last Post: 8th August 2008, 20:18
  4. Diff between QImage, QPixmap, QPicture
    By nupul in forum Newbie
    Replies: 6
    Last Post: 30th April 2008, 02:02
  5. Drawing speed: QPixmap vs QPicture
    By maverick_pol in forum Qt Programming
    Replies: 1
    Last Post: 2nd January 2008, 19:17

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.