How to insert an image on QTextEdit
-
wrote on 2 Jun 2018, 06:48 last edited by
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); } }
-
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.
-
Using Google I found this link: https://stackoverflow.com/questions/3254652/several-ways-of-placing-an-image-in-a-qtextedit which provides several solutions.
-
@aha_1980
Thanks you .
I try this solution but it doesn't work. I don't need an QTextDocument.@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. -
Using Google I found this link: https://stackoverflow.com/questions/3254652/several-ways-of-placing-an-image-in-a-qtextedit which provides several solutions.
wrote on 2 Jun 2018, 22:06 last edited by@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);
-
wrote on 3 Jun 2018, 00:11 last edited by
@jacky2580 if your issue is solved,please don't forget to mark your post as such. Thanks
-
@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);
wrote on 5 Jun 2021, 16:31 last edited by@jacky2580 , maybe you know how change size image?
-
@jacky2580 , maybe you know how change size image?
-
wrote on 6 Jun 2021, 08:38 last edited by
@mrjj , i can set the size via QComboBox for example?
-
@mrjj , i can set the size via QComboBox for example?
Lifetime Qt Championwrote on 6 Jun 2021, 08:50 last edited by mrjj 6 Jun 2021, 08:50@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. -
@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.wrote on 6 Jun 2021, 19:26 last edited by@mrjj , tnx