我的专属QQ 功能篇(二)

接上篇

 

✿好友列表

我的专属QQ 功能篇(二)_第1张图片

 

 

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


 

✿聊天窗口

我的专属QQ 功能篇(二)_第2张图片

 

聊天信息的显示处理

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();
}


 

你可能感兴趣的:(c,qq,qt,聊天,Signal)