当前位置:   article > 正文

QT 拖拽 QListWidget发送,Qgraphicview接收_qmimedata拖拽缩略图

qmimedata拖拽缩略图

Widget类发送

  1. setDragEnabled(true)函数以使该部件能接受拖放事件.

  1. 在widget类重写mousePressEvent()或mouseMoveEvent()函数中启动拖放,记住启动拖放就是调用QDrag对象的exec()函数,在此步把需要拖动的数据保存在QMimeData对象中。

  1. QDrag *drag=new QDrag(this);
  2. QMimeData *mimeData=new QMimeData;
  3. mimeData->setText(currentItem()->text());
  4. mimeData->setData("application/Units.list", encodedData);
  5. drag->setMimeData(mimeData);
  6. drag->exec(Qt::CopyAction|Qt::MoveAction);

//或者是调用setData发送自定义数据

  1. void MyListWidget::mouseMoveEvent(QMouseEvent *event)
  2. {
  3. if (!(event->buttons()&Qt::LeftButton))
  4. return;
  5. if ((event->pos() - m_dragPosition).manhattanLength() < QApplication::startDragDistance())
  6. return;
  7. if (!m_dragItem)
  8. return;
  9. QDrag *drag = new QDrag(this);
  10. QMimeData *mimeData = new QMimeData;
  11. QString strtext = currentItem()->text();
  12. if (strtext.isEmpty())
  13. {
  14. int row = currentRow() - 2;
  15. QByteArray encodedData;
  16. QDir dir1;
  17. QString strdir = dir1.currentPath();
  18. QString strfile = strdir + "/svg/" + m_strList[row];
  19. QDataStream stream(&encodedData, QIODevice::WriteOnly);
  20. {
  21. QPixmap pixmap(strfile);
  22. stream << pixmap;
  23. }
  24. mimeData->setData("draglist", encodedData);
  25. }
  26. else
  27. {
  28. mimeData->setData("draglist", strtext.toUtf8());
  29. }
  30. drag->setMimeData(mimeData);
  31. drag->exec(Qt::CopyAction | Qt::MoveAction);
  32. }

使用场景类接收拖拽

  1. 调用setAcceptDrops(true).使能接收数据。

  1. 主要重写dragEnterEvent、dragMoveEvent、dropEvent。

  1. void dragEnterEvent(QGraphicsSceneDragDropEvent *event) override;
  2. void dragMoveEvent(QGraphicsSceneDragDropEvent *event) override;
  3. void dropEvent(QGraphicsSceneDragDropEvent *event) override;
  1. void MyGraphicsScene::createRect(const QPointF &pt)
  2. {
  3. MyGraphicsRectItem *item=new MyGraphicsRectItem;
  4. item->setPos(pt);
  5. qDebug() << "rect pos:" << pt.x() << pt.y();
  6. item->setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
  7. addItem(item);
  8. }
  9. void MyGraphicsScene::createLine(const QPointF &pt)
  10. {
  11. MyGraphicsLineItem *item=new MyGraphicsLineItem;
  12. item->setPos(pt);
  13. item->setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
  14. addItem(item);
  15. }
  16. void MyGraphicsScene::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
  17. {
  18. if(event->mimeData()->hasFormat("text/plain")){
  19. event->acceptProposedAction();
  20. }
  21. }
  22. void MyGraphicsScene::dropEvent(QGraphicsSceneDragDropEvent *event)
  23. {
  24. // QGraphicsScene::dropEvent(event);
  25. if (event->mimeData()->hasFormat("text/plain"))
  26. {
  27. QString text=event->mimeData()->text();
  28. if(text==tr("直线"))
  29. {
  30. createLine(event->scenePos());
  31. }
  32. else if(text==tr("矩形"))
  33. {
  34. createRect(event->scenePos());
  35. }
  36. else{
  37. }
  38. event->acceptProposedAction();
  39. }
  40. }
  41. void MyGraphicsScene::dragMoveEvent(QGraphicsSceneDragDropEvent *event)
  42. {
  43. event->accept();
  44. }

