PDA

View Full Version : how to add smilies in QTextEdit



sivambigai
15th November 2010, 08:28
hi,

In QTextEdit if we enter ": )" it needs to change as image :) .. please guide me if u have any idea?

Thanks in advance.

Thanks & Regards,
Sivambigai.M

tbscope
15th November 2010, 08:40
React on text changed signals or events.
Use setHtml and replace the :-) string with some html image tags.

sivambigai
15th November 2010, 09:51
hi

Thanks for your idea..

i have used

QObject::connect(textEdit, SIGNAL(textChanged()), this, SLOT(changePixmap()),Qt::QueuedConnection) ;

void CSmsWidget::changePixmap()
{
QString text =textEdit->toPlainText();
if (Qt::mightBeRichText(text)){
textEdit->setHtml(text);
}
}

but this is not working.. any idea?

Thanks in advance.

tbscope
15th November 2010, 10:49
Do you think images come out of thin air?

replace the text :-) with something like <img source="...">

sivambigai
15th November 2010, 11:02
hi

Thanks for your reply :)


void CSmsWidget::changePixmap(){
QString text =textEdit->toPlainText();
if (text.contains(": )")){

QImage img(":/images/happy_smilie.png"); // todo: generate image in memory
m_currentMsg->document()->addResource(QTextDocument::HtmlResource, QUrl("happy_smilie.png" ), img);

text.replace(":)","<p><img src=\":/images/happy_smilie.png\"></p>");

m_currentMsg->setHtml(text);

}
}

This is working :) once again Thanks :)

Added after 6 minutes:

hi,

In the above code if i have entered two smiley character like this "hi :) (first one ) Hope you are fine :)(second)" .
In QTextEdit after entering the second smiley first image will get vanished.

Is it the problem because of both the smilies refers to the same image ?

Thanks in Advance

MarekR22
15th November 2010, 12:37
You are doing that in most possible wrong way. When you have multiple smileys then strange things will happen.
Take a look on that (QTextCursor is proper way of modifying editors/QTextDocument content):

void CSmsWidget::changePixmap()
{
QRegExp reg(":\\)"); // you can improve regular expression
QTextCursor cursor(textEdit->document()->find(reg));

if (!cursor.isNull()) {
cursor.insertHtml("<img src=\":/images/happy_smilie.png\">");
}
}

sivambigai
15th November 2010, 12:44
hi MarekR22,

Thanks for your help :)

sikanderrafiq
11th December 2018, 11:45
Please visit https://youtu.be/QXDfhien_vM

Please visit https://youtu.be/QXDfhien_vM for Emoticon widget to develop in Qt C++.