Results 1 to 2 of 2

Thread: How to make an icon widget draggable

  1. #1
    Join Date
    Nov 2010
    Posts
    4
    Qt products
    Qt4 Qt/Embedded

    Question How to make an icon widget draggable

    Hello everybody,

    I have an icon widget, and I want to make this widget be draggable which means when user press the mouse on this widget and drag it, this icon shall be moved under the mouse and until the mouse was release.

    I was trying to use the drag and drop mechanism to implement this feature, only to find that it can't give me what I need. Please see my code in the following section,

    Code:

    void DragWidget::dragMoveEvent(QDragMoveEvent *event)
    {
    if (event->mimeData()->hasFormat("application/x-dnditemdata")) {

    QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
    QDataStream dataStream(&itemData, QIODevice::ReadOnly);

    QPixmap pixmap;
    QPoint offset;
    dataStream >> pixmap >> offset;

    //QLabel *newIcon = new QLabel(this);
    //newIcon->setPixmap(pixmap);
    //repaint();
    //newIcon->show();
    //newIcon->setAttribute(Qt::WA_DeleteOnClose);

    if (event->source() == this) {
    event->setDropAction(Qt::MoveAction);
    event->accept();
    } else {
    event->acceptProposedAction();
    }

    move(event->pos() - offset);
    }
    else {
    event->ignore();
    }
    }

    void DragWidget::dropEvent(QDropEvent *event)
    {
    if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
    QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
    QDataStream dataStream(&itemData, QIODevice::ReadOnly);

    QPixmap pixmap;
    QPoint offset;
    dataStream >> pixmap >> offset;

    QLabel *newIcon = new QLabel(this);
    newIcon->setPixmap(pixmap);
    newIcon->move(event->pos() - offset);
    newIcon->show();
    newIcon->setAttribute(Qt::WA_DeleteOnClose);

    if (event->source() == this) {
    event->setDropAction(Qt::MoveAction);
    event->accept();
    } else {
    event->acceptProposedAction();
    }
    } else {
    event->ignore();
    }
    }


    Can someone help me out?

  2. #2
    Join Date
    Nov 2010
    Posts
    4
    Qt products
    Qt4 Qt/Embedded

    Default Re: How to make an icon widget draggable

    The following method may work, I will have a try.


    QGraphicsItem::ItemIsMovable

    0x1 The item supports interactive movement using the mouse. By clicking on the item and then dragging, the item will move together with the mouse cursor. If the item has children, all children are also moved. If the item is part of a selection, all selected items are also moved. This feature is provided as a convenience through the base implementation of QGraphicsItem's mouse event handlers.

Similar Threads

  1. How to make widget grow outside of its parent widget
    By shakeandbake in forum Qt Programming
    Replies: 2
    Last Post: 9th January 2010, 08:52
  2. Replies: 3
    Last Post: 15th October 2009, 11:41
  3. Draggable QActions in Qt
    By vajindarladdad in forum Newbie
    Replies: 6
    Last Post: 14th August 2009, 11:15
  4. Draggable Button
    By blueescort in forum Qt Programming
    Replies: 4
    Last Post: 6th November 2008, 07:36
  5. how can I make the Icon larger
    By duduqq in forum Qt Programming
    Replies: 1
    Last Post: 8th July 2008, 02:28

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
  •  
Qt is a trademark of The Qt Company.