[Solved] QTreeWidget add separator between items



  • hi , i need a tree widget like this one on uTorrent application , i need to add separator between items
    how i can create this ?
    !http://s1.postimage.org/u1kdgl8y7/tree.png(treeWidget)!

    ty



  • bq. i need to add separator between items

    The image looks like a QToolBox with QListWidgets/QListView, I think. But also looks like QTreeWidget/QTreeView.

    Sorry but I am not a user of uTorrent so it was based on what I see.



  • I would say that's more like multiple QTreeWidget in a QVBoxLayout (at least easier to do)

    @Code_ReaQtor: Also true, but without visible sections.



  • To me, it seems you can easily create this by implementing your own delegate to style your top-level items in your tree.



  • [quote author="Andre" date="1359032142"]To me, it seems you can easily create this by implementing your own delegate to style your top-level items in your tree. [/quote]
    i dont know what you mean , can u explain more ?

    [quote author="Adrien Leravat" date="1359032023"]I would say that's more like multiple QTreeWidget in a QVBoxLayout (at least easier to do)

    @Code_ReaQtor: Also true, but without visible sections.[/quote]

    yes create this but how i can manage selected items and click events on them ?

    ty for your answers



  • As QTreeWidget is based on QTreeView (A view of the Model-View-Delegate implemented in Qt), you can use the selectionModel method on you QTreeWidget to retrieve the instance of QItemSelectionModel used.

    The "QItemSelectionModel ":http://doc.qt.digia.com/qt/qitemselectionmodel.html#signals instance returned have signals that are emitted when selection change (i.e. user selected an item in you QTreeWidget).

    Connect a slot to the wanted signal emitted by QItemSelectionModel to handle them.



  • Take a look at [[doc:QStyledItemDelegate]]. You can subclass it and implement your own rendering for top-level items by reimplementing sizeHint and paint. The index that you get can be easily queried to see if you're dealing with a top-level item (those items have an invalid parent()) and if the item is not the first item (check against row 0).

    For all top level items (except the one at row 0), you simply return a bigger size hint to make space for the line, and in the paint event, you render the line and under that the item itself, re-using the existing implementation for that. For all other items, you simply return whatever the base class implementation does.



  • [quote author="Andre" date="1359035280"]Take a look at [[doc:QStyledItemDelegate]]. You can subclass it and implement your own rendering for top-level items by reimplementing sizeHint and paint. The index that you get can be easily queried to see if you're dealing with a top-level item (those items have an invalid parent()) and if the item is not the first item (check against row 0).

    For all top level items (except the one at row 0), you simply return a bigger size hint to make space for the line, and in the paint event, you render the line and under that the item itself, re-using the existing implementation for that. For all other items, you simply return whatever the base class implementation does. [/quote]

    ty its a good way , but i cant make space between items by size Hint because it make item bigger , look at picture you will know what i mean
    how i can find where painter must draw item ? for drawing line ...
    !http://ma3ta.persiangig.com/pic/2.png(size hint problem)!



  • That the item becomes bigger is good: you need the space to draw your line. You are not really making space between the items this way, you're just extending the item and making it look like you inserted a separator line.



  • [quote author="Andre" date="1359110800"]That the item becomes bigger is good: you need the space to draw your line. You are not really making space between the items this way, you're just extending the item and making it look like you inserted a separator line. [/quote]

    yes i know bigger item is good , but problem is this border of item , how can i keep border and item shape like normal items ? and how i can get that item X and Y to draw a line ?



  • What did you try already? You're not expecting a ready-made solution I hope?



  • [quote author="Andre" date="1359117433"]What did you try already? You're not expecting a ready-made solution I hope?[/quote]

    ty you Andre for your help , i have done this by reimplementing sizeHint and paint and change option.rect
    its a pic of result
    !http://ma3ta.persiangig.com/pic/tree2.png(result)!



  • Looks fine to me. Any issues left?



  • [quote author="Andre" date="1359363623"]Looks fine to me. Any issues left? [/quote]

    yes i have one more problem , for each item text and icon location is fine but i want to draw border from x=0 , like this red border in this picture , maybe i can draw item from x=0 and add padding-left to item for icon and text but i do not support this .. , i just must change in paint of this item
    any better idea ?

    !http://ma3ta.persiangig.com/pic/tree3.png(border)!

    ty ..



  • I don't think the standard highlights are rendered there either in a tree view. You might have to tweak the rect of the option you get, but I did not try this myself.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.