QWidget for showing text with small pictures (icons/emoticons)

折月煮酒 提交于 2019-12-23 17:08:39

问题


Does such widget exist?

I can write my own widget based on QLabels and layout similar to http://qt-project.org/doc/qt-5/qtwidgets-layouts-flowlayout-example.html, but then I can't select all text and copy (because this is just a set of labels).


回答1:


A QLabel's text property can have rich text in it, and the img tag is supported in rich text in Qt.

For example,

QLabel myLabel("<img src=\":/foo.png\"> Hello, World!");



回答2:


You can use a QTextEdit in which when you enter some specific text, it changes to an image like a smiley or emoticon. You should react on textChanged() signal and use QTextCursor to modify the text replacing string with some HTML image tags:

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

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\">");
     }
}


来源:https://stackoverflow.com/questions/23463457/qwidget-for-showing-text-with-small-pictures-icons-emoticons

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!