测试发现:item在视图存放的位置有些问题?前面位置没法固定,添加多了就ok.

使用视图类来处理拖拽的接收(推荐使用这种办法)

使用视图类可以保证item在视图存放的位置就是鼠标的位置。

  1. 场景类就不用重写dragEnterEvent、dragMoveEvent、dropEvent。

  1. 视图类调用setAcceptDrops(true)允许接收。

  1. 视图类重写dragEnterEvent、dragMoveEvent、dropEvent。

  1. #ifndef MYGRAPHICSVIEW_H
  2. #define MYGRAPHICSVIEW_H
  3. #include <QObject>
  4. #include <QGraphicsView>
  5. class MyGraphicsView : public QGraphicsView
  6. {
  7. public:
  8. MyGraphicsView(QWidget *parent=0);
  9. ~MyGraphicsView();
  10. protected:
  11. void dragEnterEvent(QDragEnterEvent *event);
  12. void dragMoveEvent(QDragMoveEvent *event) ;
  13. void dropEvent(QDropEvent *event);
  14. };
  15. #endif // MYGRAPHICSVIEW_H
  16. #include "mygraphicsview.h"
  17. #include "mygraphicsscene.h"
  18. #include <QDragEnterEvent>
  19. #include <QMimeData>
  20. #include <QDebug>
  21. #include <QMouseEvent>
  22. #include <QPoint>
  23. #if _MSC_VER >=1600
  24. #pragma execution_character_set("utf-8")
  25. #endif
  26. MyGraphicsView::MyGraphicsView(QWidget *parent):
  27. QGraphicsView(parent)
  28. {
  29. }
  30. MyGraphicsView::~MyGraphicsView()
  31. {
  32. }
  33. void MyGraphicsView::dragEnterEvent(QDragEnterEvent *event)
  34. {
  35. if(event->mimeData()->hasFormat("text/plain")){
  36. event->acceptProposedAction();
  37. qDebug() << "view dragEnterEvent";
  38. }
  39. QGraphicsView::dragEnterEvent(event);
  40. }
  41. void MyGraphicsView::dragMoveEvent(QDragMoveEvent *event)
  42. {
  43. qDebug() << "view dragMoveEvent";
  44. event->acceptProposedAction();
  45. }
  46. void MyGraphicsView::dropEvent(QDropEvent *event)
  47. {
  48. qDebug() << "view dropevent";
  49. if (event->mimeData()->hasFormat("draglist"))
  50. {
  51. QByteArray itemData = event->mimeData()->data("draglist");
  52. QString strText = itemData;
  53. MyGraphicsScene *scene=(MyGraphicsScene*)this->scene();
  54. QPoint po = event->pos();
  55. qDebug() << "dropevent pos:"<< po.x() << po.y() ;
  56. if (strText == "直线")
  57. {
  58. scene->createLine(mapToScene(event->pos()));
  59. }
  60. else if (strText == "矩形")
  61. {
  62. scene->createRect(mapToScene(event->pos()));
  63. }
  64. else
  65. {
  66. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
  67. QPixmap pixmap;
  68. dataStream >> pixmap;
  69. scene->createPixmap(mapToScene(event->pos()), pixmap);
  70. }
  71. event->acceptProposedAction();
  72. }
  73. QGraphicsView::dropEvent(event);
  74. }
  75. Widget::Widget(QWidget *parent) :
  76. QWidget(parent),
  77. ui(new Ui::Widget)
  78. {
  79. ui->setupUi(this);
  80. QStringList list1;
  81. list1 <<"直线" <<"矩形";
  82. MyListWidget *listWidget1 = new MyListWidget(this);
  83. listWidget1->addItems(list1);
  84. listWidget1->setIconSize(QSize(200,200));
  85. listWidget1->setGeometry(100,150,300,200);
  86. listWidget1->setDragEnabled(true);
  87. m_scene = new MyGraphicsScene();
  88. m_scene->setSceneRect(QRectF(-300,-200,600,400));
  89. m_view = new MyGraphicsView(this);
  90. m_view->setScene(m_scene);
  91. m_view->setGeometry(500,100,400,600);
  92. m_view->setAcceptDrops(true);
  93. }

