Quote Originally Posted by ashukla View Post
Qt Code:
  1. #include <QtGui>
  2. int main (int argc, char **argv)
  3. {
  4.  
  5. QApplication app(argc,argv);
  6. QLabel *lab=new QLabel(0,Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
  7. QPixmap *p=new QPixmap("plane.jpg");
  8. QPixmap p1(p->scaled ( lab->width(),lab->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
  9. lab->setPixmap(p1);
  10. lab->show();
  11. lab->adjustSize();
  12. return app.exec();
  13.  
  14. }
To copy to clipboard, switch view to plain text mode 
I hope it helps also.
Sorry, I can't resist commenting on this because there are roughly as many programming errors as there are lines:
  • "QLabel* lab" is never deleted. Why not just allocate it on the stack anyway so it goes out of scope properly after app.exec() returns?
  • Why two different instances of QPixmap?
  • Why allocate one of pixmaps on the heap? First of all it's never deleted and secondly QPixmap is an implicitly shared class.
  • Querying widget size ("lab->width(),lab->height()") returns bogus values until the widget has been shown or resized for the first time.
  • QLabel is able to scale its contents on its own, see QLabel::scaledContents.
  • When a widget is shown, it's initial size is calculated based on its sizeHint(), thus calling adjustSize() is futile.

It doesn't really matter if one deletes anything in a small sample application like this. The OS will free allocated resources anyway once the application quits. But I'm just worried how one manages to write robust real life applications without constant memory leaks if he gets to the habit of programming like this. It's a common misbelief that Qt deletes everything you don't. There is no magic garbage collection. Every QObject deletes its children but nothing more. QPixmap is not a QObject and QLabel has no parent in the code above so neither of them gets deleted.