Results 1 to 3 of 3

Thread: Draw grid in QListView

  1. #1
    Join Date
    Mar 2010
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Draw grid in QListView

    Hi there

    I'd like to draw lines between items in my list.

    I try to do it in my delegate (derived from QStyledItemDelegate):


    Qt Code:
    1. void MembersListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    2. if(!index.isValid())
    3. return;
    4.  
    5. painter->save();
    6. painter->setRenderHint(QPainter::Antialiasing);
    7.  
    8. QStyleOptionViewItemV4 opt = option;
    9. QStyledItemDelegate::initStyleOption(&opt, index);
    10.  
    11. // ...
    12.  
    13. // seperation lines
    14. QPen linePen(Qt::gray);
    15. painter->setPen(linePen);
    16. painter->drawLine(0, opt.rect.height(), opt.rect.width(), opt.rect.height());
    17.  
    18. painter->restore();
    19. }
    To copy to clipboard, switch view to plain text mode 

    If I do so the line is only painted under the first item. The paint method should be called for each index and paint the line on bottom of the item.

    Isn't there a ready made function for the lines between items like in QTableView::setShowGrid(bool show)?

    Thaks!
    Luke
    Using Qt 4.7
    Developping on Win 7 and XP
    Using Qt Creator, Eclipse and Visual Studio
    Target Platforms Win, Linux and soon OS X

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Draw grid in QListView

    opt.rect.height(), opt.rect.width(), opt.rect.height()
    will always return the same numbers.. u need opt.rect.x() opt.rect.y()

  3. #3
    Join Date
    Mar 2010
    Posts
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw grid in QListView

    thanks!

    I used some functions of QRect:
    Qt Code:
    1. painter->drawLine(opt.rect.bottomLeft(), opt.rect.bottomRight());
    To copy to clipboard, switch view to plain text mode 
    Using Qt 4.7
    Developping on Win 7 and XP
    Using Qt Creator, Eclipse and Visual Studio
    Target Platforms Win, Linux and soon OS X

Similar Threads

  1. Replies: 0
    Last Post: 30th August 2010, 20:08
  2. Cant't draw custom grid
    By Tottish in forum Qwt
    Replies: 4
    Last Post: 1st May 2010, 10:55
  3. Replies: 5
    Last Post: 21st January 2010, 16:55
  4. How do draw a frame in QListView?
    By someralex in forum Qt Programming
    Replies: 24
    Last Post: 21st December 2006, 12:56

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.