一般的应用:QListWidget控件发送拖拽,Qgraphicview接收。

发送

  1. #ifndef MYLISTWIDGET_H
  2. #define MYLISTWIDGET_H
  3. #include <QObject>
  4. #include <QListWidget>
  5. class MyListWidget:public QListWidget
  6. {
  7. Q_OBJECT
  8. public:
  9. MyListWidget(QWidget *parent=0);
  10. ~MyListWidget();
  11. protected:
  12. void mouseMoveEvent(QMouseEvent *event) override;
  13. void mousePressEvent(QMouseEvent *event) override;
  14. private:
  15. QPoint m_dragPosition; // 拖放起点
  16. QListWidgetItem *m_dragItem; // 被拖放的item
  17. public:
  18. QStringList m_strList;
  19. };
  20. #endif // MYLISTWIDGET_H
  21. #include "mylistwidget.h"
  22. #include <QMouseEvent>
  23. #include <QListWidgetItem>
  24. #include <QDrag>
  25. #include <QApplication>
  26. #include <QMimeData>
  27. #include <QPainter>
  28. #include <QPixmap>
  29. MyListWidget::MyListWidget(QWidget *parent):
  30. QListWidget(parent)
  31. {
  32. }
  33. MyListWidget::~MyListWidget(){
  34. }
  35. void MyListWidget::mouseMoveEvent(QMouseEvent *event)
  36. {
  37. if (!(event->buttons()&Qt::LeftButton))
  38. return;
  39. if ((event->pos() - m_dragPosition).manhattanLength() < QApplication::startDragDistance())
  40. return;
  41. if (!m_dragItem)
  42. return;
  43. QDrag *drag = new QDrag(this);
  44. QMimeData *mimeData = new QMimeData;
  45. QString strtext = currentItem()->text();
  46. if (strtext.isEmpty())
  47. {
  48. int row = currentRow() - 2;
  49. QByteArray encodedData;
  50. QDir dir1;
  51. QString strdir = dir1.currentPath();
  52. QString strfile = strdir + "/svg/" + m_strList[row];
  53. QDataStream stream(&encodedData, QIODevice::WriteOnly);
  54. {
  55. QPixmap pixmap(strfile);
  56. stream << pixmap;
  57. }
  58. mimeData->setData("draglist", encodedData);
  59. }
  60. else
  61. {
  62. mimeData->setData("draglist", strtext.toUtf8());
  63. }
  64. drag->setMimeData(mimeData);
  65. drag->exec(Qt::CopyAction | Qt::MoveAction);
  66. }
  67. void MyListWidget::mousePressEvent(QMouseEvent *event){
  68. m_dragPosition=event->pos();
  69. m_dragItem=this->itemAt(event->pos());
  70. //保持父类继承下来的按键行为
  71. QListWidget::mousePressEvent(event);
  72. }

