找回密码
 注册
搜索
查看: 1043|回复: 0

[讨论] 【武汉华嵌】QTextEdit实现QQ聊天对话框效果

[复制链接]
发表于 2012-4-19 10:55:23 | 显示全部楼层 |阅读模式
作者:武汉华嵌技术部

本文主要介绍了怎么使用QTextEdit控件实现类似QQ聊天对话框的效果,包括令对话框显示不同的字体、颜色,实现QQ震动效果和QQ表情效果(即在QTextEdit中显示图片)。Demo运行的界面如下图所示:





点击相关按钮可以改变输入文本框的字体,改变输入文本框的颜色,实现QQ震动效果,以及在文本框中添加自定义图片。下面将通过demo讲解实现原理。

#include <QtGui>

#include "qt1.h"

Qt1::Qt1(QWidget *parent):QDialog(parent)

{

    setupUi(this);

    tbfont->setIcon(QIcon("./images/bold.png"));

    tbcolor->setIcon(QIcon("./images/color.png"));

    tbtru->setIcon(QIcon("./images/italic.png"));

    tbbq->setIcon(QIcon("./images/biaoqing.png"));

    tbsend->setIcon(QIcon("./images/underline.png"));



    connect(tbfont,SIGNAL(clicked()),this,SLOT(fontfun()));//字体类型

    connect(tbcolor,SIGNAL(clicked()),this,SLOT(colorfun()));//字体颜色

    connect(tbtru,SIGNAL(clicked()),this,SLOT(trumfun()));//震动

    connect(tbbq,SIGNAL(clicked()),this,SLOT(insertImage()));//插入图片

    connect(tbsend,SIGNAL(clicked()),this,SLOT(sendfun()));//发送消息

}



void Qt1::fontfun()//设置字体类型

{

    bool ok;

    QFont font = QFontDialog::getFont(

                 &ok, QFont("Helvetica [Cronyx]", 10), this);

    if (ok)

    {

            te2->setFont(font);

        f = font;

    }

}

     

void Qt1::colorfun() //设置字体颜色

{

    QColor color = QColorDialog::getColor ( Qt::red, this );

    te2->setTextColor(color);

    c = color;

}



void Qt1::trumfun() //震动效果实现

{

    QPoint p = getPos(this);//获取当前窗口坐标

    int x = p.x();

    int y = p.y();

    int i = 0;



    for(i=0;i<10;i++)//改变窗口位置实现震动效果

    {

        this->move(x+5,y+5);

        usleep(300);

        this->move(x,y);

        usleep(300);

        this->move(x-5,y-5);

        usleep(300);

        this->move(x,y);

    }  



    this->move(x-1,y-28);

}



void Qt1::sendfun()

{

    QFont ff (QFont( "AR PL UKai CN,10,-1,5,50,0,0,0,0,0" )) ;

    QColor cc (0,0,0);

    setInsertTextColor(cc);//设置系统时间字体颜色

    setInsertTextFont(ff);//设置系统时间字体类型

    te1->append(showTime());

    setInsertTextColor(c);//设置插入字体颜色

    setInsertTextFont(f);//设置插入字体字体类型

    te1->append(te2->toPlainText());

    te2->clear();

}



void Qt1::setInsertTextColor(const QColor &col)//把col颜色作为字体颜色

{

    QTextCharFormat fmt;//文本字符格式

    fmt.setForeground(col);// 前景色(即字体色)设为col色

    QTextCursor cursor = te1->textCursor();//获取文本光标

    cursor.mergeCharFormat(fmt);//光标后的文字就用该格式显示

    te1->mergeCurrentCharFormat(fmt);//textEdit使用当前的字符格式

}



void Qt1::setInsertTextFont(const QFont &col)//把col颜色作为字体颜色

{

    QTextCharFormat fmt;//文本字符格式

    fmt.setFont(col);//字体

    QTextCursor cursor = te1->textCursor();//获取文本光标

    cursor.mergeCharFormat(fmt);//光标后的文字就用该格式显示

    te1->mergeCurrentCharFormat(fmt);//textEdit使用当前的字符格式

}



void Qt1::insertImage() //文本编辑框中插入图片

{

    QString file = QFileDialog::getOpenFileName(this, tr("Open File"),"pic/",
                                            tr("Images (*.png *.jpg)"));

    QUrl Uri ( QString ( "file://%1" ).arg ( file ) );

    QImage image = QImageReader ( file ).read();



    QTextDocument * textDocument = te2->document();

    textDocument->addResource( QTextDocument::ImageResource, Uri, QVariant ( image ) );

    QTextCursor cursor = te2->textCursor();

    QTextImageFormat imageFormat;

    imageFormat.setWidth( image.width() );

    imageFormat.setHeight( image.height() );

    imageFormat.setName( Uri.toString() );

    cursor.insertImage(imageFormat);

}



QPoint Qt1::getPos(QWidget* widget)//获取窗体位置坐标

{

     if (NULL == widget)

     {

        return QPoint(-1,-1);

     }

     

     float px = 0;

     float py = 0;

     QWidget *parent = widget;

     QWidget *preParent= NULL;

     do

     {

          QPoint p = parent->pos();

          px += p.x();

          py += p.y();

          preParent = parent ;

          parent = parent->parentWidget();

     } while (NULL != parent);

     

     QSize size = preParent->frameSize();

     QSize size2 = preParent->size();

     px = px + (size.width() - size2.width())/2;

     py = py + (size.height() - size2.height() - (size.width() - size2.width())/2);

     QPoint pr(px, py);

     

     return pr;

}



QString Qt1::showTime()//获取系统时间

{

    QTime time=QTime::currentTime();

    QString text = time.toString("hh:mm:ss");



    return text;

}


更多请链接武汉华嵌:http://www.embedhq.org/html/jsbw/2012/0418/3354.htmlhttp://www.embedhq.org/html/jsbw/2012/0418/3354.html
高级模式
B Color Image Link Quote Code Smilies

本版积分规则

Archiver|手机版|小黑屋|52RD我爱研发网 ( 沪ICP备2022007804号-2 )

GMT+8, 2024-5-4 00:11 , Processed in 0.045201 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表