Results 1 to 2 of 2

Thread: Why two clicks to select an object after drag-n-drop?

  1. #1
    Join Date
    Feb 2012
    Location
    San Francisco, CA
    Posts
    3
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Why two clicks to select an object after drag-n-drop?

    I have a QTreeWidget full of QTreeWidgetItems and I am dragging these items and then dropping them into a QTabWidget. Drag and drop is working perfectly. What is strange is that after I do a drag of one item from the QTreeWidget and drop it, and I go back to drag another item, I have to click *twice* to select the new item. If I do not click twice, but just click on the new item and start dragging (which is what most people do), then all items between the previous dragged item and the new item are selected and dragged over. This is happening on Linux, and I believe our QA team is seeing the same behavior on Windows.

    The drag code is:

    void MyTreeWidget::mousePressEvent(QMouseEvent *event)
    {
    if (event->button() == Qt::LeftButton)
    dragStartPos = event->pos();
    QTreeWidget::mousePressEvent(event); // Pass it on to the parent
    }

    void MyTreeWidget::mouseMoveEvent(QMouseEvent *event)
    {
    if (event->buttons() & Qt::LeftButton) {
    int distance = (event->pos() - dragStartPos).manhattanLength();
    if (distance >= QApplication::startDragDistance()) {
    performDrag();
    }
    }
    QTreeWidget::mouseMoveEvent(event);
    }

    void MyTreeWidget:: performDrag()
    {
    QList<QTreeWidgetItem*> list = selectedItems();
    QString main = tr("Devices: ");
    QTreeWidgetItem *item = 0;
    for (int i=0; i<list.size(); i++) {
    item = list.at(i);
    if (item) {
    QString s = tr("%1 ").arg(item->type());
    main.append(s);
    }
    }
    if (item) {
    QMimeData *mimeData = new QMimeData;
    mimeData->setText(main);
    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(item->icon(0).pixmap(120,120));
    drag->exec(Qt::CopyAction);
    }
    }

    The drop code is:

    void MyTabWidget::dragEnterEvent(QDragEnterEvent *event)
    {
    if (event->mimeData()->hasFormat("text/plain")) {
    event->setDropAction(Qt::CopyAction);
    event->acceptProposedAction();
    }
    }

    void MyTabWidget::dragMoveEvent(QDragMoveEvent *event)
    {
    if (event->mimeData()->hasFormat("text/plain")) {
    //event->setDropAction(Qt::CopyAction); // does not matter if this line is commented out or not
    event->acceptProposedAction();
    }
    }

    void MyTabWidget::dropEvent(QDropEvent *event)
    {
    QString dropData = event->mimeData()->text();
    event->acceptProposedAction();
    ...... handle the dropped data...
    }

    What could I be doing wrong here? Or is this a Qt bug (I doubt it)?

    --abhijit

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Why two clicks to select an object after drag-n-drop?

    Please edit your post and use [CODE] tags. Without them your post it's just a gibberish.

    As to yout problem I think what you're seeing is related to this.
    Try solution I've posted there, should help.

Similar Threads

  1. Drag and Drop an Object Between Two View(QGraphicsView)
    By alizadeh91 in forum Qt Programming
    Replies: 1
    Last Post: 29th February 2012, 11:26
  2. Replies: 0
    Last Post: 7th January 2012, 16:20
  3. Replies: 2
    Last Post: 13th October 2010, 22:51
  4. Replies: 0
    Last Post: 4th May 2010, 11:24
  5. Replies: 2
    Last Post: 27th June 2008, 20:02

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.