PDA

View Full Version : preview file dialog



mickey
18th April 2006, 23:16
I've code this below and works. When I selected (only one click) a fileName in myFileDialog, on the right, in the preview box appear if the file selected is a pixmap or not; I'd like, when I click, show in preview box, the preview of image (the image). How to do? Thanks


class Preview : public QLabel, public QFilePreview {
public:
Preview( QWidget *parent=0 ) : QLabel( parent ) {}

void previewUrl( const QUrl &u )
{
QString path = u.path();
QPixmap pix( path );
if ( pix.isNull() ) {
setText( "This is not a pixmap" );
} else {

setText( "This is a pixmap" );
}
}
};

Preview* p = new Preview;
myFileDialog* myFd = new myFileDialog(this,"",)
myFd->setContentsPreviewEnabled( TRUE );
myFd->setContentsPreview( p, p );
if ( myFd->exec() == QDialog::Accepted )
file = myFd->selectedFile();

jacek
18th April 2006, 23:24
if ( pix.isNull() ) {
setText( "This is not a pixmap" );
} else {
setPixmap( pix );
}

mickey
20th April 2006, 16:18
Thanks, it works; I'm trying to change it size; with this works, but happen this:


pix.resize(QSize(150,150));

Chicken Blood Machine
20th April 2006, 18:24
read the docs for QPixmap::resize(). It does not resize the image, jsut the size of the pixmap. You will need to create a QImage, scale it with QImage::scale() and create a pixmap from that.

jacek
20th April 2006, 18:54
Use this:
void QLabel::setScaledContents ( bool )
Sets whether the label will scale its contents to fill all available space. See the "scaledContents" property for details.

Chicken Blood Machine
20th April 2006, 19:08
Use this:

Yes of of course. This is the simpler solution!

mickey
20th April 2006, 20:22
thanks, it works; but is it possible repeat more times the same QImage onto a pixmap?

mickey
20th April 2006, 23:00
I don't understand how you say me use QLabel for my aim; beyond this, I used pixmap; and I think using qlabel (?) my problem will persists: my image appears distorted (texture 32x32). Am I wrong? (I'd like show it more large, for this I thinked to repeat an QImage on a QPixmap) Thanks

Chicken Blood Machine
21st April 2006, 01:26
I don't understand how you say me use QLabel for my aim; beyond this, I used pixmap; and I think using qlabel (?) my problem will persists: my image appears distorted (texture 32x32). Am I wrong? (I'd like show it more large, for this I thinked to repeat an QImage on a QPixmap) Thanks

Ah, the aspect ratio of your image is being distorted. In this case, you should create a QImage and scale it up to the correct size, while retaining the aspect ratio: Something like:



void previewUrl(const QUrl& rUrl)
{
QString path = rUrl.path();
QImage im(path);
if (im.isNull())
{
setText( "This is not a pixmap" );
}
else
{
im = im.smoothScale(width(), height(), QImage::ScaleMin);
setPixmap(QPixmap(im));
}
}

If you just want to tile the image, use this instead:



void previewUrl(const QUrl& rUrl)
{
QString path = rUrl.path();
QPixmap p(path);
if (p.isNull())
{
setText( "This is not a pixmap" );
}
else
{
setPaletteBackgroundPixmap(p);
}
}

mickey
21st April 2006, 22:46
with pallette seems better....but happen this. and when I select a non-pixmap, the preview widget don't change...

jacek
21st April 2006, 23:30
with pallette seems better....but happen this. and when I select a non-pixmap, the preview widget don't change...
Because you must restore the original palette.

mickey
22nd April 2006, 00:03
the very problem isn't that. If I selected a pixmap happen what you see in the pictures....the text isn't clear; and if I put


setText("" );
setPaletteBackgroundPixmap(pix);

happen that my background change in a tiny column;