Results 1 to 12 of 12

Thread: Pixmap updating QLabel

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

    Default Pixmap updating QLabel

    I am new to Qt as of yesterday, and I was hoping someone would be able to help me with a problem. I am writing a program to act like XKCD, but I can't get pixmap to update label. Here is a section, I will attach the full files in case they would help.

    Qt Code:
    1. int val = 1;
    2. XKCD::XKCD(QWidget *parent)
    3. : QWidget(parent)
    4. {
    5. QString number;
    6. QString png = ".png";
    7. QString pics = "pics/";
    8. number.setNum(val);
    9. number.prepend(pics);
    10. number.append(png);
    11.  
    12. QPixmap *original=new QPixmap(QString(number));
    13. QPixmap image(original->scaled ( 1300,700, Qt::KeepAspectRatio, Qt::SmoothTransformation ));
    14. QLabel *label = new QLabel(this);
    15. label->setPixmap(image);
    16. int pixwidth = image.width();
    17. int labelwidth = (WIDTH - pixwidth) / 2;
    18. label->setGeometry(labelwidth, SIZEY, pixwidth, 700);
    19. }
    20.  
    21. void XKCD::OnPrev()
    22. {
    23. val--;
    24. if (val < 1) {val = 0;}
    25. QString number;
    26. QString png = ".png";
    27. QString pics = "pics/";
    28. number = number.setNum(val);
    29. number = number.prepend(pics);
    30. number = number.append(png);
    31. QPixmap *original=new QPixmap(QString(number));
    32. QPixmap image= (original->scaled ( 1300, 600, Qt::KeepAspectRatio, Qt::SmoothTransformation ));
    33. QLabel *label = new QLabel(this);
    34. label->setPixmap(image);
    35. int pixwidth = image.width();
    36. int labelwidth = (WIDTH - pixwidth) / 2;
    37. label->setGeometry(labelwidth, SIZEY, pixwidth, 700);
    38. /* Debugging purposes */ std::cout << "Val is now: "<< val << ". The image path is now: " << number.toStdString() << "." << std::endl;
    39. }
    To copy to clipboard, switch view to plain text mode 

    Hopefully someone can help me, I haven't found anything in documentation yet. Thanks for looking at my problem.

    Attached are my project files.
    xkcd.cpp
    xkcd.h
    main.cpp

    Edit: If it would be possible or easier to do using QImage, I am certainly open to that if someone could help me with it. My attempt to switch from QPixmap to QImage gave me a mile long set of errors that I couldn't find a workaround for.
    Last edited by Matt; 17th August 2010 at 18:50.

  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

    If
    Qt Code:
    1. QLabel *label;
    To copy to clipboard, switch view to plain text mode 
    in the header file is any indication, I'm fairly certain that you want to change
    Qt Code:
    1. QLabel *label = new QLabel(this);
    To copy to clipboard, switch view to plain text mode 
    to
    Qt Code:
    1. label = new QLabel(this);
    To copy to clipboard, switch view to plain text mode 
    in your constructor. If this is the case, then you should consider carefully why you are re-instantiating the label variable in onPrev().

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

    Default Re: Pixmap updating QLabel

    Thanks for the reply.
    A constructor is? Like I said, I'm new to Qt. I think it is the parent widget, but I'm not sure about that.
    As to re-instantiating the variable in Prev, I thought they only last for a single function. Maybe I'm mistaken here, but isn't the parent widget one function and Prev, next, and random are different functions? Or is C++ just messing me up here?

    Making the change doesn't seem to change any functionality though =/

    onPrev and onNext still won't change the image when they are clicked. Is there a buffering feature for label?
    Last edited by Matt; 17th August 2010 at 19:31.

  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

    When you declare a variable in a header file, that variable is considered to be an integral part of any object instantiated from your class. Thus, it persists until the object is destroyed. These so-called "instance variables" are generally initialized in the constructor, a method with the same name as your class. When you say "QLabel *label = new QLabel()", you are in fact calling the default constructor (i.e., no parameters) of the QLabel class.

    In C++ you can create a variable on the stack or on the heap. When you say "type *var = new type();", you are putting the variable on the heap, and even if you do this inside a method, the variable will persist. On the other hand, if you say "type var;" then you are putting the variable on the stack. If you do this inside a brace-delimited scope (such as a method), the variable will not persist or be visible outside that scope unless you take a pointer to it first. This conforms to the idea of a local variable.

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

    Matt (17th August 2010)

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

    Default Re: Pixmap updating QLabel

    I see, thanks. With a *, it lasts the whole program. Without, it is just a local variable. So i need to call the *label at some point, but can just call label afterwards.
    Do you have any idea why the image wouldn't update though? The debugging line I put in works properly, it is like label(pixmap) is buffered and won't output anything even when I tell it to.
    Perhaps making the other variables I am relying on heap variables would help, I'll try that.

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

    What do YOU get from running the following code?

    Qt Code:
    1. label = new QLabel(this);
    2. QPixmap pmap(QString("C:\\path\\to\\file.jpg")); // windows machine, hence escapes
    3. if (pmap.isNull())
    4. label->setText("Null pixmap!");
    5. else
    6. label->setPixmap(pmap);
    To copy to clipboard, switch view to plain text mode 

    At first, I got null pixmap, then I remembered the windows directory delimiters and I got my image in the label. Anyways, yeah, you might have a null pixmap for whatever reason.

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

  9. #8
    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.

  10. #9
    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?

  11. #10
    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.

  12. #11
    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?

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

    Matt (17th August 2010)

  14. #12
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.