PDA

View Full Version : Image without window frame?



niky
1st February 2008, 20:53
Hello!

Is it possible to just view an image without frame (minimize, maximize, close buttons) somewhere on the screen (should be always in the foreground) ?

Thank you very much!

marcel
1st February 2008, 21:06
Yes, render the image on a QWidget or set it on a QLabel and use:


imageContainer->setWindowFlags(imageContainer->windowFlags() | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);


However, you won't be able to interact with the widget. You will have to add a dditional functionality to be able to move it with the mouse, etc.

ashukla
2nd February 2008, 05:44
#include <QtGui>
int main (int argc, char **argv)
{

QApplication app(argc,argv);
QLabel *lab=new QLabel(0,Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
QPixmap *p=new QPixmap("plane.jpg");
QPixmap p1(p->scaled ( lab->width(),lab->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
lab->setPixmap(p1);
lab->show();
lab->adjustSize();
return app.exec();

}

I hope it helps also.

jpn
2nd February 2008, 06:15
#include <QtGui>
int main (int argc, char **argv)
{

QApplication app(argc,argv);
QLabel *lab=new QLabel(0,Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
QPixmap *p=new QPixmap("plane.jpg");
QPixmap p1(p->scaled ( lab->width(),lab->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation ));
lab->setPixmap(p1);
lab->show();
lab->adjustSize();
return app.exec();

}

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.

ashukla
2nd February 2008, 06:36
Dear JPN!
You are right and I am accustomed to do object allocation & deallocation in real projects.

ashukla
2nd February 2008, 06:49
#include <QApplication>
#include <QPixmap>
#include <QLabel>
int main (int argc, char **argv)
{

QApplication app(argc,argv);
QLabel lab(0,Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
QPixmap p("plane.jpg");


lab.setText("Hello");
lab.show();
lab.resize(300,200);
p=p.scaled ( lab.width(),lab.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
lab.setPixmap(p);
return app.exec();
}
Dear JPN!
Now, You should happy for above. Give any suggestion with free mind.

pherthyl
3rd February 2008, 09:37
#include <QApplication>
#include <QPixmap>
#include <QLabel>
int main (int argc, char **argv)
{

QApplication app(argc,argv);
QLabel lab(0,Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
QPixmap p("plane.jpg");


lab.setText("Hello");
lab.show();
lab.resize(300,200);
p=p.scaled ( lab.width(),lab.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
lab.setPixmap(p);
return app.exec();
}
Dear JPN!
Now, You should happy for above. Give any suggestion with free mind.

Oooh, can I nitpick too? :)
You need an #include <QApplication> for this to compile
You don't need to #include <QPixmap> as that gets pulled in by QLabel
Setting "hello" as the labels text does nothing since it will just show the image
Calling show() on the label doesn't actually show the label until control returns to the event loop, so theoretically width() and height() won't work properly (although it seems that they do anyway here)
You don't need to scale the image yourself.

Here's how I would do it.



#include <QLabel>
#include <QApplication>
int main (int argc, char **argv) {
QApplication app(argc,argv);
QLabel lab(0,Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
lab.resize(300,200);
lab.setPixmap(QPixmap("plane.png"));
lab.setScaledContents(true);
lab.show();
return app.exec();
}


Any more improvements possible?