程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 我的專屬QQ功能篇(二)

我的專屬QQ功能篇(二)

編輯:關於C語言

 

接上篇

 

 

 

?好友列表

\

 

 

 

 

 

 

相關初始化,  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();

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved