打造您自己的专属QQ【附源码】


  ?写在开头     

   之前只总结了透明、无边框、可移动窗口的UI处理,为了给某位同学提供些学习资料,我再总结些功能要点。

   原则:少说废话,多上代码。

 

  ?登录窗口

    登录操作TcpSocket,如果你非要问我为什么不是UDP Socket ,我只能说因为tcp可靠偷笑

      

?登录

在确保设置IP端口后,通过QDataStream 写 QIODevice

  1. void login::on_loginButton()  
  2. {  
  3.          usrname = ui->usrnamelineEdit->text().trimmed();  
  4.          password = ui->passwordlineEdit->text().trimmed();  
  5.   
  6.   
  7.         QRegExp rx("^[1-9]{1,2}[0-9]{4,7}{1}quot;);  
  8.         rx.setPatternSyntax(QRegExp::RegExp);  
  9.         if (!rx.exactMatch(usrname))  
  10.         {  
  11.             QMessageBox::warning(NULL, tr("提示"), tr("请输入QQ号."));  
  12.         }  
  13.         else  
  14.         {  
  15.             tcpSocket->abort();  
  16.             tcpSocket->connectToHost(QHostAddress(ip), (quint16)port.toUInt());  
  17.             QString msgType = "MSG_USER_LOGIN";  
  18.             QByteArray block;  
  19.             QDataStream out(&block, QIODevice::WriteOnly);  
  20.             out.setVersion(QDataStream::Qt_4_6);  
  21.             out << (quint16)0 << msgType << usrname << password;  
  22.             out.device()->seek(0);  
  23.             out << (quint16)(block.size() - sizeof(quint16));  
  24.             tcpSocket->write(block);  
  25.         }  
  26.      
  27. }  

登录反馈

  1. void login::on_Read()  
  2. {  
  3.     QByteArray block = tcpSocket->readAll();  
  4.     QDataStream in(&block, QIODevice::ReadOnly);     //QDataStream in(tcpSocket);   
  5.     quint16 dataGramSize;  
  6.     QString msgType;  
  7.     in >> dataGramSize >> msgType;  
  8.     if ( "MSG_ID_NOTEXIST" == msgType )  
  9.    {  
  10.        QMessageBox::warning(NULL, tr("提示"), tr("该号码不存在,请先注册."));  
  11.        ui->usrnamelineEdit->clear();  
  12.        ui->passwordlineEdit->clear();  
  13.    }  
  14.     else if ( "MSG_PWD_ERROR" == msgType )  
  15.     {  
  16.            QMessageBox::information(NULL, tr("提示"), tr("密码错误."));  
  17.            ui->usrnamelineEdit->clear();  
  18.     }  
  19.     else if ( "MSG_LOGIN_ALREADY" == msgType )  
  20.     {  
  21.            QMessageBox::information(NULL, tr("提示"), tr("请不要重复登录."));  
  22.            ui->usrnamelineEdit->clear();  
  23.            ui->passwordlineEdit->clear();  
  24.     }  
  25.     else if ( "MSG_LOGIN_SUCCESS" == msgType)  
  26.     {  
  27.             qqpanel = new panel(usrname, ip, port);  
  28.             qqpanel->setWindowTitle(tr("QQcopy"));  
  29.             qqpanel->setWindowFlags(Qt::FramelessWindowHint);  
  30.             qqpanel->setAttribute(Qt::WA_TranslucentBackground);  
  31.             qqpanel->show();  
  32.             this->close();  
  33.     }  

?服务器端的监听

   

首先Tcp server

  1. void TcpSockServer::incomingConnection(int socketDescriptor)  
  2. {  
  3.     TcpConThread *thread = new TcpConThread(socketDescriptor, this);  
  4.     connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));  
  5.     thread->start();  
  6. }  

窗口构造

  1. Daemon::Daemon(QWidget *parent) :  
  2.     QMainWindow(parent),  
  3.     ui(new Ui::Daemon)  
  4. {  
  5.     ui->setupUi(this);  
  6.     this->setWindowTitle("QQ");  
  7.     ui->startListenButton->setText("开始监听");  
  8.     ui->ipLineEdit->setEnabled(true);  
  9.     ui->portLineEdit->setEnabled(true);  
  10.   
  11.     ip.clear();  
  12.     port.clear();  
  13.     db = new SqliteDB;  
  14.     tableViewRefresh();  
  15.   
  16. }  

数据信息的refresh

  1. void Daemon::tableViewRefresh()  
  2. {  
  3.     db->connectDB();  
  4.   
  5.     this->myModel = new MySqlQueryModel;  
  6.     this->myModel->setQuery(QObject::tr("select id, name, logstat from user order by logstat desc"));  
  7.     myModel->setHeaderData(0, Qt::Horizontal, tr("QQ号"));  
  8.     myModel->setHeaderData(1, Qt::Horizontal, tr("昵称"));  
  9.     myModel->setHeaderData(2, Qt::Horizontal, tr("状态"));  
  10.   
  11.     ui->tableView->setModel(myModel);  
  12.     ui->tableView->setColumnWidth(0, 71);  
  13.     ui->tableView->setColumnWidth(1, 71);  
  14.     ui->tableView->setColumnWidth(2, 71);  
  15.     ui->tableView->show();  
  16.   
  17.   
  18.     db->closeDB();  
  19. }  

系统广播和聊天UDP即可

  1. void Daemon::on_send()  
  2. {  
  3.     QByteArray sysMsg;  
  4.     QDataStream tosend(&sysMsg,QIODevice::WriteOnly);  
  5.     tosend.setVersion(QDataStream::Qt_4_6);  
  6.     QString mytype="MSG_SERVER_INFO";  
  7.     tosend<<(quint16)0<<mytype<<ui->servTextEdit->toPlainText();  
  8.     tosend.device()->seek(0);  
  9.     tosend<<(quint16)(sysMsg.size()-sizeof(quint16));  
  10.     if(!udpSocket->writeDatagram(sysMsg.data(),sysMsg.size(),QHostAddress("192.168.1.255"),6666))  
  11.     QMessageBox::warning(NULL,"message broadcast","error");  
  12.     ui->servTextEdit->clear();  
  13. }  
  • 1
  • 2
  • 3
  • 下一页
【内容导航】
第1页:功能篇(一) 第2页:功能篇(二)
第3页:附源码

相关内容