? Start
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.
? Logon 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] {1, 2} [0-9] {4, 7} {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 dataGramSize;
QString msgType;
In> dataGramSize> 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 ();
}
? Server listening
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 ();
}