1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| #include "widget.h" #include "ui_widget.h" #include <QMessageBox> #include"chat.h" Widget::Widget(QWidget *parent) : QWidget(parent) , ui(new Ui::Widget) { ui->setupUi(this); socket=new QTcpSocket; }
Widget::~Widget() { delete ui; }
void Widget::on_cancelButton_clicked() { this->close(); }
void Widget::on_connectButton_clicked() { QString IP=ui->ipLine->text(); QString port=ui->portLine->text(); socket->connectToHost(QHostAddress(IP),port.toShort());
connect(socket,&QTcpSocket::connected,[this](){ QMessageBox::information(this,"连接提示","连接服务器成功"); this->hide(); Chat *C=new Chat(socket); C->show(); });
connect(socket,&QTcpSocket::disconnected,[this](){ QMessageBox::warning(this,"连接提示","连接异常,网络断开"); });
}
|