PDA

View Full Version : Painting TreeWidget



VireX
9th May 2007, 05:24
Hello,
I been wondering how to use palettes and subclasses to properly format a TreeWidget with some sort of style. Example, I want a different font and background image for TopLevelNodes and different color fonts for certain items. I can color the background of TreeWidget with stylesheets, but I have little experience with painting and figuring that out maybe because there aren't many specifics on that issue.

What is the proper way to do this? Can someone describe to me what I need, or if its easy, show me some example/pseudo code please? It would be greatly appreciated :D.

jpn
9th May 2007, 07:07
QTreeWidgetItem::setFont() (font)
QTreeWidgetItem::setForeground() (text color)
QTreeWidgetItem::setBackground() (background brush)

?

VireX
9th May 2007, 07:27
But thats not an image its a color brush! Should I use: setTexture ( const QPixmap & pixmap ) ?

Also is there a way to repeat, example, I wanna put a colored line on a QTreeWidget, but I want it to repeat horizontally, regardless of what column or width of the Widget, could I do that?

marcel
9th May 2007, 07:28
You can pass a QBrush::QBrush( QPixmap ) to it and it will draw a tiling pattern.

VireX
9th May 2007, 07:30
Hmm, i think that should work, lemme give it a shot, thanks.

Actually, what about changing the + - expand/collapse icons on the QTreeWidget, is there a way to change that image?

marcel
9th May 2007, 07:46
Actually, what about changing the + - expand/collapse icons on the QTreeWidget, is there a way to change that image?

You have to write your custom item and item delegate for that.
But for this you will have to use a QTreeView.
It is some work involved, but not that much.

VireX
9th May 2007, 07:56
Soounds like a lot of work, especially with no example code to work with.

marcel
9th May 2007, 08:10
The item delegate:


class MyItemDelegate : public QItemDelegate
{
Q_OBJECT
public:
MyItemDelegate();
~MyItemDelegate();
virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
private:
QSize mSize;
};


The item:


class MyTreeWidgetItem : public QTreeWidgetItem
{
public:
enum DataRole
{
TextRole = Qt::UserRole + 1,
//other roles
};
MyTreeWidgetItem(... );
~MyTreeWidgetItem();
QVariant data( int role )const;
};


The paint method should be something like:


void MyItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
const QAbstractItemModel* model = index.model();
QString text = model->data(index, MyTreeWidgetItem::TextRole).toString();

//here draw the item and the expand handle
//BTW - you should keep a refernece to it's rect for when it is clicked
}

This is a basic impl. You can add things to it according to your needs.

VireX
9th May 2007, 08:30
I may or may not attempt that, but thanks for the help marcel.

Also, I tried using:
qtwI->setBackground(0, QBrush(QPixmap("red.png")));
And it didn't work, although SetFont worked.

marcel
9th May 2007, 08:36
Are you sure red.png is in the executable directory?