Results 1 to 10 of 10

Thread: Painting TreeWidget

  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Painting TreeWidget

    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 .

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Painting TreeWidget

    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    VireX (9th May 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting TreeWidget

    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?

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting TreeWidget

    You can pass a QBrush::QBrush( QPixmap ) to it and it will draw a tiling pattern.

  6. The following user says thank you to marcel for this useful post:

    VireX (9th May 2007)

  7. #5
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting TreeWidget

    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?
    Last edited by VireX; 9th May 2007 at 07:35.

  8. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting TreeWidget

    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.

  9. #7
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting TreeWidget

    Soounds like a lot of work, especially with no example code to work with.

  10. #8
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting TreeWidget

    The item delegate:
    Qt Code:
    1. class MyItemDelegate : public QItemDelegate
    2. {
    3. Q_OBJECT
    4. public:
    5. MyItemDelegate();
    6. ~MyItemDelegate();
    7. virtual QSize sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    8. virtual void paint ( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const;
    9. private:
    10. QSize mSize;
    11. };
    To copy to clipboard, switch view to plain text mode 

    The item:
    Qt Code:
    1. class MyTreeWidgetItem : public QTreeWidgetItem
    2. {
    3. public:
    4. enum DataRole
    5. {
    6. TextRole = Qt::UserRole + 1,
    7. //other roles
    8. };
    9. MyTreeWidgetItem(... );
    10. ~MyTreeWidgetItem();
    11. QVariant data( int role )const;
    12. };
    To copy to clipboard, switch view to plain text mode 

    The paint method should be something like:
    Qt Code:
    1. void MyItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
    2. {
    3. const QAbstractItemModel* model = index.model();
    4. QString text = model->data(index, MyTreeWidgetItem::TextRole).toString();
    5.  
    6. //here draw the item and the expand handle
    7. //BTW - you should keep a refernece to it's rect for when it is clicked
    8. }
    9.  
    10. This is a basic impl. You can add things to it according to your needs.
    To copy to clipboard, switch view to plain text mode 

  11. #9
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Painting TreeWidget

    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.

  12. #10
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Painting TreeWidget

    Are you sure red.png is in the executable directory?

Similar Threads

  1. About painting
    By Pang in forum Qt Programming
    Replies: 3
    Last Post: 28th March 2007, 17:21
  2. Replies: 2
    Last Post: 30th November 2006, 08:22
  3. Painting and collision detection
    By aamer4yu in forum Qt Programming
    Replies: 1
    Last Post: 18th October 2006, 08:57
  4. selecting lots of treewidget itemns slow ...
    By jh in forum Qt Programming
    Replies: 15
    Last Post: 19th June 2006, 18:52
  5. Painting lines
    By therealjag in forum Newbie
    Replies: 34
    Last Post: 23rd March 2006, 22:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.