Qtextedit 中插入图片的方法

QTextCursor cursor = ui->textEdit->textCursor();
  
  // insert the current plot at the cursor position. QCPDocumentObject::generatePlotFormat creates a
  // vectorized snapshot of the passed plot (with the specified width and height) which gets inserted
  // into the text document.
  double width = ui->cbUseCurrentSize->isChecked() ? 0 : ui->sbWidth->value();
  double height = ui->cbUseCurrentSize->isChecked() ? 0 : ui->sbHeight->value();
  cursor.insertText(QString(QChar::ObjectReplacementCharacter), QCPDocumentObject::generatePlotFormat(ui->plot, width, height));
  

ui->textEdit->setTextCursor(cursor);



QTextCharFormat QCPDocumentObject::generatePlotFormat(QCustomPlot *plot, int width, int height)
{
  QPicture picture;
  QCPPainter qcpPainter;
  qcpPainter.begin(&picture);
  plot->toPainter(&qcpPainter, width, height);
  qcpPainter.end();
  
  QTextCharFormat result;
  result.setObjectType(QCPDocumentObject::PlotTextFormat);
  result.setProperty(QCPDocumentObject::PicturePropertyId, QVariant::fromValue(picture));
  return result;
}

你可能感兴趣的:(QT)