Next
? Friend list
Related initialization, 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 ()));
}
List display
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 ();
}
? Chat Window
Display and processing of chat Information
Void chatform: displayText (QString nickname, QString usrname, QString text)
{
QListWidgetItem * displayItem = new QListWidgetItem (nickname + "(" + usrname + "): \ n" + text + "\ n ");
Ui-> listWidget-> addItem (displayItem );
}
Sending Processing
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 ();
}