Highlight items of treeview during Drag n Drop



  • Hi,

    i have tried to highlight the item I'm currently hover when moving the mouse during a DnD.

    By the way, it doesn't work. It doesn't work during the Drag'Drop but only after when no more DnD but just mouse move. I just want the highlight when DnD and not before or after

    I have enable the highlight by:

    @void TreeView::dragMoveEvent(QDragMoveEvent * event)
    {
    qDebug() << "On Drag Move Event";
    const QMimeData* mimeData = event->mimeData();

    event->setDropAction(Qt::CopyAction);
    
    if (mimeData->hasUrls())
    {
        QTreeWidgetItem *item = itemAt(event->pos());
        if(item) {
            qDebug() << "itemat: " << item->text(0);
    

    #if 0
    setStyleSheet(QString::fromUtf8("QTreeWidget::item:hover {\n"
    "background-color: rgb(123, 45, 67);\n"
    "}"));
    #endif
    }
    event->setDropAction(Qt::CopyAction);
    event->acceptProposedAction();
    }
    else
    event->ignore();

    // dropSite = event->answerRect();
    // event->acceptProposedAction();
    }@



  • I would suggest you use your own Delegate and handle the styling in its paint() method.



  • I tried to implement something with :

    @void TreeView::paintEvent ( QPaintEvent* event )
    {
    QTreeView::paintEvent (event);
    QPainter painter ( viewport() );
    int x, y, w, h;
    dropSite.getRect ( &x, &y, &w, &h );
    QPoint point(x,y);
    QModelIndex modidx = indexAt ( point );
    QRect arect = visualRect ( modidx );
    int b = arect.y();
    QBrush brush(Qt::black, Qt::Dense4Pattern);
    QPen pen;
    pen.setWidth(2);
    pen.setBrush(brush);
    painter.setPen(pen);
    painter.drawLine ( 0, b, width(), b );
    event->accept();
    }@

    but it's not really working fine. if you can point me to an example using QtreeWidget it will be perfect.


Log in to reply
 

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