Reply
Thread Tools
Posts: 289 | Thanked: 101 times | Joined on Oct 2009
#1
Is there any possibility to make the QListView to a two labeled list like the QValueButton?
 
rooster13's Avatar
Posts: 319 | Thanked: 221 times | Joined on Jan 2010 @ Finland
#2
Do you mean like having two rows on a QListItem?
If yes then use a ListDelegate. Do a google search on it and you'll find examples.

I have used a ListDelegate on my application Shortcut Stash. Check it out and if it is what you need I can share some source code with you.
 

The Following User Says Thank You to rooster13 For This Useful Post:
Posts: 289 | Thanked: 101 times | Joined on Oct 2009
#3
Yes that is exactly what I need! It would be wonderfull if you could share some code. Thanks!
 
rooster13's Avatar
Posts: 319 | Thanked: 221 times | Joined on Jan 2010 @ Finland
#4
Ok, here goes.
Attached is the ListDelegate stuff and the code to add item to QListWidget is as follows:
Code:
ui->listWidget->setItemDelegate(newListDelegate(ui->listWidget)); QListWidgetItem*item=newQListWidgetItem(); QIconappIcon=QIcon::fromTheme(icon,QIcon(":/images/general_applications.png")); item->setData(Qt::DecorationRole,appIcon); item->setData(Qt::DisplayRole,realName); item->setData(Qt::UserRole,QVariant(exec)); item->setData(Qt::UserRole+1,QVariant(comment.trimmed())); item->setData(Qt::WhatsThisRole,QVariant(2)); ui->listWidget->addItem(item);
DecorationRole is the icon
DisplayRole is the main text
UserRole is the data of the item
UserRole + 1 is the description text
and WhatsThisRole is just some additional data for the item.

You can tweak those on the Delegate itself or use whatever roles suits you best.

Hope it helps, and if you ever find out how to resize the icon on the DecorationRole, I would really appreciate if you tell me how you did it, cause I am currently having problems with scalable icons on the QListWidget...
Attached Files
File Type: zip ListDelegate.zip (1.5 KB, 126 views)
 

The Following User Says Thank You to rooster13 For This Useful Post:
Posts: 180 | Thanked: 76 times | Joined on May 2010
#5
Originally Posted by rooster13 View Post
Hope it helps, and if you ever find out how to resize the icon on the DecorationRole, I would really appreciate if you tell me how you did it, cause I am currently having problems with scalable icons on the QListWidget...
You draw the icon with QIcon:: paint method. Maybe you should use QPainter::drawPixmap instead.

The pixmap is scaled to fit the rectangle, if both the pixmap and rectangle size disagree.
 

The Following User Says Thank You to Diph For This Useful Post:
rooster13's Avatar
Posts: 319 | Thanked: 221 times | Joined on Jan 2010 @ Finland
#6
Cheers, I actually figured it out after I posted this.
I used the method in painter to set the icon size and it's working fine.
 
Posts: 289 | Thanked: 101 times | Joined on Oct 2009
#7
I got almost everything to work by now, it is just a small but serious problem left.

My program is designed so that on the mainwindow I have two different windows wich should have a listdelegate listview and one of these two should have one too. The problem that when I include the header file it tells me it have already been defined and points on "class ListDelegate : public QAbstractItemDelegate" and tell me previous definition was at the row below wich is just a "{".

I can remember I had a problem before quite like this and then it was something with the instances got into a fight about who was the parent and the problem was called something with circular. As I can not remember what it was its to hard to find out Any help?


Yours,
 
rooster13's Avatar
Posts: 319 | Thanked: 221 times | Joined on Jan 2010 @ Finland
#8
I'm no C++ expert and without having your code it's really hard to say what's wrong.
I bet u have already googled a lot on this issue, but could this help?

Especially this part:
"Circular Dependencies vs bidirectional relationships

Whenever one file #includes another, there is a strong dependency created between the files. When a dependency like this exists between header files, it can not be bidirectional: the preprocessor is unable to cope with a circular dependency between qheader files, where each one #includes the other. One of the #include statements must be replaced by a forward class declaration.

Forward declarations help remove circular dependencies between classes, and in the process, enable bidirectional relationships to exist between them."

Are u by any chance including the Delegate twice in two different classes?
 
Posts: 180 | Thanked: 76 times | Joined on May 2010
#9
Originally Posted by rooster13 View Post
IAre u by any chance including the Delegate twice in two different classes?
That shouldn't be a problem.

But if you have it like this way there is a problem:

file1.h:
Code:
#include "file2.h"
file2.h
Code:
#include "file1.h"
Use forward declaration like rooster13 said.
 

The Following User Says Thank You to Diph For This Useful Post:
rooster13's Avatar
Posts: 319 | Thanked: 221 times | Joined on Jan 2010 @ Finland
#10
Originally Posted by Diph View Post
But if you have it like this way there is a problem:

file1.h:
Code:
#include "file2.h"
file2.h
Code:
#include "file1.h"
Use forward declaration like rooster13 said.
Thx, that's what I was actually trying to say
If we were talking about java then I could've explained it right.
 
Reply


 
Forum Jump


All times are GMT. The time now is 00:17.