Results 1 to 18 of 18

Thread: [QT4 M/V] QTreeView::DrawBranches()

  1. #1
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default [QT4 M/V] QTreeView::DrawBranches()

    Hello all,

    I've ported my application to Qt4 using qt model view framework.
    But my TreeView (which extends QTreeView) does not paint the dotted line (branches).

    How i can do it?
    I've tried to overload drawBranches, drawTree and drawRow without success.

    Any help would be highly appreciated...!

    PS: I saw some threads on this forum but none helped me...

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    Well, please give us more details

    Possible 'mistakes' are:
    * signature errors (forgotten const)
    * QTreeView::rootIsDecorated set to false
    * indentation set to 0

    But without more information it is hard to help.

    Christoph

  3. #3
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    Quote Originally Posted by caduel View Post
    Well, please give us more details

    Possible 'mistakes' are:
    * signature errors (forgotten const)
    * QTreeView::rootIsDecorated set to false
    * indentation set to 0

    But without more information it is hard to help.

    Christoph
    Thanks for your answer.

    * Signatures are same (the plus/minus color is changed)
    * rootIsDecorated set to false should not interfer with the drawing of items at depth >= 2 (i guess).
    * indentation != 0

    More precisely, i think i don't know what i should write in drawbranches or drawTree...
    Here is an example code i tried... I try to put the same on drawbranches...
    Qt Code:
    1. void MyTreeView::drawTree(QPainter *painter, const QRegion &region) const {
    2. QColor color(Qt::blue);
    3. painter->setPen(color);
    4. painter->save();
    5.  
    6. QTreeView::drawTree(painter, region);
    7. painter->restore();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Any tip would be appreciated.
    I simply want a trivial dotted line

    (But no QStyleSheet)

  4. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    I havent used that function myself, but can suggest the following to try -

    try drawing something in that function. MYTreeView::drawTree()
    something like -
    painter->drawLine(region.x,region.y,region.x+region.width( ),region.y+region.height);

    Also check how calling the base function works ( QTreeView::drawTree(painter, region)) .
    try calling QTreeView::drawTree once before the drawline. and once after it. Hope you will move ahead

  5. #5
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    I don't quite see, what the purpose of your drawTree function is.
    You modify the pen (which might be overriden internally, if I had to guess) and then restore the painter afterwards?
    I would guess that QTreeView restores its own modifications anyway.

    That aside, try to replace your MyTreeView with an instance of QTreeView.
    If the dotted line still is not drawn, the issue is not with your code but with the configuration of the tree view. If it is drawn, check your code closer.

    If that check should show that indeed your code is responsible, then I would comment out drawTree (and other functions you might have overloadad) and find the culprit that way.

    HTH

    PS: You don't call drawTree yourself, do you?

  6. #6
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    Quote Originally Posted by aamer4yu View Post
    I havent used that function myself, but can suggest the following to try -

    try drawing something in that function. MYTreeView::drawTree()
    something like -
    painter->drawLine(region.x,region.y,region.x+region.width( ),region.y+region.height);

    Also check how calling the base function works ( QTreeView::drawTree(painter, region)) .
    try calling QTreeView::drawTree once before the drawline. and once after it. Hope you will move ahead
    Thanks for helping.
    I simply put a breakpoint in my drawTree method and see that she's never called...
    Signature is the same, this method is protected in QTreeView and i extend this class. (don't understand!)

    However my drawbranches get called. And the above code change the plus/minus color...


    Quote Originally Posted by caduel View Post
    I don't quite see, what the purpose of your drawTree function is.
    You modify the pen (which might be overriden internally, if I had to guess) and then restore the painter afterwards?
    I would guess that QTreeView restores its own modifications anyway.

    That aside, try to replace your MyTreeView with an instance of QTreeView.
    If the dotted line still is not drawn, the issue is not with your code but with the configuration of the tree view. If it is drawn, check your code closer.

    If that check should show that indeed your code is responsible, then I would comment out drawTree (and other functions you might have overloadad) and find the culprit that way.

    HTH

    PS: You don't call drawTree yourself, do you?
    I read somewhere that the painter is a stack. save operations put on stack and are *never* (i guess) released. The restore operation is the only way to do that.
    But i guess youre right on the fact that this pen should be overwritten.
    BUT (lol) my pen color in drawBranches (see above) still the one i specified...

    The dotted line is printed but in the same color as the treeView background(white) so she's not visible. If you select items this line appears in white.

    I will try to change by a simple qtreeview.

    PS: No i don't call drawTree() myself.
    Thanks all for helping

  7. #7
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    Ok it seems that the problem is in my code.
    I have used the same model/view at another place, and branches are correctly displayed...
    I tried to apply same options as the working one but no effect.
    I cannot make the working view to not work and vice versa!

    So can anyone tell me how i can specify for a model index a branch color (and apply it to the children...)

    I think this will solves my problem.

    Thanks.

  8. #8
    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: [QT4 M/V] QTreeView::DrawBranches()

    Are you using style sheets or did you adjust palettes?
    J-P Nurmi

  9. #9
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    I don't use stule sheets and i don't want to use them (Color have to be dynamic in the code).
    So as you can see in the code above, i guess i just adjust palettes.
    How can I do using this method?
    Thx

  10. #10
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    jpn: Perhaps you have a suggestion? thanks

  11. #11
    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: [QT4 M/V] QTreeView::DrawBranches()

    The best solution I see is to use a [WIKI]proxy style[/WIKI] and draw QStyle::PE_IndicatorBranch yourself. Various styles draw it differently, there is no unified way to adjust palettes so that it would look good on all platforms. And for example QPlastiqueStyle uses a mixture of colors in the palette (background + text) which makes it even more tedious to try to change the color with palettes.
    J-P Nurmi

  12. #12
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    Ok, many thanks, i'll give it a try and give feedbacks.
    I thought that it was possible to modify pen colour of the painter before calling supermethod QTreeView::drawBranches(), it's simple and qtreeview would be in charge of drawing (like currently)

    If someone knows how to do that stuff, please tell me

    Thanks all.

  13. #13
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    Style seems to be ok.
    I extend CommonStyle to overload branches color.
    But i still not understand why "option->palette.dark().color()" returns the same colour as background (in QWindowsStyle::drawPrimitive)

    But this method has an inconvenient: i cannot control the colour of branches of model indexes independently... Any idea ?

  14. #14
    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: [QT4 M/V] QTreeView::DrawBranches()

    Quote Originally Posted by lunatike View Post
    I thought that it was possible to modify pen colour of the painter before calling supermethod QTreeView::drawBranches(), it's simple and qtreeview would be in charge of drawing (like currently)

    If someone knows how to do that stuff, please tell me
    It's just that the QTreeView doesn't draw the branch indicator itself but passes required information to the current style to draw it. It's up to the style to decide the color... Anyway, QCommonStyle uses the pen color directly so you could even do it like this:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. // NOTE: you may use the ProxyStyle is available at Qt Centre wiki but make it inherit QCommonStyle instead of QStyle
    4. #include "proxystyle.h"
    5.  
    6. class MyProxyStyle : public ProxyStyle
    7. {
    8. public:
    9. explicit MyProxyStyle(const QString& baseStyle) : ProxyStyle(baseStyle)
    10. {
    11. }
    12.  
    13. void drawPrimitive(PrimitiveElement element, const QStyleOption* option, QPainter* painter, const QWidget* widget = 0) const
    14. {
    15. if (element == QStyle::PE_IndicatorBranch)
    16. {
    17. painter->save();
    18. painter->setPen(QPen(Qt::blue, 1, Qt::DashLine));
    19. QCommonStyle::drawPrimitive(element, option, painter, widget);
    20. painter->restore();
    21. }
    22. ProxyStyle::drawPrimitive(element, option, painter, widget);
    23. }
    24. };
    25.  
    26. int main(int argc, char* argv[])
    27. {
    28. QApplication app(argc, argv);
    29. tree.setStyle(new MyProxyStyle(app.style()->objectName()));
    30. for (int i = 0; i < 10; ++i)
    31. new QTreeWidgetItem(&tree, QStringList() << QString::number(i));
    32. tree.show();
    33. return app.exec();
    34. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  15. #15
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    It's what i did and it worked almost fine!
    In fact it remains a problem:
    When painting item on a modelindex, all the branches of the row of the model index are painted too.
    And it's a bad behaviour.

    I'm trying to resolve it and will be back !

    Thanks for all your efforts!

  16. #16
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    Just a little question : which version of windows have you ? XP or Seven ?

  17. #17
    Join Date
    Jun 2008
    Posts
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    in 2008? must be XP

  18. #18
    Join Date
    Apr 2011
    Posts
    8
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: [QT4 M/V] QTreeView::DrawBranches()

    Quote Originally Posted by lunatike View Post
    in 2008? must be XP
    Yes ... lol

Similar Threads

  1. [QT4 & XP] QTreeView issue with Designer form
    By incapacitant in forum Newbie
    Replies: 3
    Last Post: 2nd March 2006, 18:42
  2. [QT4 & XP] doubleClick on QtreeView
    By incapacitant in forum Newbie
    Replies: 2
    Last Post: 2nd March 2006, 13:15
  3. [QT4 & XP] connect on QtreeView
    By incapacitant in forum Newbie
    Replies: 1
    Last Post: 2nd March 2006, 12:08

Tags for this Thread

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.