✿写在开头
之前只总结了透明、无边框、可移动窗口的UI处理,为了给某位同学提供些学习资料,我再总结些功能要点。
原则:少说废话,多上代码。
✿登录窗口
登录操作TcpSocket,如果你非要问我为什么不是UDP Socket ,我只能说因为tcp可靠。
❀登录
在确保设置IP端口后,通过QDataStream写 QIODevice
void login::on_loginButton() { usrname = ui->usrnamelineEdit->text().trimmed(); password = ui->passwordlineEdit->text().trimmed(); QRegExp rx("^[1-9]{1,2}[0-9]{4,7}{1}quot;); rx.setPatternSyntax(QRegExp::RegExp); if (!rx.exactMatch(usrname)) { QMessageBox::warning(NULL, tr("提示"), tr("请输入QQ号.")); } else { tcpSocket->abort(); tcpSocket->connectToHost(QHostAddress(ip), (quint16)port.toUInt()); QString msgType = "MSG_USER_LOGIN"; QByteArray block; QDataStream out(&block, QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_4_6); out << (quint16)0 << msgType << usrname << password; out.device()->seek(0); out << (quint16)(block.size() - sizeof(quint16)); tcpSocket->write(block); } }
登录反馈
void login::on_Read() { QByteArray block = tcpSocket->readAll(); QDataStream in(&block, QIODevice::ReadOnly); //QDataStream in(tcpSocket); quint16 dataGramSize; QString msgType; in >> dataGramSize >> msgType; if ( "MSG_ID_NOTEXIST" == msgType ) { QMessageBox::warning(NULL, tr("提示"), tr("该号码不存在,请先注册.")); ui->usrnamelineEdit->clear(); ui->passwordlineEdit->clear(); } else if ( "MSG_PWD_ERROR" == msgType ) { QMessageBox::information(NULL, tr("提示"), tr("密码错误.")); ui->usrnamelineEdit->clear(); } else if ( "MSG_LOGIN_ALREADY" == msgType ) { QMessageBox::information(NULL, tr("提示"), tr("请不要重复登录.")); ui->usrnamelineEdit->clear(); ui->passwordlineEdit->clear(); } else if ( "MSG_LOGIN_SUCCESS" == msgType) { qqpanel = new panel(usrname, ip, port); qqpanel->setWindowTitle(tr("QQcopy")); qqpanel->setWindowFlags(Qt::FramelessWindowHint); qqpanel->setAttribute(Qt::WA_TranslucentBackground); qqpanel->show(); this->close(); }
❀服务器端的监听
首先Tcp server
void TcpSockServer::incomingConnection(int socketDescriptor) { TcpConThread *thread = new TcpConThread(socketDescriptor, this); connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater())); thread->start(); }
窗口构造
数据信息的refresh
系统广播和聊天UDP即可