-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
74 lines (65 loc) · 1.86 KB
/
mainwindow.cpp
File metadata and controls
74 lines (65 loc) · 1.86 KB
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
72
73
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTime>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowTitle("Client's dialog");
socket = new QTcpSocket(this);
connect(socket,SIGNAL(disconnected()),this,SLOT(deleteLater()));
connect(ui->pushButton_connect,SIGNAL(clicked()),SLOT(connectServer()));
connect(ui->pushButton_send,SIGNAL(clicked()),SLOT(sendToServer()));
ui->lineEdit_message->setEnabled(false);
ui->pushButton_send->setEnabled(false);
}
void MainWindow::connectServer()
{
socket->connectToHost("localhost",12);
if(socket->waitForConnected(5000))
{
QString con = "***CONNECTED***";
ui->textBrowser_main->append(con);
ui->pushButton_connect->setEnabled(false);
ui->pushButton_send->setEnabled(true);
ui->lineEdit_message->setEnabled(true);
ui->lineEdit_message->clear();
}
else
{
QString str = "***Not connected***";
ui->textBrowser_main->append(str);
qDebug() << socket->errorString() << socket->state();
}
}
void MainWindow::sendToServer()
{
data.clear();
bool flag = true;
QImage img(ui->lineEdit_message->text());
if(img.isNull())
{
QString str = QTime::currentTime().toString() + ": The image is invalid";
ui->textBrowser_main->append(str);
flag = false;
}
else
{
QString str = QTime::currentTime().toString() + ": The image was sent";
ui->textBrowser_main->append(str);
}
QDataStream out(&data,QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_6_0);
out << img << flag;
socket->write(data);
ui->lineEdit_message->clear();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_lineEdit_message_returnPressed()
{
sendToServer();
}