PDA

View Full Version : QListWidget...what's wrong



nupul
1st April 2006, 19:03
i wish to display the file icon of listview.cpp in the QListView Widget...but am just getting the text passed to the constructor i.e. "nupul"

What is wrong?



#include <QApplication>
#include <QListWidget>

int main(int argc, char *argv[])
{

QApplication app(argc, argv);

QListWidget *l=new QListWidget(0);
l->setViewMode(QListView::IconMode);

QIcon icon("/Qt/4.1.0/project/listview/listview.cpp");
QListWidgetItem *item=new QListWidgetItem(icon, "nupul",l,0);

l->addItem(item);

l->show();

return app.exec();

}


Thanks

Nupul

wysota
1st April 2006, 19:07
"/Qt/4.1.0/project/listview/listview.cpp" is this a valid path of an icon? If nothing changed, files with "cpp" extension tend to be c++ source files and not icons. And it's doubtfull that your Qt directory is a direct child of root of the directory tree (unless you have a weird installation).

jpn
1st April 2006, 19:14
I suppose you are looking for something like this:

QFileIconProvider iconProvider;
QFileInfo fileInfo("...listview.cpp");
QIcon icon = iconProvider.icon(fileInfo);
QListWidgetItem *item=new QListWidgetItem(icon, "nupul",l,0);

nupul
1st April 2006, 19:26
Well Wysota, this is what the Qt4.1 doc has to say



QIcon::QIcon ( const QString & fileName )
Constructs an icon from the file with the given fileName. The file will be loaded on demand. If the file does not exist or is of an unknown format, the icon becomes a null icon.
If fileName contains a relative path (e.g. the filename only) the relevant file must be found relative to the runtime working directory.
The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.


Which implies that an icon name needn't be passed...

as for the path on my system is concerned....Qt is installed in windows and thus the strange path....I chose it ;)

p.s. I took your suggestion and also gave a path to a .ico file....No luck
:confused:

Nupul

jpn
1st April 2006, 19:49
The file must be a supported image format (http://doc.trolltech.com/4.1/qimage.html#reading-and-writing-image-files).

wysota
1st April 2006, 20:20
Which implies that an icon name needn't be passed...
"fileName" is not the name of a file for which the icon is to be generated but a name of a file which contains an image that is to be loaded as an icon.


as for the path on my system is concerned....Qt is installed in windows and thus the strange path....I chose it ;)
Shouldn't it start with a drive letter?


p.s. I took your suggestion and also gave a path to a .ico file....No luck

Qt doesn't handle .ico files (see the link in the previous post).

nupul
2nd April 2006, 12:23
Firstly Thanks for the replies...but I am still confused

:confused:

As per the qt doc:


The simplest way to create a Q3IconView is to create a Q3IconView object and create some Q3IconViewItems with the Q3IconView as their parent, set the icon view's geometry and show it. For example:


Q3IconView *iv = new Q3IconView(this);
QDir dir(path, "*.xpm");

for (uint i = 0; i < dir.count(); i++)
{
(void) new Q3IconViewItem(iv, dir[i], QPixmap(path + dir[i]));
}

iv->resize(600, 400);
iv->show();



W.r.t to the above code could anyone pls explain the Constructor of Qdir as given...Is it:



QDir ( const QString & path, const QString & nameFilter, SortFlags sort = SortFlags( Name | IgnoreCase ), Filters filters = AllEntries )


...where the sortflags and filters are optional??

Also what is this code doing different from what I put forth before??

I intend to display the contents of "/homes/nupul/desktop" folder on the ListWidget exactly how my linux (SuSE 10) system would show it on the Desktop...with all the associated icons for the respective files.

Do i need to custom create my icons w.r.t the allowable Image formats supported by Qt4? Can't I use the Icons provided by the system? <-- how should i achieve the latter?

The QListWidgetItem Constructor is given as:



QListWidgetItem ( const QIcon & icon, const QString & text, QListWidget * parent = 0, int type = Type )



what is the 'text' for? it's the text to be displayed below the icon, right? I want the file name to appear below the Icon...so what should I do?

what is the last int type=Type for and how does it affect the code???


Also could you please elaborate (in simple terms and NOT doc language) on the Use of QIconEngine...when/why should one use it?

Thanks

Nupul

jpn
2nd April 2006, 13:17
So are you using a QListWidget or Q3IconView?

I would suggest you to check out QDirModel+(QTreeView/QListView) combination.
With those you can very easily show contents of the file system.

