Results 1 to 2 of 2

Thread: Using multiple QFonts in QTreeView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Using multiple QFonts in QTreeView

    Hello,

    I am trying to create a QTreeView that can have different fonts used for different rows of the tree. I know that I can do this with the QTreeWidget convenience class, but I need some extra functionality that this class can't provide (e.g. making only a certain column editable).

    I think that the way to do this is by subclassing QTreeView and using different QPainters for different rows. However, I am unable to get my TreeView class to use my painter's font.

    This is currently what I have in my subclassed TreeView's paintEvent function (it does not use the specified font):

    Qt Code:
    1. void TreeView::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this->viewport());
    4. QFont serifFont("Times", 11, QFont::Bold);
    5. painter.setFont(serifFont);
    6. drawTree(&painter, event->region());
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    However, if I code it this way instead, it does end up using "serifFont".

    Qt Code:
    1. void TreeView::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this->viewport());
    4. QFont serifFont("Times", 11, QFont::Bold);
    5. setFont(serifFont);
    6. drawTree(&painter, event->region());
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    Can anybody tell me how to get the TreeView to use the painter's font? Or is there a better way of having multiple fonts in a QTreeView without using the convenience classes?

    Thanks

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Using multiple QFonts in QTreeView

    QTreeView can uses the font provided by model, you can return the required font as per row & column from the model instead setting in custom view. Check for Qt::FontRole in data () in model implementation

Similar Threads

  1. QTreeView and multiple models
    By Daher in forum Qt Programming
    Replies: 3
    Last Post: 3rd December 2019, 20:57
  2. QTreeView with multiple QDir models
    By papillon in forum Qt Programming
    Replies: 3
    Last Post: 11th November 2011, 23:23
  3. QTreeView with multiple models
    By Daher in forum Qt Programming
    Replies: 5
    Last Post: 18th October 2011, 10:53
  4. [QT4] QTreeView and rows with multiple lines
    By KShots in forum Qt Programming
    Replies: 8
    Last Post: 30th March 2006, 18:47
  5. Replies: 2
    Last Post: 10th March 2006, 18:04

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.