接上篇
?好友列表
相關初始化, UDP
void panel::init()
{
udpSocket = new QUdpSocket(this);
udpSocket->bind(6666);
QString msgType = "MSG_CLIENT_NEW_CONN";
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);
out << (quint16)0 << msgType << usrname;
out.device()->seek(0);
udpSocket->writeDatagram(block.data(), block.size(), QHostAddress(ip), (quint16)port.toUInt()+1);
connect(this->udpSocket, SIGNAL(readyRead()), this, SLOT(recvMsg()));
}
列表顯示
void panel::on_itemDoubleClicked(QListWidgetItem* item)
{
QString nameStr = ui->usrlistWidget->currentItem()->text();
nameStr.replace("\n","");
chatform *c = chatformHash.value(nameStr);
if(c == 0)
{
c = new chatform(this->usrname,this->ip,this->port, udpSocket);
c->setWindowTitle("chatting with " + nameStr + ".");
chatformHash.insert(nameStr,c);//??
}
c->setWindowFlags(Qt::FramelessWindowHint);
c->setAttribute(Qt::WA_TranslucentBackground);
c->show();
}
?聊天窗口
聊天信息的顯示處理
void chatform::displayText(QString nickname, QString usrname, QString text)
{
QListWidgetItem *displayItem = new QListWidgetItem(nickname + "(" + usrname + ") :\n" + text + "\n");
ui->listWidget->addItem(displayItem);
}
發送處理
void chatform::on_sendButton()
{
QString sendText = ui->textEdit->toPlainText();
if(!sendText.isEmpty())
{
QString windowTitle = this->windowTitle().replace("].","");
QString toId = QString(windowTitle.split("[").at(1));
QString msgType = "MSG_CLIENT_CHAT";
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_6);
out << (quint16)0 << msgType << usrname << toId << sendText;
out.device()->seek(0);
out << (quint16)(block.size() - sizeof(quint16));
udpSocket->writeDatagram(block.data(), block.size(), QHostAddress(serverIp), (quint16)serverPort.toUInt()+1);
ui->listWidget->addItem("I say :\n" + sendText + "\n");
}
ui->textEdit->clear();
}