void Client::clientSelected(QListWidgetItem* item) { clientSelected(item->text()); }
ChessSelectionWidget::ChessSelectionWidget(QWidget *parent) : QWidget(parent) { //创建三个选项:服务器、客户端、复盘 // 服务器 serverRadioButton = new QRadioButton(QString::fromUtf8("\xe6\x9c\x8d\xe5\x8a\xa1\xe5\x99\xa8")); // 客户端 clientRadioButton = new QRadioButton(QString::fromUtf8("\xe5\xae\xa2\xe6\x88\xb7\xe7\xab\xaf")); // 复盘 replayRadioButton = new QRadioButton(QString::fromUtf8("\xe5\xa4\x8d\xe7\x9b\x98")); // 人机对战 AIRadioButton = new QRadioButton(QString::fromUtf8("\xe4\xba\xba\xe6\x9c\xba\xe5\xaf\xb9\xe6\x88\x98")); AIRadioButton->setChecked(true); //创建IP地址输入框,默认值为127.0.0.1 QLatin1String reg("^((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-4]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))$"); ipLineEdit = new QLineEdit; QRegExpValidator *validator = new QRegExpValidator(QRegExp(reg), this); ipLineEdit->setValidator(validator); ipLineEdit->setDisabled(true); ipLineEdit->setText(QLatin1String("127.0.0.1")); // IP: ipLabel = new QLabel(QString::fromUtf8("\x49\x50\xef\xbc\x9a")); ipLabel->setBuddy(ipLineEdit); //创建端口号输入框,默认值为12345 portSpinBox = new QSpinBox; portSpinBox->setToolTip(QLatin1String("10000~65535")); portSpinBox->setRange(10000, 65535); portSpinBox->setValue(12345); portSpinBox->setDisabled(true); // 端口: portLabel = new QLabel(QString::fromUtf8("\xe7\xab\xaf\xe5\x8f\xa3\xef\xbc\x9a")); portLabel->setBuddy(portSpinBox); //创建“确定”按钮 okPushButton = new QPushButton(QString::fromUtf8("\xe7\xa1\xae\xe5\xae\x9a")); //界面布局:选项竖排在左边,输入框在右边,“确定”按钮在右下角 QHBoxLayout *hBoxLayout1 = new QHBoxLayout; hBoxLayout1->addWidget(serverRadioButton); hBoxLayout1->addWidget(clientRadioButton); hBoxLayout1->addWidget(replayRadioButton); hBoxLayout1->addWidget(AIRadioButton); QHBoxLayout *hBoxLayout2 = new QHBoxLayout; hBoxLayout2->addWidget(ipLabel); hBoxLayout2->addWidget(ipLineEdit); hBoxLayout2->addWidget(portLabel); hBoxLayout2->addWidget(portSpinBox); QHBoxLayout *hBoxLayout3 = new QHBoxLayout; hBoxLayout3->addStretch(); hBoxLayout3->addWidget(okPushButton); QVBoxLayout *layout = new QVBoxLayout; layout->addSpacing(15); layout->addLayout(hBoxLayout1); layout->addSpacing(15); layout->addLayout(hBoxLayout2); layout->addSpacing(15); layout->addLayout(hBoxLayout3); setLayout(layout); //创建信号和槽联系 connect(serverRadioButton, SIGNAL(clicked()), this, SLOT(serverSelected())); connect(clientRadioButton, SIGNAL(clicked()), this, SLOT(clientSelected())); connect(replayRadioButton, SIGNAL(clicked()), this, SLOT(replaySelected())); connect(AIRadioButton, SIGNAL(clicked()), this, SLOT(AISelected())); connect(okPushButton, SIGNAL(clicked()), this, SLOT(okPushButtonClicked())); //固定界面大小 setFixedSize(sizeHint()); //设置界面标题“中国象棋” setWindowTitle(QString::fromUtf8("\xe4\xb8\xad\xe5\x9b\xbd\xe8\xb1\xa1\xe6\xa3\x8b")); Chess_Trace(tr("build ChessSelectionWidget")); Chess_Info(tr("please select chess type and input ip or port")); }
void Client::messageComing() { QDataStream in(secureSocket); in.setVersion(QDataStream::Qt_4_0); do { qDebug() << "message coming"; if (blockSize == 0) { if (secureSocket->bytesAvailable() < (int)sizeof(quint16)) return; in >> blockSize; } qDebug() << "\tblocksize: " << blockSize << "\tavailable: " << secureSocket->bytesAvailable(); if (secureSocket->bytesAvailable() < blockSize) return; QString nextMessage; in >> nextMessage; qDebug() << "\tmessage: " << nextMessage; blockSize = 0; currentMessage = nextMessage; if (currentMessage == "connected") { connectButton->setEnabled(false); hostLineEdit->setEnabled(false); portLineEdit->setEnabled(false); clientNameLineEdit->setEnabled(false); userList.setWindowTitle(tr("User list of %1").arg(clientNameLineEdit->text())); chatLog.setWindowTitle(tr("Chat log of %1").arg(clientNameLineEdit->text())); layout()->addWidget(&chatLog); layout()->addWidget(&userList); chatLog.setReadOnly(true); } else if (currentMessage.startsWith("$reconnect")) { connectButton->setEnabled(true); hostLineEdit->setEnabled(true); portLineEdit->setEnabled(true); clientNameLineEdit->setEnabled(true); } else if (currentMessage.startsWith("$addclient ")) { userList.addItem(currentMessage.right(currentMessage.length() - tr("$addclient ").length())); } else if (currentMessage.startsWith("$delclient ")) { QString name = currentMessage.right(currentMessage.length() - tr("$delclient ").length()); foreach (QListWidgetItem* item, userList.findItems(name, 0)) userList.takeItem(userList.row(item)); } else if (currentMessage.startsWith("$open ")) { clientSelected(currentMessage.right(currentMessage.length() - tr("$open ").length())); } else if (currentMessage.contains(QRegExp("^\\S+>"))) { clientChatLog.appendPlainText(currentMessage); } else chatLog.appendPlainText(currentMessage); qDebug() << "bytes in queue: " << secureSocket->bytesAvailable(); } while (secureSocket->bytesAvailable() > 0); }