You are not logged in.

jiveaxe

Beginner

  • "jiveaxe" is male
  • "jiveaxe" started this thread

Posts: 20

Location: Italy

  • Send private message

1

Thursday, July 26th 2007, 11:04am

2 questions on QTreeWidget

Hi,
I am experimenting with qTreeWidget and change something in the look;

1) how change text look only for the parent item? i have used the following code for rendering a bold text:

#include <QApplication>
#include <QTreeWidgetItem>
#include <QFont>

class TreeWidget : public QTreeWidget
{
protected:
void drawRow(QPainter *, const QStyleOptionViewItem &, const QModelIndex &) const;
};

void TreeWidget::drawRow(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QFont serifFont("Times", 11);
serifFont.setBold(true);

QStyleOptionViewItem myopt(option);
myopt.font = serifFont;
QTreeView::drawRow(painter, myopt, index);
}

int main( int argc, char **argv )
{
QApplication app( argc, argv );
TreeWidget *tree = new TreeWidget();
tree->setColumnCount(2);

QTreeWidgetItem *item = new QTreeWidgetItem(tree,0);
item->setText(0, "Parent");
item->setText(1, "Parent description");

QTreeWidgetItem *item2 = new QTreeWidgetItem(item,1);
item2->setText(0, "Child");
item2->setText(1, "child description");
tree->show();

return app.exec();
}

but parent and child are both in bold.

2) How change the default expand/collapse symbols with custom images?

Thanks,
Giuseppe

Junior

Professional

  • "Junior" is male

Posts: 1,613

Location: San Antonio, TX USA

Occupation: Senior Secure Systems Engineer

  • Send private message

2

Thursday, July 26th 2007, 1:36pm

RE: 2 questions on QTreeWidget

jiveaxe,

Quoted


1) how change text look only for the parent item?

Source code

1
2
3
4
5
6
7
QFont font;
QTreeWidgetItem *item = new QTreeWidgetItem(tree,0);
font = item->font();  // grab item current font
font.setBold( true );  // set bold (and otherthings if needed)
item->setText(0, "Parent");
item->setText(1, "Parent description");
item->setFont( font ); // set font back on item

Quoted


How change the default expand/collapse symbols with custom images?

You can sub class QTreeWidget and redraw the primative element for the state of the element during paint operations:

Check QStyle:as a starting point.

-or- you may be able to control this with a style sheet on the QTreeWidget itself if it is supported.

Hope this helps...

Junior

jiveaxe

Beginner

  • "jiveaxe" is male
  • "jiveaxe" started this thread

Posts: 20

Location: Italy

  • Send private message

3

Thursday, July 26th 2007, 3:03pm

RE: 2 questions on QTreeWidget

Hi junior,
thanks for your help; your code is what i need (in row 3 and 7 is missing the column number as parameter of the functions font() and setFont()).

The other question is unsolved; among available sub-controls in style sheets nothing is related to qtreewidget.

Bye,
Giuseppe

lifthrasir

Trainee

  • "lifthrasir" is male

Posts: 159

Location: France

Occupation: Student

  • Send private message

4

Thursday, July 26th 2007, 4:40pm

RE: 2 questions on QTreeWidget

Quoted

Originally posted by jiveaxe

2) How change the default expand/collapse symbols with custom images?

I see two solutions:
- you re-implement QTreeWidgetItem::drawBranches()
- you create your custom style and re-implement QStyle::drawPrimitive() to custom the primitive element PE_IndicatorBranch

jiveaxe

Beginner

  • "jiveaxe" is male
  • "jiveaxe" started this thread

Posts: 20

Location: Italy

  • Send private message

5

Thursday, July 26th 2007, 5:54pm

RE: 2 questions on QTreeWidget

I was wrong when in the previous post i said that stylesheet hasn't a subcontrol; it is ::branch.

I have added these 2 rows to the code above:

Source code

1
2
tree->setStyleSheet("QTreeView::branch:has-children {image: url(:/images/1rightarrow.png)}"
			"QTreeView::branch:open {image: url(:/images/1downarrow.png)}");


Thanks to Junior and lifthrasir for the precious help.

jiveaxe

Beginner

  • "jiveaxe" is male
  • "jiveaxe" started this thread

Posts: 20

Location: Italy

  • Send private message

6

Monday, July 30th 2007, 10:37am

RE: 2 questions on QTreeWidget

Here I am again; this time with question #1 for qtreeview.
How can i format a bold text for a parent item in qtreeview?
I have created and populated a model which is passed to treeView.
I was able changing the branch with stylesheet but no luck in changing font weight.

As usual , thanks
Giuseppe