Results 1 to 11 of 11

Thread: The drag and moving problem??

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2006
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default The drag and moving problem??

    I am planning to build a tool bar like Dr.eye,when using the Qt's example D&D ,I set set the program like this:

    Qt Code:
    1. MainWidget::MainWidget(QWidget *parent) : QLabel(parent)
    2. {
    3. setWindowState(this->windowState() ^ Qt::WindowFullScreen);
    4. setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
    5. setAcceptDrops(true);
    6.  
    7. toolbar = new ToolWidget(this);
    8. toolbar->move(50, 50);
    9. toolbar->show();
    10. toolbar->setAttribute(Qt::WA_DeleteOnClose);
    11. }
    12.  
    13. void MainWidget::dragEnterEvent(QDragEnterEvent *event)
    14. {
    15. if (event->mimeData()->hasFormat("MyToolBar"))
    16. {
    17. if (event->source() == this)
    18. {
    19. event->setDropAction(Qt::MoveAction);
    20. event->accept();
    21. } else
    22. {
    23. event->acceptProposedAction();
    24. }
    25. }
    26. else
    27. {
    28. event->ignore();
    29. }
    30. }
    31.  
    32. void MainWidget::dragMoveEvent(QDragMoveEvent *event)
    33. {
    34. if (event->mimeData()->hasFormat("MyToolBar"))
    35. {
    36. if (children().contains(event->source()))
    37. {
    38. event->setDropAction(Qt::MoveAction);
    39. event->accept();
    40. }
    41. else
    42. {
    43. event->acceptProposedAction();
    44. }
    45. printf("this is the test\n");
    46. }
    47. else
    48. {
    49. event->ignore();
    50. }
    51. }
    52.  
    53. void MainWidget::dropEvent(QDropEvent *event)
    54. {
    55. if (event->mimeData()->hasFormat("MyToolBar"))
    56. {
    57. QByteArray itemData = event->mimeData()->data("MyToolBar");
    58. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
    59.  
    60. QPixmap pixmap;
    61. QPoint offset;
    62. dataStream >> pixmap >> offset;
    63.  
    64. ToolWidget *Transfer = new ToolWidget(this);
    65. Transfer->setPixmap(pixmap);
    66. Transfer->move(event->pos() - offset);
    67. Transfer->show();
    68. Transfer->setAttribute(Qt::WA_DeleteOnClose);
    69.  
    70. if (event->source() == this)
    71. {
    72. event->setDropAction(Qt::MoveAction);
    73. event->accept();
    74. } else {
    75. event->acceptProposedAction();
    76. }
    77. }
    78. else
    79. {
    80. event->ignore();
    81. }
    82. }
    83.  
    84. void MainWidget::mousePressEvent(QMouseEvent *event)
    85. {
    86. if(Qt::LeftButton == event->button ())
    87. {
    88. ToolWidget *child = static_cast<ToolWidget*>(childAt(event->pos()));
    89. if (!child)
    90. return;
    91.  
    92. QPixmap pixmap = *child->pixmap(); //get the pixmap on the label
    93.  
    94.  
    95. QByteArray itemData;
    96. QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    97. dataStream << pixmap << QPoint(event->pos() - child->pos());
    98.  
    99. QMimeData *mimeData = new QMimeData;
    100. mimeData->setData("MyToolBar", itemData);
    101. child->hide();
    102.  
    103. QDrag *drag = new QDrag(this);
    104. drag->setMimeData(mimeData);
    105. drag->setPixmap(pixmap);
    106. drag->setHotSpot(event->pos() - child->pos());
    107.  
    108. if (drag->start(Qt::MoveAction) == Qt::MoveAction)
    109. child->close();
    110. else
    111. child->show();
    112. }
    113. }
    To copy to clipboard, switch view to plain text mode 

    this is following the example,but I create a class ToolWidget,
    it contains several button's。
    the question is,when i drag and move the toolbar, all of the object except toolbar'r 's icon
    are invisiable
    is it possible make the label visible,and how to do it??
    Last edited by wysota; 26th September 2006 at 11:47. Reason: missing [code] tags

Similar Threads

  1. Drag & drop with model/view (Qt4)
    By yogeshm02 in forum Qt Programming
    Replies: 16
    Last Post: 19th September 2011, 20:36
  2. Drag and drop outside the application
    By jpn in forum Newbie
    Replies: 7
    Last Post: 27th August 2006, 15:37

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.