Results 1 to 6 of 6

Thread: QTreeView::drawRow() problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Question QTreeView::drawRow() problem

    Hi
    I've reimplemented QTreeView::drawRow(). Everything is fine except I need a way to determine how to draw alternating row background or in short how to find out whether background should be drawn with alternate background colour or not. Currently I'm using this
    Qt Code:
    1. const int def_item_height = itemDelegate()->sizeHint(option, model->index(0, 0)).height();
    2. int row_no;
    3. if(verticalScrollMode() == QAbstractItemView::ScrollPerItem){
    4. row_no = (visualRect(index).top() / def_item_height) + verticalScrollBar()->value();
    5. }else{
    6. row_no = (verticalScrollBar()->value() + visualRect(index).top()) / def_item_height;
    7. }
    8. const bool alternate_on = row_no & 1;
    To copy to clipboard, switch view to plain text mode 
    As you can see, above code only works if all rows have same height. Any suggestions, please...

  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: QTreeView::drawRow() problem

    QTreeView seems to store that information as a private member before it calls drawRow() from drawTree(). There is no way for you to access that private member unless you include a private header (which might not be available on all environments so you might not want to do that)...

    This is roughly the way QTreeView does it ("d" is a pointer to QTreeViewPrivate):
    Qt Code:
    1. void QTreeView::drawTree(QPainter *painter, const QRegion &region) const
    2. {
    3. ...
    4.  
    5. foreach (visible item) {
    6. ...
    7. d->current = i;
    8. ...
    9. drawRow(painter, option, viewItems.at(i).index);
    10. }
    11.  
    12. ...
    13. }
    14.  
    15. void QTreeView::drawRow(QPainter *painter, const QStyleOptionViewItem &option,
    16. const QModelIndex &index) const
    17. {
    18. ...
    19.  
    20. if (alternate) {
    21. if (d->current & 1) {
    22. opt.features |= QStyleOptionViewItemV2::Alternate;
    23. } else {
    24. opt.features &= ~QStyleOptionViewItemV2::Alternate;
    25. }
    26. }
    27.  
    28. ...
    29. }
    To copy to clipboard, switch view to plain text mode 
    It would be much nicer if QTreeView::drawTree() would set the option so you could just check it from opt.features in QTreeView::drawRow() reimplementation...

    But one question: why do you need a custom drawRow()?
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView::drawRow() problem

    Quote Originally Posted by jpn View Post
    QTreeView seems to store that information as a private member before it calls drawRow() from drawTree(). There is no way for you to access that private member unless you include a private header (which might not be available on all environments so you might not want to do that)...
    Just wondered if there was something which I was missing; so the thread.

    Quote Originally Posted by jpn View Post
    But one question: why do you need a custom drawRow()?
    1. Always draw branches on the first *visible* column
    2. Always have the first visible column span all columns if it has child rows


    Actually I just had to copy code from QTreeView::drawRow() and change it by about 20%. Apart from alternate background, there are two more places where I need the data stored in private pointer; while one of them I can certainly live without, following is the one I'm not sure of
    Qt Code:
    1. // when the row contains an index widget which has focus,
    2. // we want to paint the entire row as active
    3. bool indexWidgetHasFocus = false;
    4. if ((current.row() == index.row()) && !d->editors.isEmpty()) {
    5. const int r = index.row();
    6. QWidget *fw = QApplication::focusWidget();
    7. for (int c = 0; c < header->count(); ++c) {
    8. QModelIndex idx = d->model->index(r, c, parent);
    9. if (QWidget *editor = indexWidget(idx)) {
    10. if (ancestorOf(editor, fw)) {
    11. indexWidgetHasFocus = true;
    12. break;
    13. }
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: QTreeView::drawRow() problem

    Quote Originally Posted by yogeshm02 View Post
    1. Always draw branches on the first *visible* column
    Have you tried QTreeView::drawBranches()?

    Quote Originally Posted by yogeshm02 View Post
    1. Always have the first visible column span all columns if it has child rows
    Have you tried QTreeView::setFirstColumnSpanned()?
    J-P Nurmi

  5. #5
    Join Date
    Jan 2006
    Location
    India
    Posts
    115
    Thanks
    3
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTreeView::drawRow() problem

    Quote Originally Posted by jpn View Post
    Yes, but it is very tightly built around drawRow(). So, I cant use it for this purpose.

    Quote Originally Posted by jpn View Post
    Yes. The problem with it is that setFirstColumnSpanned() needs to be explicitly set on the index; this seems a bit cumbersome given that it can be done automatically with drawRow(). It also draws selection thinking that selection always start at column having index == 0 and hence when that column is moved the selection is drawn in two parts.

  6. #6
    Join Date
    Oct 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTreeView::drawRow() problem

    Quote Originally Posted by yogeshm02 View Post
    Actually I just had to copy code from QTreeView::drawRow() and change it by about 20%. Apart from alternate background, there are two more places where I need the data stored in private pointer; while one of them I can certainly live without, following is the one I'm not sure of <...>
    Hi yogeshm02,

    Did you find a way how to obtain pointer to the private data?
    I also have to override drawRow or most likely even drawTree method and I need that pointer to make it work.

Similar Threads

  1. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  2. PyQt QTimer problem { FIXED }
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 1st March 2008, 16:50
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.