Results 1 to 7 of 7

Thread: Draggable QActions in Qt

  1. #1
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Draggable QActions in Qt

    Hi All Experts ,
    I am developing an application in which i want a QMenu in which the actions are draggable.
    Is there any standard Flag that can be set to the QMenu or QAction .

    I have developed a Custom QMenu, in which i have implemented the drag and drop Events.
    But the problem is that .. the action is getting added to the end , this happens only when the beforeAction is 0.

    Qt Code:
    1. class CustomMenu : public QMenu
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CustomMenu(QWidget *parent = 0);
    7. bool addActionBool;
    8. bool check;
    9. QAction *beforeAction;
    10. QAction *afterAction;
    11. QAction *action;
    12. QPoint startPos;
    13. int i;
    14. protected:
    15. void mousePressEvent(QMouseEvent *event);
    16. void mouseMoveEvent(QMouseEvent *event);
    17. void dragEnterEvent(QDragEnterEvent *event);
    18. void dragMoveEvent(QDragMoveEvent *event);
    19. void dropEvent(QDropEvent *event);
    20.  
    21. private:
    22. void performDrag();
    23. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. CustomMenu::CustomMenu(QWidget *parent):QMenu(parent)
    2. {
    3. setAcceptDrops(true);
    4. //action = new QAction(this);
    5. }
    6.  
    7. void CustomMenu::mousePressEvent(QMouseEvent *event)
    8. {
    9. if (event->button() == Qt::LeftButton)
    10. {
    11. startPos = event->pos();
    12. action = this->activeAction(); // getting the current active action
    13. }
    14. QMenu::mousePressEvent(event);
    15. }
    16.  
    17. void CustomMenu::mouseMoveEvent(QMouseEvent *event)
    18. {
    19. if (event->buttons() & Qt::LeftButton) {
    20. int distance = (event->pos() - startPos).manhattanLength();
    21. if (distance >= QApplication::startDragDistance())
    22. {
    23. performDrag();
    24. }
    25. }
    26. QMenu::mouseMoveEvent(event);
    27. }
    28.  
    29. void CustomMenu::performDrag()
    30. {
    31. check = action->isChecked();
    32. if (action) {
    33. QMimeData *mimeData = new QMimeData;
    34. mimeData->setText(action->text());
    35.  
    36. QDrag *drag = new QDrag(this);
    37. drag->setMimeData(mimeData);
    38. if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
    39. /*delete action;*/removeAction(action);
    40. }
    41. }
    42.  
    43. void CustomMenu::dragEnterEvent(QDragEnterEvent *event)
    44. {
    45. event->setDropAction(Qt::MoveAction);
    46. event->accept();
    47. }
    48.  
    49. void CustomMenu::dragMoveEvent(QDragMoveEvent *event)
    50. {
    51. event->setDropAction(Qt::MoveAction);
    52. event->accept();
    53. }
    54.  
    55. void CustomMenu::dropEvent(QDropEvent *event)
    56. {
    57. QAction *newAction = new QAction(event->mimeData()->text(),this);
    58. newAction->setCheckable(true);
    59. newAction->setChecked(check);
    60. beforeAction = actionAt(QCursor::pos());
    61. insertAction(beforeAction,newAction); //It appends action if before is 0 or before is not a valid action for this widget. beforeAction is 0 here.
    62. event->setDropAction(Qt::MoveAction);
    63. event->accept();
    64. }
    To copy to clipboard, switch view to plain text mode 

    where i am going wrong ??
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Draggable QActions in Qt

    Why ar you creating a new action in the drop event instead of reusing the original one? Are you sure beforeAction points to a valid action (can you retrieve its name) after using actionAt? To me it would seem more appropriate to use the position from the drop event and not from QCursor::pos().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draggable QActions in Qt

    Hello Sir,
    beforeAction always comes out to be 0.
    Not able to access it.
    How to get the action which has the mouse focus ? I have used actionAt , but its not working.
    I am struck here & I will reuse the original action instead of newAction.
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Draggable QActions in Qt

    Quote Originally Posted by vajindarladdad View Post
    Hello Sir,
    beforeAction always comes out to be 0.
    Not able to access it.
    Probably because of misuse of QCursor::pos()

    How to get the action which has the mouse focus ? I have used actionAt , but its not working.
    The way you did it was incorrect. Don't use QCursor::pos() which is unreliable and returns the position in screen coordinates, not widget coordinates.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draggable QActions in Qt

    Sir ,
    Please give me a hint to access the QAction based on the mouse position.
    is there any other function to access it.
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Draggable QActions in Qt

    Probably QDropEvent::pos() and QMenu::actionAt() but I think I have already said that...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    vajindarladdad (14th August 2009)

  8. #7
    Join Date
    Dec 2008
    Location
    PUNE (INDIA)
    Posts
    49
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draggable QActions in Qt

    Thanks a million ,
    Thanks & Regards ,

    Vajindar Laddad .
    Trainee Developer.
    (INDIA).
    91+9325014248

Similar Threads

  1. Replies: 1
    Last Post: 18th December 2008, 10:25
  2. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 01:04
  3. about QAction's slot
    By Pang in forum Qt Programming
    Replies: 2
    Last Post: 4th June 2007, 07:48
  4. Problems with QActions and Pixmaps
    By ToddAtWSU in forum Qt Programming
    Replies: 8
    Last Post: 11th April 2007, 21:53
  5. Enabling/Disabling QActions in QMenus
    By rshabsin in forum Qt Programming
    Replies: 3
    Last Post: 15th February 2006, 22:13

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.