QTextEdit实现图片和文本同行显示
1.開發(fā)環(huán)境:Qt 4.7.3 + MinGW 32bit (MSVC2015 32bit)
2.實(shí)現(xiàn)大綱:
??????? 1)實(shí)現(xiàn)圖片和文本在同一行顯示,且能夠設(shè)置字體大小和顏色
??????? 2)能夠手動(dòng)設(shè)置行的寬度和間距
??????? 3)設(shè)置字體樣式,然后添加圖片,字體樣式重置成默認(rèn)狀態(tài)
3.具體實(shí)現(xiàn):
??????? 1)QTextEdit支持文本和圖片顯示,通過將圖片轉(zhuǎn)化為html字符串的形式,然后調(diào)用append函數(shù)將圖片添加到QEditText中即即可,具體實(shí)現(xiàn)API如下:
??????? void changeImagePathToHtml(QString &path) {
??????????????? path = QString("<image src = \"%1\"/>").arg(path);
??????? }
??????? 2)一般來說,文檔都需要設(shè)置每一行的寬度和間距,QTextEdit中通過QTextCusor和QTextBlockFormat來設(shè)置實(shí)現(xiàn),具體實(shí)現(xiàn)API如下:
??????? void setFixedLineHeight(int height) {
??????????????? QTextCusor textCusor = m_logInfoTextEdit->textCusor();?? //m_logInfoTextEdit為自己新建的QTextEdit
??????????????? QTextCusorFormat textBlockFormat;
??????????????? textBlockFormat->setLineEdit(height, QTextBlockFormat::FixedHeight);
??????????????? textCusor.setBlockFormat(textBlockFormat);
??????????????? m_logInfoTextEdit->setTextCusor(textCusor);
??????? }
??????? void setFixedLineSpacing(int space) {
??????????????? QTextCusor textCusor = m_logInfoTextEdit->textCusor();?? //m_logInfoTextEdit為自己新建的QTextEdit
??????????????? QTextCusorFormat textBlockFormat;
??????????????? textBlockFormat->setBottomMargin(space);
??????????????? textCusor.setBlockFormat(textBlockFormat);
??????????????? m_logInfoTextEdit->setTextCusor(textCusor);
??????? }
??????? 3)設(shè)置字體顏色和只讀換行屬
??????????????? 1)由于我需要實(shí)現(xiàn)的log工作區(qū)的實(shí)現(xiàn),因此需要設(shè)置QTextEdit只讀屬性,調(diào)用setReadOnly(true)即可
??????????????? 2)由于設(shè)置的格式是圖片+文本的形式,因此將行打包模式設(shè)置成不打包(系統(tǒng)不會(huì)自動(dòng)添加換行符),調(diào)用setLineWrapMode(QTextEdit::NoWrap);
??????????????? 3)設(shè)置顏色采用QPalette來實(shí)現(xiàn),具體實(shí)現(xiàn)如下:
?????????????????? ? QPalette p = palette();?? p.setColor(QPalette::Text, QColor(0, 255, 0);?
???????????????????? m_logInfoTextEdit->setPalette(p); //m_logInfoTextEdit同上
??????? 4)? 將上述實(shí)現(xiàn)封裝成API,方便調(diào)用:
??????????????? void addLogInformation(int logRank, QString logInfo) {
?????????????????????? m_logInfoTextEdit->moveCursor(QTextCusro::End, QTextCusor::MoveAnchor); //鼠標(biāo)一直文檔末尾
?????????????????????? QString imagePath;
?????????????????????? switch(logRank) {
?????????????????????????????? case 1:
??????????????????????????????????????? imagePath = QString(":resource/icons/error.svg");? //圖片大小無法自適應(yīng)(沒找到設(shè)置方法)
??????????????????????????????????????? break;???????????????????????????????????????????????????????????????????????? //因此設(shè)置圖片大小成每行的間距
?????????????????????????????? case 2:??????????????????????????????????????????????????????????????????????????????? //如果有博主知道如何修改,請(qǐng)?jiān)谙旅媪粞?/p>
?????????????????????????????????????? imagePath = QString(":resource/icons/warning.svg);
??????????????????????????????????????? break;
?????????????????????????????? default:
??????????????????????????????????????? imagePath = QString(":resource/icons/info.svg);
??????????????????????????????????????? break;
?????????????????????? }
?????????????????????? changeImagePathToHtml(imagePath);
?????????????????????? m_logInfoTextEdit->append(imagePath);
?????????????????????? m_logInfoTextEdit->setFontPointSize(20);?? //在添加完圖片之后,字體大小設(shè)置重置,需要重新設(shè)置
?????????????????????? QDateTime *dateTime = new QDateTime(QDateTime::currentDateTime());
?????????????????????? QString str = dateTime->toString(" [yyyy-mm-dd hh:mm:ss]");
?????????????????????? str += " " + logInfo;
?????????????????????? m_logInfoTextEdit->insertPlainText(str);
?????????????? }
4. 具體實(shí)現(xiàn)效果:
5.延伸:實(shí)現(xiàn)編譯器的問題編譯問題列表樣式(可以考慮使用QListWidget添加Item來具體實(shí)現(xiàn))
???????????? 這里的圖片自適應(yīng)大小,通過setIcon來設(shè)置
?
總結(jié)
以上是生活随笔為你收集整理的QTextEdit实现图片和文本同行显示的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 摩尔庄园宅宅三兄弟如何获取
- 下一篇: 解决QTreeWidget中item无法