My dedicated QQ functions (1)

Source: Internet
Author: User
Begin

Previously, I only summarized the UI processing of transparent, borderless, and movable windows. In order to provide some learning materials to some students, I would like to summarize some functional points.

Principle: less nonsense and more code.

 

Login window

Log on to tcpsocket. If you have to ask why I am not a UDP socket, I can only say that TCP is reliable.

 

Login

After setting the IP port, use qdatastream to write qiodevice

Void login: on_loginbutton () {usrname = UI-> usrnamelineedit-> text (). trimmed (); Password = UI-> passwordlineedit-> text (). trimmed (); qregexp rx ("^ [1-9] {} [0-9] {} {1} quot;); Rx. setpatternsyntax (qregexp: Regexp); If (! Rx. exactmatch (usrname) {qmessagebox: Warning (null, TR ("prompt"), TR ("Enter QQ number. ");} 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 );}}

 

Logon feedback

Void login: on_read () {qbytearray block = tcpsocket-> readall (); qdatastream in (& Block, qiodevice: readonly); // qdatastream in (tcpsocket ); quint16 implements ramsize; qstring msgtype; In> extends ramsize> msgtype; If ("msg_id_notexist" = msgtype) {qmessagebox: Warning (null, TR ("prompt "), TR ("this number does not exist. Please register first. "); UI-> usrnamelineedit-> clear (); UI-> passwordlineedit-> clear ();} else if (" msg_pwd_error "= msgtype) {qmessagebox :: information (null, TR ("prompt"), TR ("Incorrect password. "); UI-> usrnamelineedit-> clear ();} else if (" msg_login_already "= msgtype) {qmessagebox: Information (null, TR (" prompt "), TR ("Please do not log on again. "); 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 ();}

Listener

 

First, TCP Server

void TcpSockServer::incomingConnection(int socketDescriptor){    TcpConThread *thread = new TcpConThread(socketDescriptor, this);    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));    thread->start();}

Window Construction

Daemon: daemon (qwidget * parent): qmainwindow (parent), UI (new UI: Daemon) {UI-> setupui (this ); this-> setwindowtitle ("QQ"); UI-> startlistenbutton-> settext ("Start listening"); UI-> iplineedit-> setenabled (true ); UI-> portlineedit-> setenabled (true); IP. clear (); Port. clear (); DB = new sqlitedb; tableviewrefresh ();}

Refresh of data information

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 ("nickname"); mymodel-> setheaderdata (2, QT: horizontal, TR ("status ")); UI-> tableview-> setmodel (mymodel); UI-> tableview-> setcolumnwidth (0, 71); UI-> tableview-> setcolumnwidth (1, 71 ); UI-> tableview-> setcolumnwidth (2, 71); UI-> tableview-> show (); DB-> closedb ();}

System broadcast and chat 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();}

 

 

 

Author: yiyaaixuexi published on 1:45:27 Original article link Read: 13191 comment: 194 view comment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.