2016-11-03 179 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

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