接收

  1. #ifndef MYGRAPHICSSCENE_H
  2. #define MYGRAPHICSSCENE_H
  3. #include <QObject>
  4. #include <QGraphicsView>
  5. class MyGraphicsScene : public QGraphicsScene
  6. {
  7. Q_OBJECT
  8. public:
  9. MyGraphicsScene(QWidget *parent=0);
  10. ~MyGraphicsScene();
  11. void createRect(const QPointF &pt);
  12. void createLine(const QPointF &pt);
  13. void createPixmap(const QPointF &pt, const QPixmap &pixmap);
  14. private:
  15. QGraphicsScene *m_scene;
  16. };
  17. #endif // MYGRAPHICSSCENE_H
  18. #include "mygraphicsscene.h"
  19. #include <QDebug>
  20. #include <QMimeData>
  21. #include <QGraphicsSceneDragDropEvent>
  22. #include <QGraphicsLineItem>
  23. #include <QGraphicsRectItem>
  24. #include <QGraphicsScene>
  25. #include <QPen>
  26. #include "mygraphicslineitem.h"
  27. #include "mygraphicsrectitem.h"
  28. #if _MSC_VER >=1600
  29. #pragma execution_character_set("utf-8")
  30. #endif
  31. MyGraphicsScene::MyGraphicsScene(QWidget *parent):
  32. QGraphicsScene(parent)
  33. {
  34. }
  35. MyGraphicsScene::~MyGraphicsScene()
  36. {
  37. }
  38. void MyGraphicsScene::createRect(const QPointF &pt)
  39. {
  40. //MyGraphicsRectItem *item=new MyGraphicsRectItem;
  41. QGraphicsRectItem *item = new QGraphicsRectItem();
  42. item->setBrush(Qt::red);
  43. item->setRect(0,0,20,30);
  44. item->setPos(pt);
  45. qDebug() << "createRect pos:" << pt.x() << pt.y();
  46. // item->setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
  47. addItem(item);
  48. }
  49. void MyGraphicsScene::createLine(const QPointF &pt)
  50. {
  51. MyGraphicsLineItem *item=new MyGraphicsLineItem;
  52. item->setPos(pt);
  53. item->setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
  54. addItem(item);
  55. }
  56. void MyGraphicsScene::createPixmap(const QPointF &pt, const QPixmap &pixmap)
  57. {
  58. QGraphicsPixmapItem *item = new QGraphicsPixmapItem();
  59. item->setPixmap(pixmap);
  60. item->setPos(pt);
  61. qDebug() << "createRect pos:" << pt.x() << pt.y();
  62. // item->setFlags(QGraphicsItem::ItemIsSelectable|QGraphicsItem::ItemIsMovable);
  63. addItem(item);
  64. }
  1. #ifndef MYGRAPHICSVIEW_H
  2. #define MYGRAPHICSVIEW_H
  3. #include <QObject>
  4. #include <QGraphicsView>
  5. class MyGraphicsView : public QGraphicsView
  6. {
  7. public:
  8. MyGraphicsView(QWidget *parent=0);
  9. ~MyGraphicsView();
  10. protected:
  11. void dragEnterEvent(QDragEnterEvent *event);
  12. void dragMoveEvent(QDragMoveEvent *event) ;
  13. void dropEvent(QDropEvent *event);
  14. void mouseMoveEvent(QMouseEvent *event);
  15. void mousePressEvent(QMouseEvent *event);
  16. void mouseReleaseEvent(QMouseEvent *event) ;
  17. public:
  18. bool m_blinedraw{false};
  19. private:
  20. QPoint startpoint;
  21. QPoint finishpoint;
  22. };
  23. #endif // MYGRAPHICSVIEW_H
  24. #include "mygraphicsview.h"
  25. #include "mygraphicsscene.h"
  26. #include <QDragEnterEvent>
  27. #include <QMimeData>
  28. #include <QDebug>
  29. #include <QMouseEvent>
  30. #include <QPoint>
  31. #include <QGraphicsLineItem>
  32. #if _MSC_VER >=1600
  33. #pragma execution_character_set("utf-8")
  34. #endif
  35. MyGraphicsView::MyGraphicsView(QWidget *parent):
  36. QGraphicsView(parent)
  37. {
  38. startpoint.setX(0);
  39. startpoint.setY(0);
  40. finishpoint.setX(0);
  41. finishpoint.setY(0);
  42. }
  43. MyGraphicsView::~MyGraphicsView()
  44. {
  45. }
  46. void MyGraphicsView::dragEnterEvent(QDragEnterEvent *event)
  47. {
  48. if(event->mimeData()->hasFormat("text/plain")){
  49. event->acceptProposedAction();
  50. qDebug() << "view dragEnterEvent";
  51. }
  52. QGraphicsView::dragEnterEvent(event);
  53. }
  54. void MyGraphicsView::dragMoveEvent(QDragMoveEvent *event)
  55. {
  56. qDebug() << "view dragMoveEvent";
  57. event->acceptProposedAction();
  58. }
  59. void MyGraphicsView::dropEvent(QDropEvent *event)
  60. {
  61. qDebug() << "view dropevent";
  62. if (event->mimeData()->hasFormat("draglist"))
  63. {
  64. QByteArray itemData = event->mimeData()->data("draglist");
  65. QString strText = itemData;
  66. MyGraphicsScene *scene=(MyGraphicsScene*)this->scene();
  67. QPoint po = event->pos();
  68. qDebug() << "dropevent pos:"<< po.x() << po.y() ;
  69. if (strText == "直线")
  70. {
  71. scene->createLine(mapToScene(event->pos()));
  72. }
  73. else if (strText == "矩形")
  74. {
  75. scene->createRect(mapToScene(event->pos()));
  76. }
  77. else
  78. {
  79. QDataStream dataStream(&itemData, QIODevice::ReadOnly);
  80. QPixmap pixmap;
  81. dataStream >> pixmap;
  82. scene->createPixmap(mapToScene(event->pos()), pixmap);
  83. }
  84. event->acceptProposedAction();
  85. }
  86. QGraphicsView::dropEvent(event);
  87. }
  88. void MyGraphicsView::mouseMoveEvent(QMouseEvent *event)
  89. {
  90. QGraphicsView::mouseMoveEvent(event);
  91. int a = 1;
  92. }
  93. void MyGraphicsView::mousePressEvent(QMouseEvent *event)
  94. {
  95. if (event->button()==Qt::LeftButton && m_blinedraw)
  96. {
  97. QPoint viewpos = event->pos();
  98. QPointF scenepos = mapToScene(viewpos);
  99. QGraphicsItem *item=NULL;
  100. item = scene()->itemAt(scenepos,transform());
  101. if (item != nullptr) //有绘图项
  102. {
  103. startpoint = viewpos;
  104. }
  105. }
  106. QGraphicsView::mousePressEvent(event);
  107. int a = 1;
  108. }
  109. void MyGraphicsView::mouseReleaseEvent(QMouseEvent *event)
  110. {
  111. if (event->button()==Qt::LeftButton && m_blinedraw)
  112. {
  113. QPoint viewpos = event->pos();
  114. QPointF scenepos = mapToScene(viewpos);
  115. QGraphicsItem *item=NULL;
  116. item = scene()->itemAt(scenepos,transform());
  117. if (item != nullptr && !(startpoint.x() == 0 && startpoint.y() == 0) ) //有绘图项
  118. {
  119. finishpoint = viewpos;
  120. QLineF line(mapToScene(startpoint),scenepos);
  121. QGraphicsLineItem *lineitem = new QGraphicsLineItem(line);
  122. scene()->addItem(lineitem);
  123. startpoint.setX(0);
  124. startpoint.setY(0);
  125. finishpoint.setX(0);
  126. finishpoint.setY(0);
  127. }
  128. }
  129. QGraphicsView::mouseReleaseEvent(event);
  130. }
  1. #ifndef MYGRAPHICSLINEITEM_H
  2. #define MYGRAPHICSLINEITEM_H
  3. #include <QGraphicsItem>
  4. class MyGraphicsLineItem : public QGraphicsItem
  5. {
  6. public:
  7. MyGraphicsLineItem();
  8. ~MyGraphicsLineItem();
  9. QRectF boundingRect()const;
  10. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  11. };
  12. #endif // MYGRAPHICSLINEITEM_H
  13. #include "mygraphicslineitem.h"
  14. #include <QPainter>
  15. #include <QBrush>
  16. MyGraphicsLineItem::MyGraphicsLineItem()
  17. {
  18. }
  19. MyGraphicsLineItem::~MyGraphicsLineItem()
  20. {
  21. }
  22. QRectF MyGraphicsLineItem::boundingRect() const
  23. {
  24. qreal penWidth=1;
  25. return QRectF(0-penWidth/2,0-penWidth/2,4+penWidth,80+penWidth);
  26. }
  27. void MyGraphicsLineItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  28. {
  29. Q_UNUSED(option)
  30. Q_UNUSED(widget)
  31. painter->setBrush(Qt::red);
  32. painter->drawRect(0,0,4,80);
  33. }

  1. #ifndef MYGRAPHICSRECTITEM_H
  2. #define MYGRAPHICSRECTITEM_H
  3. #include <QGraphicsItem>
  4. class MyGraphicsRectItem : public QGraphicsItem
  5. {
  6. public:
  7. MyGraphicsRectItem();
  8. ~MyGraphicsRectItem();
  9. QRectF boundingRect()const;
  10. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  11. };
  12. #endif // MYGRAPHICSRECTITEM_H
  13. #include "mygraphicsrectitem.h"
  14. #include <QPainter>
  15. #include <QDebug>
  16. #include <QBrush>
  17. MyGraphicsRectItem::MyGraphicsRectItem()
  18. {
  19. }
  20. MyGraphicsRectItem::~MyGraphicsRectItem()
  21. {
  22. }
  23. QRectF MyGraphicsRectItem::boundingRect() const
  24. {
  25. qreal penWidth=1;
  26. return QRectF(0-penWidth/2,0-penWidth/2,18+penWidth,56+penWidth);
  27. }
  28. void MyGraphicsRectItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  29. {
  30. Q_UNUSED(option)
  31. Q_UNUSED(widget)
  32. painter->setBrush(Qt::red);
  33. painter->drawRect(0,0,18,56);
  34. }
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QtSvg>
  5. namespace Ui {
  6. class Widget;
  7. }
  8. class MyGraphicsScene;
  9. class MyGraphicsView;
  10. class MyListWidget;
  11. class Widget : public QWidget
  12. {
  13. Q_OBJECT
  14. public:
  15. explicit Widget(QWidget *parent = nullptr);
  16. ~Widget();
  17. MyGraphicsScene * m_scene;
  18. MyGraphicsView *m_view;
  19. MyListWidget *listWidget1;
  20. private:
  21. Ui::Widget *ui;
  22. protected:
  23. void paintEvent(QPaintEvent *event);
  24. QSvgRenderer* svgRender;
  25. public slots:
  26. void readbuttonslot();
  27. void writebuttonslot();
  28. void Linebuttonslot();
  29. void Allreadbuttonslot();
  30. };
  31. #endif // WIDGET_H
  32. #include "widget.h"
  33. #include "ui_widget.h"
  34. #include "mylistwidget.h"
  35. #include "mygraphicsscene.h"
  36. #include <QDebug>
  37. #include <QGraphicsItem>
  38. #include <QList>
  39. #include <mygraphicsview.h>
  40. #include "mysvgwidget.h"
  41. #include <QPushButton>
  42. #if _MSC_VER >=1600
  43. #pragma execution_character_set("utf-8")
  44. #endif
  45. Widget::Widget(QWidget *parent) :
  46. QWidget(parent),
  47. ui(new Ui::Widget)
  48. {
  49. ui->setupUi(this);
  50. QStringList list1, list2;
  51. list1 <<"直线" <<"矩形";
  52. list2 <<"阿童木" <<"皮卡丘" <<"卡卡西" <<"奈落"<<"一休" << "黑猫警长" <<"兔八哥"<<"三毛"<<"舒克"<<"贝塔";;
  53. listWidget1 = new MyListWidget(this);
  54. listWidget1->addItems(list1);
  55. listWidget1->setIconSize(QSize(32,32));
  56. listWidget1->setGeometry(100,100,500,300);
  57. listWidget1->setDragEnabled(true);
  58. m_scene = new MyGraphicsScene();
  59. m_scene->setSceneRect(QRectF(-300,-200,600,400));
  60. m_view = new MyGraphicsView(this);
  61. m_view->setScene(m_scene);
  62. m_view->setGeometry(500,100,400,600);
  63. m_view->setAcceptDrops(true);
  64. // m_view->show();
  65. QString strPath = "arrow.svg";
  66. svgRender = new QSvgRenderer(this);
  67. bool ret = svgRender->load(QString("G:/opencode/qt/msvc/test/drag/arrow.svg"));
  68. int a = 1;
  69. MySvgWidget *psvgwidget;
  70. psvgwidget = new MySvgWidget(this);
  71. psvgwidget->setGeometry(200,0,200,200);
  72. psvgwidget->show();
  73. psvgwidget->load(QString("G:/opencode/qt/msvc/test/drag/arrow.svg"));
  74. QPushButton *pReadbutton = new QPushButton(tr("加载svg"),this);
  75. pReadbutton->setGeometry(10,height()-50,60,40);
  76. connect(pReadbutton,SIGNAL(clicked()),this,SLOT(readbuttonslot()));
  77. QPushButton *pWritebutton = new QPushButton(tr("保存svg"),this);
  78. pWritebutton->setGeometry(100,height()-50,60,40);
  79. connect(pWritebutton,SIGNAL(clicked()),this,SLOT(writebuttonslot()));
  80. QPushButton *pLinebutton = new QPushButton(tr("连线"),this);
  81. pLinebutton->setGeometry(200,height()-50,60,40);
  82. connect(pLinebutton,SIGNAL(clicked()),this,SLOT(Linebuttonslot()));
  83. QPushButton *pAllReadbutton = new QPushButton(tr("一键加载svg"), this);
  84. pAllReadbutton->setGeometry(280, height() - 50, 60, 40);
  85. connect(pAllReadbutton, SIGNAL(clicked()), this, SLOT(Allreadbuttonslot()));
  86. }
  87. Widget::~Widget()
  88. {
  89. delete ui;
  90. }
  91. void Widget::paintEvent(QPaintEvent *event)
  92. {
  93. QPainter p(this);
  94. svgRender->render(&p,QRectF(0,0,100,100));
  95. }
  96. void Widget::readbuttonslot()
  97. {
  98. QString filePath = QFileDialog::getOpenFileName(0, "打开文件", ".", "SVG(*.svg)");
  99. if(filePath.isEmpty())
  100. {
  101. return;
  102. }
  103. QSvgRenderer *renderer = new QSvgRenderer(filePath);
  104. QGraphicsSvgItem *blackSvgItem = new QGraphicsSvgItem();
  105. blackSvgItem->setSharedRenderer(renderer);
  106. m_scene->addItem(blackSvgItem);
  107. blackSvgItem->setFlags (QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);
  108. }
  109. void Widget::writebuttonslot()
  110. {
  111. QString filePath = QFileDialog::getSaveFileName(0, "保存文件", ".", "SVG(*.svg)");
  112. if(filePath.isEmpty())
  113. {
  114. return;
  115. }
  116. QSvgGenerator svgGenerator;
  117. svgGenerator.setFileName(filePath);
  118. svgGenerator.setSize(QSize(rect().width(), rect().height()));
  119. svgGenerator.setViewBox(rect()); svgGenerator.setTitle("测试QSvgGenerator");
  120. svgGenerator.setDescription("另存为绘制测试");
  121. QPainter painter;
  122. painter.begin(&svgGenerator);
  123. painter.setPen(Qt::red);
  124. painter.drawLine(QPointF(20,20),QPointF(100,100));
  125. painter.drawText(rect(), "测试Svg");
  126. QPixmap pixmap = this->grab(QRect(0, 0, 1000, 800));
  127. painter.drawPixmap(10,10,pixmap);
  128. painter.end();
  129. }
  130. void Widget::Linebuttonslot()
  131. {
  132. if(m_view)
  133. {
  134. m_view->m_blinedraw = true;
  135. }
  136. }
  137. void Widget::Allreadbuttonslot()
  138. {
  139. listWidget1->m_strList.clear();
  140. QDir dir1;
  141. QString strdir = dir1.currentPath();
  142. strdir += "/svg";
  143. QDir dir(strdir);
  144. listWidget1->m_strList = dir.entryList(QDir::Files);
  145. int num = listWidget1->m_strList.size();
  146. QString strfile;
  147. for (int i = 0; i < num; ++i)
  148. {
  149. strfile = strdir + "/" +listWidget1->m_strList[i];
  150. QListWidgetItem *item = new QListWidgetItem(QIcon(strfile),"");
  151. listWidget1->addItem(item);
  152. }
  153. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/91705
推荐阅读
相关标签
  

闽ICP备14008679号