QT 网络通讯软件群聊源码

QT 网络通讯软件群聊源码

xteam.h-------------------------------------------------------------

#include<QtGui>
#include <QtGui/QWidget>
#include <QtNetwork>
#include <QTcpSocket>
#include <QUdpSocket>
#include <QAbstractSocket>
#include <QtGui/QLabel>
#include <QtGui/QLineEdit>
#include <QtGui/QPushButton>
#include <QString>
#include <QMainWindow>
#include <QPixmap>
#include <QFileInfo>
#include "ui_teamchat.h"
//#include "xselectface1.h"
class Team:public QMainWindow, public Ui::MainWindow
{
Q_OBJECT
public:
Team(int,int,QString,QStringList);
void messagefirmx(int ,QString ,QString );
private:
int id;
int userid;
QString sname;
QStringList nlist;
void list_init();
//selectfacemm *ssselectface;

//QString pic;
quint16 tcpport;
quint16 udpport;
//QTcpServer *server;
QTcpSocket *clientsocket;
QUdpSocket *udpsocket;
QHostAddress address;
QPixmap screenshot;
void writeDate(int flag);

void closeEvent(QCloseEvent *event);


private slots:
void setFontColor();
void setBoldFont(bool flag);
void setFontxx();
void windowshot();
//void sendFileSlot();
//void sendImageSlot();
//void showEmotionSlot();
//void showfaceSlot(QString);
void sendMessageSlot();
//void readMessageSlot();
void requestconnectSlot();
//void messagefirmSlot(int,QString,QString);
//void appendpushbuttonSlot();
//void messangeconfirm();

signals:
void closebutton(int);
//void friendmessage(QString,int);

};

team.cpp-------------------------------------------------------------------------

