当前位置:   article > 正文

Qt 富文本 表格列表图片_在qt中实现在富文本编辑器,支持插入图片和表格

在qt中实现在富文本编辑器,支持插入图片和表格

1.在mainwindow.cpp添加工具按钮

  1. //添加工具按钮
  2. QAction *action_textTable = new QAction("表格",this);
  3. QAction *action_textList = new QAction("列表",this);
  4. QAction *action_textIamge = new QAction("图片",this);
  5. //进行连接
  6. connect(action_textTable,&QAction::triggered,
  7. this,&MainWindow::insertTable);
  8. connect(action_textList,&QAction::triggered,
  9. this,&MainWindow::insertList);
  10. connect(action_textIamge,&QAction::triggered,
  11. this,&MainWindow::insertIamge);
  12. //添加到工具栏上
  13. ui->mainToolBar->addAction(action_textTable);
  14. ui->mainToolBar->addAction(action_textList);
  15. ui->mainToolBar->addAction(action_textIamge);

2.在头文件里面添加槽函数

  1. void insertTable(); //插入表格
  2. void insertList(); //插入列表
  3. void insertIamge(); //插入图片

 3.对槽函数进行定义

  1. void MainWindow::insertTable()
  2. {
  3. //对文本进行操作首先要获取光标
  4. QTextCursor cursor = ui->textEdit->textCursor();
  5. QTextTableFormat format; //表格格式
  6. format.setCellSpacing(2); //表格外边白
  7. format.setCellPadding(10); //表格内边白
  8. cursor.insertTable(3,3,format); //三行三列
  9. }
  10. void MainWindow::insertList()
  11. {
  12. //列表格式
  13. QTextListFormat format;
  14. //数字编号
  15. format.setStyle(QTextListFormat::ListDecimal);
  16. ui->textEdit->textCursor().insertList(format);
  17. }
  18. void MainWindow::insertIamge()
  19. {
  20. QString filepath = QFileDialog::getOpenFileName(this,
  21. "选择图片",
  22. ".",
  23. "JPEG(*.jpg *.jpeg);;"
  24. "GIF(*.gif);;"
  25. "PNG(*.png)");
  26. QUrl url(QString("file://%1").arg(filepath));
  27. QImage image = QImageReader(filepath).read();
  28. QTextDocument* document = ui->textEdit->document();
  29. //文档添加图片资源
  30. document->addResource(QTextDocument::ImageResource,
  31. url,QVariant(image));
  32. QTextCursor cursor = ui->textEdit->textCursor();
  33. QTextImageFormat imgFormat;
  34. imgFormat.setWidth(image.width());
  35. imgFormat.setHeight(image.height());
  36. imgFormat.setName(url.toString());
  37. cursor.insertImage(imgFormat);
  38. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小惠珠哦/article/detail/764775
推荐阅读
相关标签
  

闽ICP备14008679号