PDA

View Full Version : Add icon to the right in a QTreeView



hubbobubbo
16th April 2010, 22:32
Hi

I am using my first QTreeView with QStandardItem and I got it to work as I wanted. I have a list with names and an icon to the left like this, it is a contact list:

(icon) Mike
(icon) Peter
(icon) John
(icon) Steve

What I want is a button aligned to the right on each row in the tree view like this:

(icon) Mike...................(icon2)

So when I click icon2 I will open up a screen with some more info about Mike, his phone no, adress etc. Like on the iPhone.

I have tried to read up on the MVC but I really could use some advice to get over the first threshold since I am a bit confused. What do I need to add to my QTreeView/QStandardModel/QStandardModelItem in order to achive this.

Thanks in advance

faldzip
17th April 2010, 08:13
I would suggest implementing custom delegate responsible for drawing and handling clicking.

hubbobubbo
21st April 2010, 17:02
Thanks, I have started to readup on this but I wanted to try the QStandardItem approach first.

What I did is to add some text instead of the icon to the right, like this.
(icon) ContactName.................>>

I did like this, adding two QStandardItem:
QStandardItem* item = new QStandardItem( QIcon(":/gui/images/bigw_online.png"), contactId);
item->setCheckable(true); //Use setcheckState to programatically set the checkbox
item->setEditable(false);

//Add the second item
QStandardItem* item2 = new QStandardItem( QString(">")); //QStandardItems are destroyed by the model they are used in
item2->setCheckable(false);
item2->setEditable(false);
item2->setTextAlignment(Qt::AlignRight);

I could then catch the click on item2 fine, the only problem is that the > characted in item 2 is put outside of the screen so I have to scroll to see it. I tried to shut of horizontal scrolling but that did not work. I tried setSizeHint so the text in item1 did was very short but this also failed.

So I have two QStandardItems, one with icon and text and the second only with text. How can I force them to fit on the screen so there is no horizontal scrolling.

Any ideas or is delegates the only way to go, or would I have the same problem with them?

Thanks alot

aamer4yu
21st April 2010, 19:01
I second to faldzip... use delegates...
give time to it.. it will help in long run