-
Notifications
You must be signed in to change notification settings - Fork 0
/
connectdialog.cpp
71 lines (62 loc) · 1.51 KB
/
connectdialog.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "connectdialog.h"
#include "ui_connectdialog.h"
#include <regex>
#include <QMessageBox>
connectDialog::connectDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::connectDialog)
{
ui->setupUi(this);
}
connectDialog::~connectDialog()
{
delete ui;
if(!hasConnected){
delete readWriteSocket;
}
}
QHostAddress connectDialog::getAddress()
{
return QHostAddress(ui->lineEdit->text());
}
quint16 connectDialog::getPort()
{
return ui->lineEdit_2->text().toUShort();
}
QTcpSocket *connectDialog::getReadWriteSocket()
{
return readWriteSocket;
}
bool connectDialog::ipOk()
{
QString ip = ui->lineEdit->text();
std::regex r("[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+");
std::string s = ip.toStdString();
if(std::regex_match(s, r)){
return true;
}
else{
return false;
}
}
void connectDialog::acceptConnection(){
hasConnected = true;
return QDialog::accept();
}
void connectDialog::accept()
{
//int a = 0;
if(readWriteSocket == nullptr){
if(!ipOk()){
QMessageBox::warning(this, "error", "Your input is wrong!");
return;
}
ui->lineEdit->setVisible(false);
ui->lineEdit_2->setVisible(false);
ui->label->setVisible(false);
ui->label_2->setVisible(false);
this->readWriteSocket = new QTcpSocket;
QObject::connect(readWriteSocket,SIGNAL(connected()),this,SLOT(acceptConnection()));
this->readWriteSocket->connectToHost(getAddress(),getPort());
}
}