How to insert an image on QTextEdit
-
how can I do to display images. Here is my code but it does not open an image with QTextEdit.
thank you```QString text2 = ui->textEdit->toPlainText(); ui->textEdit->setText(text2); QString file = QFileDialog::getOpenFileName(this); if (!file.isEmpty()) { QFile sFile(file); if (sFile.open(QFile::ReadOnly |QFile::Text)) { QTextStream in (&sFile); QString text1; text2 += "\n"+ text1 + in.readAll() ; sFile.close(); ui->textEdit->setPlainText(text2); } }
-
Using Google I found this link: https://stackoverflow.com/questions/3254652/several-ways-of-placing-an-image-in-a-qtextedit which provides several solutions.
-
@jacky2580
Hi,
its not possible to show images using just plain text.
If you don't want to use QTextDocument , you will need to construct some html
to show image instead.
it will not accept some image as text and show it. -
@aha_1980
I resolved this problem.Thank you so much.//your code here
QString cheminImage = QFileDialog::getOpenFileName(this, "Choisir une image", QString(), "Images (.)");
QString texteFinal = ui->textEdit->toHtml() + "<img src = \""+ cheminImage +"\" alt = \"\"/>"; ui->textEdit->setHtml(texteFinal);
-
@jacky2580 if your issue is solved,please don't forget to mark your post as such. Thanks
-
@jacky2580 , maybe you know how change size image?
-
-
@Alex_Tsikhun
Hi
Sure you could let user choose some predefined sized from a combobox and then use
these values to make the HTML.However, if you plan on letting user alter size after it has been inserted then
you might want to look into
https://doc.qt.io/qt-5/qtextimageformat.html
which lets you control size after it has been inserted.