我的专属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();
}
作者:yiyaaixuexi 发表于2011-7-28 1:45:27 原文链接
阅读:13191 评论:194 查看评论
浙公网安备 33010602011771号