PDA

View Full Version : How to show two icon in QTreeView



Pardeep
5th February 2013, 07:24
Hello

I am developing an application using Christophe Dumez's blog cdumez http://blogspot.in/2010/11/how-to-use-c-list-model-in-qml.htm
Then i am showing my items using QTreeView.
In tree model it take only one icon.

Now i want to show two icons (One on left side of text & other on right side of text) in same row.
so how can i do that.
Can any one suggest me(any example) for that

Stanfillirenfro
6th February 2013, 09:08
Maybe could you provide a piece of your code?

Pardeep
6th February 2013, 10:03
My model is custom, which is an abstract model. I have learned about Item delegate & try to find example code to display tow icons in a row but not successful. I found example about custom delegate e.g star delegate(http://qt-project.org/doc/qt-4.8/itemviews-stardelegate.html) but nothing for TreeView. I want to only display two icons in a row which display when my treeview display. But still i don't have any idea how to do that, so thats why i can't provide any code?

wysota
6th February 2013, 10:46
QTreeView uses the same delegates as other view classes. You can use exactly the same approach. I have no idea what it has to do with the QML-related block post you linked to in your opening post.

Pardeep
11th February 2013, 11:05
Hi,
i am using QItemDelegates paint method for custom delegate. My code is as following
.h file


#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QSpinBox>

class TreeViewItemDelegate : public QItemDelegate
{
Q_OBJECT

public:
TreeViewItemDelegate(QObject *parent = 0);

void paint( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const;
};
.cpp file


TreeViewItemDelegate::TreeViewItemDelegate(QObject *parent)
: QItemDelegate(parent)
{
}

void TreeViewItemDelegate ::paint( QPainter * painter,
const QStyleOptionViewItem & option,
const QModelIndex & index ) const
{
QString contactURI = index.model()->data(index, Qt::DisplayRole).toString();
QIcon icon = qvariant_cast<QIcon>(index.model()->data(index, Qt::DecorationRole));
QIcon icon2 = qvariant_cast<QIcon>(index.model()->data(index, Qt::UserRole+5));

QPixmap iconPixmap = icon.pixmap(option.decorationSize);
QPixmap iconPixmap2 = icon2.pixmap(option.decorationSize);

QStyleOptionViewItem myoption = option;
QStyleOptionViewItem myoption2 = option;
QStyleOptionViewItem myoption3 = option;
myoption.displayAlignment = Qt::AlignCenter;
myoption2.decorationAlignment = Qt::AlignLeft;
myoption3.decorationAlignment = Qt::AlignRight;

drawBackground(painter, myoption, index);
drawDecoration(painter, myoption, myoption2.rect, iconPixmap);
drawDisplay(painter,myoption, myoption.rect, contactURI);
drawDecoration(painter, myoption, myoption3.rect, iconPixmap2);
drawFocus(painter, myoption,myoption.rect);
}
There is on problem that only one icon is displaying. I know i am doing something wrong, so can anyone help me to correct this code to display both icon??

wysota
11th February 2013, 11:36
Don't call drawDecoration() for the additional icon. Instead use QPainter::drawPixmap() directly.

Pardeep
11th February 2013, 13:04
i added two more lines in my cpp file



QRect *rect = new QRect(0,0,10,10);
painter->drawPixmap(rect,iconPixmap);

but it is not working..

wysota
11th February 2013, 13:52
It doesn't even compile. And if it did, the rect probably doesn't make sense anyway.