我的专属QQ 功能篇 (一)

✿写在开头

之前只总结了透明、无边框、可移动窗口的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(); }


窗口构造

Daemon::Daemon(QWidget *parent) : QMainWindow(parent), ui(new Ui::Daemon) { ui->setupUi(this); this->setWindowTitle("QQ"); ui->startListenButton->setText("开始监听"); ui->ipLineEdit->setEnabled(true); ui->portLineEdit->setEnabled(true); ip.clear(); port.clear(); db = new SqliteDB; tableViewRefresh(); }


数据信息的refresh

void Daemon::tableViewRefresh() { db->connectDB(); this->myModel = new MySqlQueryModel; this->myModel->setQuery(QObject::tr("select id, name, logstat from user order by logstat desc")); myModel->setHeaderData(0, Qt::Horizontal, tr("QQ号")); myModel->setHeaderData(1, Qt::Horizontal, tr("昵称")); myModel->setHeaderData(2, Qt::Horizontal, tr("状态")); ui->tableView->setModel(myModel); ui->tableView->setColumnWidth(0, 71); ui->tableView->setColumnWidth(1, 71); ui->tableView->setColumnWidth(2, 71); ui->tableView->show(); db->closeDB(); }


系统广播和聊天UDP即可

void Daemon::on_send() { QByteArray sysMsg; QDataStream tosend(&sysMsg,QIODevice::WriteOnly); tosend.setVersion(QDataStream::Qt_4_6); QString mytype="MSG_SERVER_INFO"; tosend<<(quint16)0<<mytype<<ui->servTextEdit->toPlainText(); tosend.device()->seek(0); tosend<<(quint16)(sysMsg.size()-sizeof(quint16)); if(!udpSocket->writeDatagram(sysMsg.data(),sysMsg.size(),QHostAddress("192.168.1.255"),6666)) QMessageBox::warning(NULL,"message broadcast","error"); ui->servTextEdit->clear(); }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值