没有合适的资源?快使用搜索试试~ 我知道了~
linux下即时通讯软件
5星 · 超过95%的资源 需积分: 29 26 下载量 160 浏览量
2013-09-01
21:32:16
上传
评论 2
收藏 129KB DOC 举报
温馨提示
试读
19页
这是一个用c++开发的在linux局域网下的简单的聊天工具。
资源推荐
资源详情
资源评论
Main
#include <QtGui/QApplication>
#include "widget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
#if defined(Q_WS_S60)
w.showMaximized();
#else
w.show();
#endif
return a.exec();
}
Widget
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->resize(850,550);
ui->textEdit->setFocusPolicy(Qt::StrongFocus);
ui->textBrowser->setFocusPolicy(Qt::NoFocus);
ui->textEdit->setFocus();
ui->textEdit->installEventFilter(this);
udpSocket = new QUdpSocket(this);
port = 45454;
udpSocket->bind(port,QUdpSocket::ShareAddress
| QUdpSocket::ReuseAddressHint);
connect(udpSocket,SIGNAL(readyRead()),this,SLOT(processPendingDatagrams()));
sendMessage(NewParticipant);
server = new TcpServer(this);
connect(server,SIGNAL(sendFileName(QString)),this,SLOT(sentFileName(QString)));
connect(ui-
>textEdit,SIGNAL(currentCharFormatChanged(QTextCharFormat)),this,SLOT(currentFormatCh
anged(const QTextCharFormat)));
}
void Widget::currentFormatChanged(const QTextCharFormat &format)
{//当编辑器的字体格式改变时,我们让部件状态也随之改变
ui->fontComboBox->setCurrentFont(format.font());
if(format.fontPointSize()<9) //如果字体大小出错,因为我们最小的字体为 9
ui->fontsizecomboBox->setCurrentIndex(3); //即显示 12
else ui->fontsizecomboBox->setCurrentIndex(
ui->fontsizecomboBox->findText(QString::number(format.fontPointSize())));
ui->textbold->setChecked(format.font().bold());
ui->textitalic->setChecked(format.font().italic());
ui->textUnderline->setChecked(format.font().underline());
color = format.foreground().color();
}
void Widget::processPendingDatagrams() //接收数据 UDP
{
while(udpSocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpSocket->pendingDatagramSize());
udpSocket->readDatagram(datagram.data(),datagram.size());
QDataStream in(&datagram,QIODevice::ReadOnly);
int messageType;
in >> messageType;
QString userName,localHostName,ipAddress,message;
QString time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss");
switch(messageType)
{
case Message:
{
in >>userName >>localHostName >>ipAddress >>message;
ui->textBrowser->setTextColor(Qt::blue);
ui->textBrowser->setCurrentFont(QFont("Times New Roman",12));
ui->textBrowser->append("[ " +userName+" ] "+ time);
ui->textBrowser->append(message);
break;
}
case NewParticipant:
{
in >>userName >>localHostName >>ipAddress;
newParticipant(userName,localHostName,ipAddress);
break;
}
case ParticipantLeft:
{
in >>userName >>localHostName;
participantLeft(userName,localHostName,time);
break;
}
case FileName:
{
in >>userName >>localHostName >> ipAddress;
QString clientAddress,fileName;
in >> clientAddress >> fileName;
hasPendingFile(userName,ipAddress,clientAddress,fileName);
break;
}
case Refuse:
{
in >> userName >> localHostName;
QString serverAddress;
in >> serverAddress;
QString ipAddress = getIP();
if(ipAddress == serverAddress)
{
server->refused();
}
break;
}
}
}
}
//处理新用户加入
void Widget::newParticipant(QString userName,QString localHostName,QString ipAddress)
{
bool bb = ui->tableWidget->findItems(localHostName,Qt::MatchExactly).isEmpty();
if(bb)
{
QTableWidgetItem *user = new QTableWidgetItem(userName);
剩余18页未读,继续阅读
资源评论
- 关外野妖2015-10-27不错的資料 谢谢
tobby333
- 粉丝: 0
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功