Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Drop between items in QTreeWidget

    General and Desktop
    2
    4
    759
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      Pete Sassafras last edited by

      My QTreeWidget is created like in a code below:

      treeWidget = new QTreeWidget(centralWidget);
      treeWidget->setDragDropMode(QAbstractItemView::DragDrop);
      treeWidget->setDefaultDropAction(Qt::MoveAction);
      treeWidget->setVerticalScrollMode(QAbstractItemView::ScrollPerPixel);
      

      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

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi and welcome to devnet,

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

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply Reply Quote 1
        • P
          Pete Sassafras last edited by

          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.

          treeWidget->setUniformRowHeights(true);
          
          1 Reply Last reply Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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).

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply Reply Quote 1
            • First post
              Last post