View Full Version : how to preview of image in QFileDialog by default
yoll522
26th April 2014, 15:01
hi,
I am using QFileDialog::getOpenFileNames() in my code
it is good to use in Windows 7
10315
but I find a question in Windows XP, that is QFileDialog has no preview image,
like this photo
10317
I must manually open the preview from red circle every time when I use QFileDialog::getOpenFileNames()
so I would like to ask is there any way to automatic open preview image?
thanks a lot
and, sorry for my bad English.
anda_skoa
26th April 2014, 15:38
The two versions of the operating system probably have different default file dialogs, which is what Qt is using for its static convenience methods.
You could experiement with creating a QFileDialog instance manually and providing a custom QFileIconProvder when on XP
Cheers,
_
yoll522
27th April 2014, 15:04
The two versions of the operating system probably have different default file dialogs, which is what Qt is using for its static convenience methods.
You could experiement with creating a QFileDialog instance manually and providing a custom QFileIconProvder when on XP
Cheers,
_
Hi, thank you your answer
today, I try to make FileDialog,
I use QFileSystemModel in QListView
QFileSystemModel *fileSystemModel = new QFileSystemModel();
ui->listView_file->setModel(fileSystemModel);
this is a picture of my program
10319
please help me again,
I would like to ask how to let it show preview image on QListView?
like QFileDialog have preview image
10320
thank you very much
anda_skoa
27th April 2014, 16:28
Have you tried what I suggested?
Cheers,
_
yoll522
27th April 2014, 17:19
yes
but I going to look QFileIconProvider document, it have "icon" function,
it is to provide systematic image, like the following instructions
enum QFileIconProvider::IconType
QFileIconProvider::Computer
QFileIconProvider::Desktop
QFileIconProvider::Trashcan
QFileIconProvider::Network
QFileIconProvider::Drive
QFileIconProvider::Folder
QFileIconProvider::File
I try it to use, this is my code
QFileIconProvider fileIcon;
ui->pushButton->setIcon(fileIcon.icon(QFileInfo("1.jpg")));
but I got this answer
10321
it also have no preview Image
maybe I was wrong about something
could you please guide me
thanks you very much
anda_skoa
27th April 2014, 17:32
My suggestion was to use a QFileDialog instance and set your own QFileIconProvider, i.e. one that implemens the icon(QFileInfo) function such that it creates a preview image of the file.
Of course you can implement your own file dialog as well, but that will be quite some work.
Cheers,
__
yoll522
28th April 2014, 08:59
My suggestion was to use a QFileDialog instance and set your own QFileIconProvider, i.e. one that implemens the icon(QFileInfo) function such that it creates a preview image of the file.
Of course you can implement your own file dialog as well, but that will be quite some work.
Cheers,
__
hi, I try to Inheritance QFileIconProvider and over write icon function, this is my code
class myFileIconProvider:public QFileIconProvider {
public:
QIcon icon(const QFileInfo &info) const{
if(!QFileInfo(info.filePath()).isDir())
{
//if the file is image
if( info.suffix().compare("jpg",Qt::CaseInsensitive)==0 ||
info.suffix().compare("bmp",Qt::CaseInsensitive)==0 ||
info.suffix().compare("png",Qt::CaseInsensitive)==0 ||
info.suffix().compare("jpeg",Qt::CaseInsensitive)==0 )
{
//creat a preview image to QIcon
QIcon myIcon(info.filePath());
return myIcon;
}
}
//if the file is not image, return default icon
return QFileIconProvider::icon(info);
}
};
myFileIconProvider fileIconProvider;
QFileSystemModel fileSystemModel;
fileSystemModel.setIconProvider(&fileIconProvider);
ui->listView_file->setModel(fileSystemModel);
10322
it look good, but some problem in program
if the file is too much, then the ui->listView_file (QListView) will delay for a few second,
I think maybe icon function draw the QIcon was slowly,
if I go into a folder has 20 image, than the program will delay 2-3 second ˇ_ˇ
anyway, I am very grateful for your help
anda_skoa
28th April 2014, 13:23
Hmm.
Another option might be a class derived from QFileSystemModel or a QIdentityProxyModel subclass on top of a normal QFileSystemModel.
When an icon for a file (Qt::DecorationRole) is requested, a cache lookup could check if there is already a thumbnail for the given file.
if not, return a standard icon and trigger an asynchronous creation of the thumbail and notify the view when it is ready.
Cheers,
_
yoll522
29th April 2014, 09:24
Hmm.
Another option might be a class derived from QFileSystemModel or a QIdentityProxyModel subclass on top of a normal QFileSystemModel.
When an icon for a file (Qt::DecorationRole) is requested, a cache lookup could check if there is already a thumbnail for the given file.
if not, return a standard icon and trigger an asynchronous creation of the thumbail and notify the view when it is ready.
Cheers,
_
I try to create myFileSystemModel class, but I don't know how to load thumbnail from system
I use QPixmapCache and use "fine" function, but it seem to be looking the cache from program, not system.
so, if my program restart, the thumbnail will be reload,
could you please give me more tips, thank you.
this is my code:
class cMyFileSystemModel : public QFileSystemModel{
public:
QVariant data (const QModelIndex & index, int role = Qt::DisplayRole) const{
if(role == Qt::DecorationRole){
QFileInfo fileInfo = this->fileInfo(index);
if( fileInfo.suffix().compare("jpg",Qt::CaseInsensitive)==0 ||
fileInfo.suffix().compare("bmp",Qt::CaseInsensitive)==0 ||
fileInfo.suffix().compare("png",Qt::CaseInsensitive)==0 ||
fileInfo.suffix().compare("jpeg",Qt::CaseInsensitive)==0 )
{
QPixmap pm;
if (!QPixmapCache::find(fileInfo.filePath(), &pm)) {
pm.load(fileInfo.filePath());
QPixmapCache::insert(fileInfo.filePath(), pm);
QIcon myIcon(pm);
return myIcon;
}
}else{
QIcon myIcon(pm);
return myIcon;
}
}
}
return QFileSystemModel::data(index,role);
}
};
anda_skoa
29th April 2014, 13:08
I use QPixmapCache and use "fine" function, but it seem to be looking the cache from program, not system.
I am not sure what you mean. "The QPixmapCache class provides an application-wide cache for pixmaps."
so, if my program restart, the thumbnail will be reload,
Your code doesn't show any persitance handling, i.e. no saving to disk or initializing the cache from disk.
Maybe you forgot that and you are starting with an empty cache on each startup?
Cheers,
_
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.