2016-11-03 78 views
0

我花了几天搜索如何解决一个问题,我尝试了不同的方法,但没有结果。我的问题是这样的:我用Qt框架(C++)开发了一个聊天程序,我想显示聊天记录。在每个到达的消息中都有一个用户的图标,他的名字和消息到达的确切日期。该消息可以包含文本的意志为图标(gif或只是png格式)(就像Facebook或Skype的) 我想这样的正是: Like This Exactly显示聊天消息使用Qt(C++)

这是我曾尝试。 我创建了一个名为MessageText的类,派生自QPlaintextEdit,它有一个名为append的方法来添加新消息,但问题是我无法添加图标,而且用户无法更改消息颜色。 这是我的代码:

#include "MessageText.h" 
    #include <QTextEdit> 
    MessageText::MessageText() 
    { 
     this->setObjectName("asf"); 
     this->setStyleSheet("#asf{background-color:#AACC44;}"); 
     this->setReadOnly(true);  
    } 

    void MessageText::appendMessage(QString icon, QString name, QString text) 
    { 
     QLabel *nameTime1=new QLabel(this); //container of user's name and the    date 
     QLabel *iconContiner1=new QLabel(this); /container of user's icon 
     nameTime1->setStyleSheet("background-color:rgb(242,242,242);"); 
     nameTime1->setText("   "+QString(QChar(0x200E))+name); 
     iconContiner1->setStyleSheet("background-image:url(ua/"+icon+".png);background-repeat:no-repeat;"); 

      this->appendPlainText(+"\n\n"+text); // Adds the message to the widget 

      nameTime1->setGeometry(0,(this->document()->size().height()-2)*22,1056,18); //to put new message just after the previous 
      iconContiner1->setGeometry(2,(this->document()->size().height()-2)*22,27,27); 
      this->verticalScrollBar()->setValue(this->verticalScrollBar()->maximum()); 
      nameTime1->show(); 
      iconContiner1->show(); 
    } 

什么是实现thisi会很感激 感谢你洙多提前最好的办法。

+0

你为什么从QPlaintextEdit派生类? –

+0

,因为我在这个类中定义了一个方法(追加)来追加每次新消息也只写短代码。因为像私人谈话一样公开谈话。 –

+0

我相信这不是最好的方式来做到这一点。如果你可以让我更好的方式,我会非常感谢 –

回答

1

您可以在QLabel的文本中使用HTML。像text = "hello <img src=":/Desktop/GameAssetDesign/springgameAssets/MassBody_G.png" width = 30 height = 30>"

example

+0

这不完全是我想要的,我希望有人能帮助我 –