Nominate our 2022 Qt Champions!

Drop between items in QTreeWidget

  • My QTreeWidget is created like in a code below:

    treeWidget = new QTreeWidget(centralWidget);

    Sometimes when i drag item and drop it between 2 other items it becomes top level insetd of beeing inserted where it supposed to. It happens only if VerticalScrollMode is QAbstractItemView::ScrollPerPixel and if item is dropped at red line ( on screenshot). Is there any way to avoid this behaviour?
    alt text

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You may have unearthed something. However, do you really need ScrollPerPixel ?

  • With ScrollPerItem it scrolls too "jumpy". Anyway, I looked at sources and I think the problem is in

    int QTreeViewPrivate::itemAtCoordinate	(	int 	coordinate	 ) 	const

    There is if statement:

    if (verticalScrollMode == QAbstractItemView::ScrollPerPixel) {
             if (uniformRowHeights) {
                 const int viewItemIndex = (coordinate + q->verticalScrollBar()->value()) / defaultItemHeight;
                 return (viewItemIndex >= itemCount ? -1 : viewItemIndex);
             // ### optimize
             int viewItemCoordinate = 0;
             const int contentsCoordinate = coordinate + q->verticalScrollBar()->value();
             for (int viewItemIndex = 0; viewItemIndex < viewItems.count(); ++viewItemIndex) {
                 viewItemCoordinate += itemHeight(viewItemIndex);
                 if (viewItemCoordinate >= contentsCoordinate)  //here
                     return (viewItemIndex >= itemCount ? -1 : viewItemIndex);

    And line

    if (viewItemCoordinate >= contentsCoordinate) 

    should be like

    if (viewItemCoordinate > contentsCoordinate) 

    So, if uniformRowHeight is true then everything works fine.


  • Lifetime Qt Champion

    Good catch. You should check the bug report system to see if there's already something related. If not then you should open a report with your findings (and a minimal compilable example).

Log in to reply