Results 1 to 12 of 12

Thread: Pixmap updating QLabel

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Pixmap updating QLabel

    When I run that, replacing the image obviously, it displays the image I want it to.

    If I had a null pixmap in one of the lower functions (onPrev, etc), would it display a null or just continue to show the same thing. If it continues to show the same thing, that is my problem.

    Edit: A little testing with and pixmap.isNull() has determined that my image isn't null in any of my functions. It just refuses to update to the new filepath.
    Last edited by Matt; 17th August 2010 at 20:39.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Pixmap updating QLabel

    Quote Originally Posted by Matt View Post
    If I had a null pixmap in one of the lower functions (onPrev, etc), would it display a null or just continue to show the same thing.
    It would display absolutely nothing at all, or in the case of my example code, "Null Pixmap!" (tested). A null pixmap has no content, but QLabel::setPixMap() wipes out the label's previous pixmap.

  3. #3
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Pixmap updating QLabel

    Qt Code:
    1. QString number;
    2. QString png = ".png";
    3. QString pics = "pics/";
    4. number = number.setNum(val);
    5. number = number.prepend(pics);
    6. number = number.append(png);
    7. QPixmap *original=new QPixmap(QString(number));
    8. QPixmap image= (original->scaled ( 1300, 600, Qt::KeepAspectRatio, Qt::SmoothTransformation ));
    9. label = new QLabel(this);
    10. label->setPixmap(image);
    To copy to clipboard, switch view to plain text mode 
    So in this code, label should be cleared and replaced with the new value of image, which is clearly not happening. Could the problem be in QPixmap *original? Removing the * from that variable makes it refuse to compile, but could the * be preventing the value of original from updating? If so, is there a way to get rid of the * safely without giving errors about converting QPixmap* to QPixmap?

  4. #4
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Pixmap updating QLabel

    How about I just show you what worked for me?

    First, here is the header file:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QWidget>
    5.  
    6. class QLabel;
    7.  
    8. class MainWindow : public QWidget
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MainWindow(QWidget *parent = 0);
    14. ~MainWindow();
    15.  
    16. public slots:
    17. void updatePixmap();
    18.  
    19. private:
    20. QPixmap pmap1, pmap2;
    21. QLabel *label;
    22. QPushButton *button;
    23. int numUpdates;
    24. };
    25.  
    26. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Now, here is the .cpp file:

    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QLabel>
    3. #include <QPushButton>
    4. #include <QVBoxLayout>
    5.  
    6. MainWindow::MainWindow(QWidget *parent)
    7. : QWidget(parent), numUpdates(0)
    8. {
    9. pmap1 = QPixmap("C:\\path\\to\\image1.jpg");
    10. pmap2 = QPixmap("C:\\path\\to\\image2.jpg");
    11. label = new QLabel();
    12. label->setPixmap(pmap1);
    13.  
    14. button = new QPushButton("Update!");
    15. connect(button, SIGNAL(clicked()), this, SLOT(updatePixmap()));
    16.  
    17. QVBoxLayout *layout = new QVBoxLayout(this);
    18. layout->addWidget(label);
    19. layout->addWidget(button);
    20. layout->setSizeConstraint(QLayout::SetFixedSize);
    21. setLayout(layout);
    22. }
    23.  
    24. void MainWindow::updatePixmap()
    25. {
    26. ++numUpdates % 2 ? label->setPixmap(pmap2) : label->setPixmap(pmap1);
    27. }
    28.  
    29. MainWindow::~MainWindow()
    30. {
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 

    update: images now toggle back and forth instead of being a one-shot deal
    Last edited by Urthas; 17th August 2010 at 23:03.

  5. #5
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Pixmap updating QLabel

    label should be cleared and replaced with the new value of image, which is clearly not happening.

    But but but you are destroying the previous label entirely...

    What happens if you comment out line 9?

  6. The following user says thank you to Urthas for this useful post:

    Matt (17th August 2010)

  7. #6
    Join Date
    Aug 2010
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Pixmap updating QLabel

    If I comment out line 9, I get an error. But that line is only for resizing the window, it doesn't affect anything else.

    I am reading over your first post trying to integrate it into my system.

    Edit: Thank you very much, that did it. When I removed the code that destroyed label, it solved the problem. I will definitely remember this in the future.
    Last edited by Matt; 17th August 2010 at 21:15.

Similar Threads

  1. DB changes not updating in views
    By Jeffb in forum Newbie
    Replies: 11
    Last Post: 11th May 2010, 10:00
  2. Replies: 1
    Last Post: 29th September 2009, 19:44
  3. Updating from Qt-4.4.3 to Qt-4.5.0 --Help Needed
    By swamyonline in forum Installation and Deployment
    Replies: 1
    Last Post: 9th February 2009, 11:37
  4. Replies: 1
    Last Post: 2nd August 2008, 15:46
  5. empty pixmap as a QLabel
    By tommy in forum Qt Programming
    Replies: 16
    Last Post: 11th December 2007, 21:15

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.