自动打开qq聊天对话框的方法

<iframe src="tencent://Message/?Menu=YES&amp;Uin=123456&amp;websiteName=im.qq.com" style="display:none"></iframe>

 

展开阅读全文

QTextEdit实现QQ聊天对话框效果

04-19

作者:武汉华嵌技术部rnrn本文主要介绍了怎么使用QTextEdit控件实现类似QQ聊天对话框的效果,包括令对话框显示不同的字体、颜色,实现QQ震动效果和QQ表情效果(即在QTextEdit中显示图片)。Demo运行的界面如下图所示:rnrnrnrn rnrn点击相关按钮可以改变输入文本框的字体,改变输入文本框的颜色,实现QQ震动效果,以及在文本框中添加自定义图片。下面将通过demo讲解实现原理。rnrn#include rnrn#include "qt1.h"rnrnQt1::Qt1(QWidget *parent):QDialog(parent)rnrnrnrn setupUi(this);rnrn tbfont->setIcon(QIcon("./images/bold.png"));rnrn tbcolor->setIcon(QIcon("./images/color.png"));rnrn tbtru->setIcon(QIcon("./images/italic.png"));rnrn tbbq->setIcon(QIcon("./images/biaoqing.png"));rnrn tbsend->setIcon(QIcon("./images/underline.png"));rnrn rnrn connect(tbfont,SIGNAL(clicked()),this,SLOT(fontfun()));//字体类型rnrn connect(tbcolor,SIGNAL(clicked()),this,SLOT(colorfun()));//字体颜色rnrn connect(tbtru,SIGNAL(clicked()),this,SLOT(trumfun()));//震动rnrn connect(tbbq,SIGNAL(clicked()),this,SLOT(insertImage()));//插入图片rnrn connect(tbsend,SIGNAL(clicked()),this,SLOT(sendfun()));//发送消息rnrnrnrn rnrnvoid Qt1::fontfun()//设置字体类型rnrnrnrn bool ok;rnrn QFont font = QFontDialog::getFont(rnrn &ok, QFont("Helvetica [Cronyx]", 10), this);rnrn if (ok)rnrn rnrn te2->setFont(font);rnrn f = font;rnrn rnrnrnrn rnrnvoid Qt1::colorfun() //设置字体颜色rnrnrnrn QColor color = QColorDialog::getColor ( Qt::red, this );rnrn te2->setTextColor(color);rnrn c = color;rnrnrnrn rnrnvoid Qt1::trumfun() //震动效果实现rnrnrnrn QPoint p = getPos(this);//获取当前窗口坐标rnrn int x = p.x();rnrn int y = p.y();rnrn int i = 0;rnrn rnrn for(i=0;i<10;i++)//改变窗口位置实现震动效果rnrn rnrn this->move(x+5,y+5);rnrn usleep(300);rnrn this->move(x,y);rnrn usleep(300);rnrn this->move(x-5,y-5);rnrn usleep(300);rnrn this->move(x,y);rnrn rnrn rnrn this->move(x-1,y-28);rnrnrnrn rnrnvoid Qt1::sendfun()rnrnrnrn QFont ff (QFont( "AR PL UKai CN,10,-1,5,50,0,0,0,0,0" )) ;rnrn QColor cc (0,0,0);rnrn setInsertTextColor(cc);//设置系统时间字体颜色rnrn setInsertTextFont(ff);//设置系统时间字体类型rnrn te1->append(showTime());rnrn setInsertTextColor(c);//设置插入字体颜色rnrn setInsertTextFont(f);//设置插入字体字体类型rnrn te1->append(te2->toPlainText());rnrn te2->clear();rnrnrnrn rnrnvoid Qt1::setInsertTextColor(const QColor &col)//把col颜色作为字体颜色rnrnrnrn QTextCharFormat fmt;//文本字符格式rnrn fmt.setForeground(col);// 前景色(即字体色)设为col色rnrn QTextCursor cursor = te1->textCursor();//获取文本光标rnrn cursor.mergeCharFormat(fmt);//光标后的文字就用该格式显示rnrn te1->mergeCurrentCharFormat(fmt);//textEdit使用当前的字符格式rnrnrnrn rnrnvoid Qt1::setInsertTextFont(const QFont &col)//把col颜色作为字体颜色rnrnrnrn QTextCharFormat fmt;//文本字符格式rnrn fmt.setFont(col);//字体rnrn QTextCursor cursor = te1->textCursor();//获取文本光标rnrn cursor.mergeCharFormat(fmt);//光标后的文字就用该格式显示rnrn te1->mergeCurrentCharFormat(fmt);//textEdit使用当前的字符格式rnrnrnrn rnrnvoid Qt1::insertImage() //文本编辑框中插入图片rnrnrnrn QString file = QFileDialog::getOpenFileName(this, tr("Open File"),"pic/",\rnrn tr("Images (*.png *.jpg)"));rnrn QUrl Uri ( QString ( "file://%1" ).arg ( file ) );rnrn QImage image = QImageReader ( file ).read();rnrn rnrn QTextDocument * textDocument = te2->document();rnrn textDocument->addResource( QTextDocument::ImageResource, Uri, QVariant ( image ) );rnrn QTextCursor cursor = te2->textCursor();rnrn QTextImageFormat imageFormat;rnrn imageFormat.setWidth( image.width() );rnrn imageFormat.setHeight( image.height() );rnrn imageFormat.setName( Uri.toString() );rnrn cursor.insertImage(imageFormat);rnrn rnrn rnrnQPoint Qt1::getPos(QWidget* widget)//获取窗体位置坐标rnrnrnrn if (NULL == widget)rnrn rnrn return QPoint(-1,-1);rnrn rnrn rnrn float px = 0;rnrn float py = 0;rnrn QWidget *parent = widget;rnrn QWidget *preParent= NULL;rnrn dornrn rnrn QPoint p = parent->pos();rnrn px += p.x();rnrn py += p.y();rnrn preParent = parent ;rnrn parent = parent->parentWidget();rnrn while (NULL != parent);rnrn rnrn QSize size = preParent->frameSize();rnrn QSize size2 = preParent->size();rnrn px = px + (size.width() - size2.width())/2;rnrn py = py + (size.height() - size2.height() - (size.width() - size2.width())/2);rnrn QPoint pr(px, py);rnrn rnrn return pr;rnrnrnrn rnrnQString Qt1::showTime()//获取系统时间rnrnrnrn QTime time=QTime::currentTime();rnrn QString text = time.toString("hh:mm:ss");rnrn rnrn return text;rnrnrnrn 论坛

没有更多推荐了,返回首页