Results 1 to 13 of 13

Thread: Change QPixmap image at runtime

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

    Default Change QPixmap image at runtime

    Hello all,
    I m using Qt 4.3.4


    My program has one QLabel in which I m dispalying an image using QPixmap.

    I want to load another image in QPixmap at runtime..

    How to do that???

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Change QPixmap image at runtime

    Quote Originally Posted by Qt Coder View Post
    I want to load another image in QPixmap at runtime...
    In the QPixmap or in the QLabel?
    Qt Code:
    1. mylabel->setPixmap(QPixmap("newFile"));
    2. // or
    3. mypixmap->load("newFile");
    4. mylabel->setPixmap(mypixmap);
    To copy to clipboard, switch view to plain text mode 
    ?

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

    Default Re: Change QPixmap image at runtime

    QPixmap PixEllipseNose;
    PixEllipseNose.load(QString::fromUtf8("images/Ellipse1.PNG"));
    ui.lblNoseEllipse->setPixmap(PixEllipseNose);

    Later some time in program (after timer goes off )I need to display another image

    I want to load another image in existing PixEllipseNose but I dont want to set another pixmap in Qlabel .

    How to do it???

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Change QPixmap image at runtime

    You must create the QPixmap on the heap to do so. "QPixmap PixEllipseNose;" is deleted after the scope. Use
    Qt Code:
    1. QPixmap *m_PixEllipseNose = new QPixmap(...);
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Change QPixmap image at runtime

    Hey but I declared "QPixmap PixEllipseNose;" as public variable in my class .

    I m loading image in it in Class constructor.

    Also the function in which I want to load another image in " PixEllipseNose" is a member function declared in "public" section.


    //Class constructor

    GLT_Submenu::GLT_Submenu(QWidget * parent):QDialog(parent)
    {
    ui.setupUi(this);
    PixEllipseNose.load(QString::fromUtf8("images/Ellipse1.PNG"));
    ui.lblNoseEllipse->setPixmap(PixEllipseNose);
    }


    void GLT_Submenu::f_FillNoseEllipse(int m_Stage)
    {
    QPen GreenPen(Qt::green, 1);
    QPainter p(&PixEllipseNose);
    p.setPen(GreenPen);
    p.drawLine(EllipseNoseX1,EllipseNoseY1,EllipseNose X2,EllipseNoseY2);
    ui.lblNoseEllipse->setPixmap(PixEllipseNose);
    if(m_Stage ==LOAD_YELLOW_PEN)
    {
    //add Code to display next image in PixEllipseNose
    }

    }

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Change QPixmap image at runtime

    Hey but I declared "QPixmap PixEllipseNose;" as public variable in my class .
    Yeah, forget about the heap thing.

    And where is now your problem. Sorry can't get the point. Just use load() or set it to an QPainter to use it again. Everywhere you want.

    EDIT: If you want to use it with a painter you may want to clear the pixmap first.

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

    Default Re: Change QPixmap image at runtime

    Hey I tried using

    PixEllipseNose.load(QString::fromUtf8("Images/YellowEllipse.PNG"));
    but the image is not changed ,still displayes the old image...

    Now what to do?

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Change QPixmap image at runtime

    You have to set it on your label once again, because QLabel::setPixmap() copies your pixmap. So PixEllipseNose isn't a pointer to QLabel::pixmap().

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

    Default Re: Change QPixmap image at runtime

    I m using code


    PixEllipseNose.load(QString::fromUtf8("Images/YellowEllipse.PNG"));
    ui.lblNoseEllipse->setPixmap(PixEllipseNose);

    But it doesnt change image.


    if I use


    ui.lblNoseEllipse->setPixmap(QPixmap(QString::fromUtf8("Images/YellowEllipse.PNG")));
    it displayes the next image.

    But I want to change contents of "PixEllipseNose" instead off making QLabel to point to another pixmap.....

  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: Change QPixmap image at runtime

    But I want to change contents of "PixEllipseNose" instead off making QLabel to point to another pixmap.....
    The contents of the pixmap will be changed,,, but how do you plan to see that the contents have been changed ? you need to paint it somewhere...dont u ?
    and using QLabel::setPixmap is one of the easiest way, without making your own classes, and doing the painting yourself.
    Last edited by aamer4yu; 28th March 2009 at 09:26.

  11. #11
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Change QPixmap image at runtime

    Qt Code:
    1. void QLabel::setPixmap(const QPixmap &pixmap)
    2. {
    3. Q_D(QLabel);
    4. if (!d->pixmap || d->pixmap->cacheKey() != pixmap.cacheKey()) {
    5. d->clearContents();
    6. d->pixmap = new QPixmap(pixmap); //<- it copies your Pixmap
    7. }
    8.  
    9. if (d->pixmap->depth() == 1 && !d->pixmap->mask())
    10. d->pixmap->setMask(*((QBitmap *)d->pixmap));
    11.  
    12. d->updateLabel();
    13. }
    To copy to clipboard, switch view to plain text mode 

    So your try to avoid increase memory are obsolete.

    EDIT: Why your first option is not working has to do with the cacheKey. But that I also don't understand right.

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

    Default Re: Change QPixmap image at runtime

    Hey the thing is

    I m painting this QPixmap image (Drawing some lines over the image using QPainter)

    Now after some period of time I need the original image in QPixmap and start my drawing again.

    I just want to change image of my Qpixmap at runtime .


    How to aceive this??

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

    Talking Re: Change QPixmap image at runtime

    Hey I got the solution to above problem

    QPixmap PixEllipseNoseGreen,PixEllipseNoseGreenBKP;
    PixEllipseNoseGreen.load(QString::fromUtf8("images/GreenEllipse.PNG"));
    PixEllipseNoseGreenBKP.load(QString::fromUtf8("ima ges/GreenEllipse.PNG"));

    After I finished painting QPixmap,I load the backup image as followes

    PixEllipseNoseGreen.operator=(PixEllipseNoseGreenB KP);

Similar Threads

  1. Replies: 4
    Last Post: 16th March 2009, 09:08
  2. add(draw) an icon(or image) to a QPixmap?
    By ascii in forum Qt Programming
    Replies: 4
    Last Post: 20th November 2008, 12:44
  3. Replies: 2
    Last Post: 29th September 2008, 00:08
  4. Replies: 5
    Last Post: 10th April 2008, 09:52
  5. Loading a custom image into a QPixmap
    By KShots in forum Qt Programming
    Replies: 12
    Last Post: 5th August 2006, 00:16

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.