For system specific icons you need to provide your own QFileIconProvider.
QFileIconProvider is a simple class which (as the name suggests) provides icons.
The default QFileIconProvider provided by Qt handles only a few types of icons (Computer, Desktop, Trashcan, Network, Drive, Folder, File). By subclassing QFileIconProvider you can supply system specific icons for QDirModel.

nupul
2nd April 2006, 14:12
well i will be using Qlistwidget only....QIconView was just an example i stumbled on while sifting through the doc...

I wish to use the System Icons only i.e those by KDE 3.2 on my SuSE 10....and wish to display them as is in Qt - (as i mentioned earlier). Is There NO way to do this in Qt without tinkering around with the QFileIconProvider....

...hope you understand what I am trying to say....

e.g say i wish to design Konqueror or a windows explorer type application....I want the contents of a folder to be displayed on the right listwidget with the same icons as the system uses. Shouldn't Qt be able to do this Directly??

Also in the book C++ programming with Qt3, there is a find-file-dialog example....in which the listview at the bottom displays the files just as a normal Find dialog in windows/X would....i.e. the corresponding file icons are shown....but there is no mention of it's implementation...



Thanks

SkripT
2nd April 2006, 14:30
If you want the system icons try to get the standard pixamp of the current style with:

style -> standardPixmap(QStyle::SP_DirOpenIcon)
for example it will return the standar pixamp for the folder opened icon. Take a look at all the other standarPixamps defined in QStyle class. Then you only have to set this pixamp to the icon you want.

jpn
2nd April 2006, 14:46
File icon bindings are system specific.
In windows you would probably have to use SHGetFileInfo.
Somewhy I have a strong feeling that on X11, this even differs on different desktops.

wysota
2nd April 2006, 16:02
If you wish to bind your application tightly with KDE, you should consider switching to KDE-based development. Then you'll be able to use icons provided by KDE directly. But this will make your application unusable on non-kde systems. And also remember that KDE4 is still in early development stage, so you might need to switch to Qt3/KDE3.

nupul
3rd April 2006, 04:59
Well let me tell you all what i intend to do....I am creating a desktop manager for Linux (SuSE 9/10) as a project and am using Qt for it's development. If Wysota, jpn will try and recollect most of my doubts are indirectly/directly related to components related to the desktop manager viz. panel, toolbars, etc..

Now the thing is this....I will be displaying the icons of /homes/<user name/desktop
on the desktop....and I'll be using QListWidget for the same. Now what you need to tell me is how should I display the Icons depicting various files

1. Should I subclass QFileIconProvider??
--then you need to give me some hints on how to do it, as I never thought that I'd need to custom design my own Icons :eek:

or

2. Is it possible to display the Icons provided by the underlying system?
--I will need some help here too ;)

If at all i design my own icons...pls tell me the preferable file format to use for icon images and how do I make sure that I cover all the icons in the system

If i am not wrong there is a folder in KDE (can't recollect at the moment though) where the icon images are stored...how can I make use of this??

Thanks

Nupul

wysota
3rd April 2006, 11:24
You can subclass QFileIconProvider which will use /usr/share/icons (or simmilar) to fetch and provide icons.

nupul
3rd April 2006, 12:10
It seems i'll be taking a "hybrid" of the 2 ways of using icons, i mentioned before. But there is still one thing....

I want QListWidget to open the corresponding folder/file when the user double/single clicks...It didn't work by default ( or was it supposed to?!?). So do i need to add signals and slots for it or what....???

Thanks

wysota
3rd April 2006, 13:30
You're not using QDirModel, right? So how do you expect the list of icons open some directory by itself? You have to connect signals you want to slots where you'll implement that functionality yourself. Alternatively you can use QDirModel and have the behaviour out of the box :)

nupul
4th April 2006, 12:17
Phew!!!.........

Firstly a BIG thanks to all of you to help me in this issue...
After receiving clarification/guidance from you guys, I did a more extensive study from the Qt4 doc (er..with more concentration) ;) and finally understood what all were trying to make me understand

Concluding from the discussion, this is what I'll be doing :

1. Will be using the QListView+QDirModel structure for displaying the icons on 'my' desktop.

2. Will be subclassing the QFileIconProvider and providing for my own icons (actually got them from KDE distro....they aren't used by any of the distros I know...and they are open source too :) ...saves me the pain of custom developing my own icons ;))

3. Will clarify doubts @ qtcentre as and when they arise!!! :D

Thanks!

Nupul.