【问题标题】:QWidget for showing text with small pictures (icons/emoticons)QWidget 用于显示带有小图片(图标/表情符号)的文本
【发布时间】:2014-05-05 01:13:33
【问题描述】:

这样的小部件存在吗?

我可以基于QLabels 和类似于http://qt-project.org/doc/qt-5/qtwidgets-layouts-flowlayout-example.html 的布局编写自己的小部件,但是我无法选择所有文本并复制(因为这只是一组标签)。

【问题讨论】:

  • 为什么您认为无法在 QLabel 中选择文本?
  • 也许可以,但我不能同时在多个标签中进行。

标签: c++ qt qt5 qwidget


【解决方案1】:

QLabel 的text property 可以在其中包含富文本,而img tag is supported 在Qt 的富文本中。

例如,

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

【讨论】:

    【解决方案2】:

    您可以使用QTextEdit,当您在其中输入某些特定文本时,它会变为笑脸或表情符号等图像。您应该对textChanged() 信号做出反应并使用QTextCursor 将文本替换字符串修改为一些HTML 图像标签:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 2021-10-11
      • 2012-05-13
      • 2012-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多