#include"xteam.h"
Team::Team(int userid,int id,QString sname,QStringList nlist)
{
setupUi(this);
this->userid=userid;
this->id=id;
this->sname=sname;
this->nlist=nlist;
list_init();
label->setNum(id);
textEdit->setReadOnly(true);
//ssselectface = new selectfacemm();

////////////////////////////////////////
//网络设定
tcpport = 60001;
address = "192.168.2.231";
udpport = 45454;
clientsocket = new QTcpSocket(this);
udpsocket = new QUdpSocket(this);
udpsocket->bind(QHostAddress::Any, udpport);
//工具按钮状态
toolButton_Font_3->setCheckable(true);
toolButton_Font_4->setCheckable(true);
toolButton_Font_2->setCheckable(true);
//信号和槽
connect(pushButton_3, SIGNAL(clicked()), this, SLOT(close()));
connect(toolButton_Font_5, SIGNAL(clicked()), this, SLOT(setFontColor()));
connect(toolButton_Font_3, SIGNAL(toggled(bool)), textEdit_2, SLOT(setFontItalic(bool)));
connect(toolButton_Font_4, SIGNAL(toggled(bool)), textEdit_2, SLOT(setFontUnderline(bool)));
connect(toolButton_Font_2, SIGNAL(toggled(bool)), this, SLOT(setBoldFont(bool)));
connect(toolButton_Font, SIGNAL(clicked()), this, SLOT(setFontxx()));
connect(toolButton_Font_7, SIGNAL(clicked()), this, SLOT(windowshot()));
//connect(toolButton_Font_6, SIGNAL(clicked()), this, SLOT(showEmotionSlot()));
//connect(ssselectface, SIGNAL(showiconSignal( QString )), this, SLOT(showfaceSlot(QString)));
connect(pushButton, SIGNAL(clicked()),this, SLOT(requestconnectSlot()));
connect(clientsocket, SIGNAL(connected()), this, SLOT(sendMessageSlot()));

}
void Team::list_init()
{
QTreeWidgetItem *a[50];
int d=0;
treeWidget->clear();
treeWidget->setColumnCount(2);
treeWidget->setColumnHidden (1,true );
QTreeWidgetItem *teamheadItem = new QTreeWidgetItem();
treeWidget->setHeaderItem(teamheadItem);
teamheadItem->setText(0, nlist.at(0));
teamheadItem->setText(1, nlist.at(1));
teamheadItem->setTextColor(0,QColor::QColor(255,255,0));
for(int i=2 ; i<nlist.size();i++)
{
a[d] = new QTreeWidgetItem();
treeWidget->addTopLevelItem(a[d]);
a[d]->setText(0,tr("%1(%2)")
.arg(nlist.at(i))
.arg(nlist.at(i+1)));
a[d]->setText(1,nlist.at(i+1));
i++;
d++;
}
}
//槽函数,设置字体颜色
void Team::setFontColor()
{
QColor col = QColorDialog::getColor(textEdit_2->textColor(), this);
if (!col.isValid())
{
return;
}
textEdit_2->setTextColor(col);

QPixmap pix(16, 16);
pix.fill(col);
toolButton_Font_5->setIcon(pix);
this->setStatusTip(tr("set FontColor"));
}
//槽函数,设置输入字体为粗体
void Team::setBoldFont(bool flag)
{
if(flag == true)
{
textEdit_2->setFontWeight(QFont::Bold);
}
else
textEdit_2->setFontWeight(QFont::Normal);
this->setStatusTip(tr("set BoldFont"));
}
//槽函数,设置字体大小+选择字体
void Team::setFontxx()
{
bool ok;
QFont font = QFontDialog::getFont(&ok, QFont("Times", 12), this);
if (!ok)
{
return;
}
textEdit_2->setFont(font);
this->setStatusTip(tr("set FontSize"));
}
//槽函数,添加表情图片
/*
void Team::showEmotionSlot()
{

ssselectface->show();
this->setStatusTip(tr("add Facepic"));
}*/
//槽函数,快速抓屏
void Team::windowshot()
{
screenshot = QPixmap::grabWindow(QApplication::desktop()->winId(),0,0,QApplication::desktop()->width(),QApplication::desktop()->height());
QString format = "png";
QString picName = QFileDialog::getSaveFileName(this, tr("Save As"),
"temp-image/",
tr("%1 Files (*.%2);;All Files (*)")
.arg(format.toUpper())
.arg(format));


if (!picName.isEmpty())
screenshot.save(picName, format.toAscii());
textEdit_2->append("<img src='temp-image/" + QFileInfo(picName).fileName() + "' />");

this->setStatusTip(tr("windowshot"));

}
//槽函数,把图片放入Textedit内
/*
void Team::showfaceSlot(QString pic )
{
textEdit_2->append("<img src='"+pic+"' />");
}*/
void Team::requestconnectSlot()
{
clientsocket->abort();
clientsocket->connectToHost(address,tcpport);
this->setStatusTip(tr("connect Ready"));
}
void Team::sendMessageSlot()
{
this->setStatusTip(tr(" send a message"));
writeDate(6);
}
void Team::writeDate(int flag)
{
QDataStream out;
out.setVersion(QDataStream::Qt_4_0);
out.setDevice(clientsocket);
int groupid = id;
int useid = userid;
QString kname = sname;
out << flag << useid;
if(flag == 6)
{out << groupid << kname;
out << textEdit_2->toHtml();
}

textEdit_2->clear();
}
void Team::messagefirmx(int xid,QString xmessage,QString sayname)
{
if(id == xid)
{
textEdit->append( "/n[ " + QDateTime::currentDateTime().toString(tr("yyyy-MM-dd hh:mm:ss"))
+ " ] " + sayname + tr(" say:/n/t") );
textEdit->append(xmessage);
}
}
void Team::closeEvent(QCloseEvent *event)
{
emit closebutton(id);
event->accept();
}

《转 为亲身验证 分析代码没有问题》

你可能感兴趣的:(qt)