一生一世学坛

 找回密码
 立即注册
搜索
查看: 4811|回复: 0
打印 上一主题 下一主题

QT实现两个窗口之间拖拽数据

[复制链接]

334

主题

385

帖子

6830

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
6830
跳转到指定楼层
楼主
发表于 2021-3-11 16:14:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
主程序:该程序演示,左右两个窗口可以互相拖动数据:文本数据的传递可以用下面的方法:
event->mimeData()->text();//取
mimeData->setText();//放

拖动时获取当前窗口的图像:QPixmap pixMap = QPixmap::grabWidget(this);


  1. #include <QApplication>
  2. #include <QHBoxLayout>

  3. #include "dragwidget.h"

  4. int main(int argc, char *argv[])
  5. {
  6.     Q_INIT_RESOURCE(draggableicons);

  7.     QApplication app(argc, argv);

  8.     QWidget mainWidget;
  9.     QHBoxLayout *horizontalLayout = new QHBoxLayout(&mainWidget);
  10.     horizontalLayout->addWidget(new DragWidget);
  11.     horizontalLayout->addWidget(new DragWidget);

  12.     mainWidget.setWindowTitle(QObject::tr("Draggable Icons"));
  13.     mainWidget.show();

  14.     return app.exec();
  15. }
复制代码
支持拖出拖入的窗口:
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the examples of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:BSD$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** BSD License Usage
  18. ** Alternatively, you may use this file under the terms of the BSD license
  19. ** as follows:
  20. **
  21. ** "Redistribution and use in source and binary forms, with or without
  22. ** modification, are permitted provided that the following conditions are
  23. ** met:
  24. **   * Redistributions of source code must retain the above copyright
  25. **     notice, this list of conditions and the following disclaimer.
  26. **   * Redistributions in binary form must reproduce the above copyright
  27. **     notice, this list of conditions and the following disclaimer in
  28. **     the documentation and/or other materials provided with the
  29. **     distribution.
  30. **   * Neither the name of The Qt Company Ltd nor the names of its
  31. **     contributors may be used to endorse or promote products derived
  32. **     from this software without specific prior written permission.
  33. **
  34. **
  35. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  46. **
  47. ** $QT_END_LICENSE$
  48. **
  49. ****************************************************************************/

  50. #ifndef DRAGWIDGET_H
  51. #define DRAGWIDGET_H

  52. #include <QFrame>

  53. QT_BEGIN_NAMESPACE
  54. class QDragEnterEvent;
  55. class QDropEvent;
  56. QT_END_NAMESPACE

  57. //! [0]
  58. class DragWidget : public QFrame
  59. {
  60. public:
  61.     explicit DragWidget(QWidget *parent = nullptr);

  62. protected:
  63.     void dragEnterEvent(QDragEnterEvent *event) override;
  64.     void dragMoveEvent(QDragMoveEvent *event) override;
  65.     void dropEvent(QDropEvent *event) override;
  66.     void mousePressEvent(QMouseEvent *event) override;
  67. };
  68. //! [0]

  69. #endif // DRAGWIDGET_H
  70. Cpp文件:
  71. /****************************************************************************
  72. **
  73. ** Copyright (C) 2016 The Qt Company Ltd.
  74. ** Contact: https://www.qt.io/licensing/
  75. **
  76. ** This file is part of the examples of the Qt Toolkit.
  77. **
  78. ** $QT_BEGIN_LICENSE:BSD$
  79. ** Commercial License Usage
  80. ** Licensees holding valid commercial Qt licenses may use this file in
  81. ** accordance with the commercial license agreement provided with the
  82. ** Software or, alternatively, in accordance with the terms contained in
  83. ** a written agreement between you and The Qt Company. For licensing terms
  84. ** and conditions see https://www.qt.io/terms-conditions. For further
  85. ** information use the contact form at https://www.qt.io/contact-us.
  86. **
  87. ** BSD License Usage
  88. ** Alternatively, you may use this file under the terms of the BSD license
  89. ** as follows:
  90. **
  91. ** "Redistribution and use in source and binary forms, with or without
  92. ** modification, are permitted provided that the following conditions are
  93. ** met:
  94. **   * Redistributions of source code must retain the above copyright
  95. **     notice, this list of conditions and the following disclaimer.
  96. **   * Redistributions in binary form must reproduce the above copyright
  97. **     notice, this list of conditions and the following disclaimer in
  98. **     the documentation and/or other materials provided with the
  99. **     distribution.
  100. **   * Neither the name of The Qt Company Ltd nor the names of its
  101. **     contributors may be used to endorse or promote products derived
  102. **     from this software without specific prior written permission.
  103. **
  104. **
  105. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  106. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  107. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  108. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  109. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  110. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  111. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  112. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  113. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  114. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  115. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  116. **
  117. ** $QT_END_LICENSE$
  118. **
  119. ****************************************************************************/

  120. #include <QtWidgets>

  121. #include "dragwidget.h"
  122. #include <qdebug.h>

  123. //! [0]
  124. DragWidget::DragWidget(QWidget *parent)
  125.     : QFrame(parent)
  126. {
  127.     setMinimumSize(200, 200);
  128.     setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
  129.     setAcceptDrops(true);

  130.     QLabel *boatIcon = new QLabel(this);
  131.     boatIcon->setPixmap(QPixmap(":/images/boat.png"));
  132.     boatIcon->move(10, 10);
  133.     boatIcon->show();
  134.     boatIcon->setAttribute(Qt::WA_DeleteOnClose);

  135.     QLabel *carIcon = new QLabel(this);
  136.     carIcon->setPixmap(QPixmap(":/images/car.png"));
  137.     carIcon->move(100, 10);
  138.     carIcon->show();
  139.     carIcon->setAttribute(Qt::WA_DeleteOnClose);

  140.     QLabel *houseIcon = new QLabel(this);
  141.     houseIcon->setPixmap(QPixmap(":/images/house.png"));
  142.     houseIcon->move(10, 80);
  143.     houseIcon->show();
  144.     houseIcon->setAttribute(Qt::WA_DeleteOnClose);
  145. }
  146. //! [0]

  147. void DragWidget::dragEnterEvent(QDragEnterEvent *event)
  148. {
  149.     if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
  150.         if (event->source() == this) {
  151.             event->setDropAction(Qt::MoveAction);
  152.             event->accept();
  153.         } else {
  154.             event->acceptProposedAction();
  155.         }
  156.     } else {
  157.         event->ignore();
  158.     }
  159. }

  160. void DragWidget::dragMoveEvent(QDragMoveEvent *event)
  161. {
  162.     if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
  163.         if (event->source() == this) {
  164.             event->setDropAction(Qt::MoveAction);
  165.             event->accept();
  166.             qDebug()<<"dragMoveEvent\n";
  167.         } else {
  168.             event->acceptProposedAction();
  169.         }
  170.     } else {
  171.         event->ignore();
  172.     }
  173. }

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

  179.         QPixmap pixmap;
  180.         QPoint offset;
  181.         dataStream >> pixmap >> offset;

  182.         QLabel *newIcon = new QLabel(this);
  183.         newIcon->setPixmap(pixmap);
  184.         newIcon->move(event->pos() - offset);
  185.         newIcon->show();
  186.         newIcon->setAttribute(Qt::WA_DeleteOnClose);

  187.         if (event->source() == this) {
  188.             event->setDropAction(Qt::MoveAction);
  189.             event->accept();
  190.         } else {
  191.             event->acceptProposedAction();
  192.         }
  193.     } else {
  194.         event->ignore();
  195.     }
  196. }

  197. //! [1]
  198. void DragWidget::mousePressEvent(QMouseEvent *event)
  199. {
  200.     QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
  201.     if (!child)
  202.         return;

  203.     QPixmap pixmap = *child->pixmap();

  204.     QByteArray itemData;
  205.     QDataStream dataStream(&itemData, QIODevice::WriteOnly);
  206.     dataStream << pixmap << QPoint(event->pos() - child->pos());
  207. //! [1]

  208. //! [2]
  209.     QMimeData *mimeData = new QMimeData;
  210.     mimeData->setData("application/x-dnditemdata", itemData);
  211.    
  212. //! [2]

  213. //! [3]
  214.     QDrag *drag = new QDrag(this);
  215.     drag->setMimeData(mimeData);
  216.     drag->setPixmap(pixmap);
  217.     drag->setHotSpot(event->pos() - child->pos());
  218. //! [3]

  219.     QPixmap tempPixmap = pixmap;
  220.     QPainter painter;
  221.     painter.begin(&tempPixmap);
  222.     painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
  223.     painter.end();

  224.     child->setPixmap(tempPixmap);

  225.     if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
  226.         child->close();
  227.     } else {
  228.         child->show();
  229.         child->setPixmap(pixmap);
  230.     }
  231. }
复制代码



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|分享学习  

GMT+8, 2024-5-17 10:57 , Processed in 0.044412 second(s), 6 queries , File On.

声明:本站严禁任何人以任何形式发表违法言论!

本站内容由网友原创或转载,如果侵犯了您的合法权益,请及时联系处理!© 2017 zamxqun@163.com

皖公网安备 34010402700634号

皖ICP备17017002号-1

快速回复 返回顶部 返回列表