程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 我的專屬QQ功能篇 (一)

我的專屬QQ功能篇 (一)

編輯:關於C語言

 

?寫在開頭     

   之前只總結了透明、無邊框、可移動窗口的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();

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved