-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathmainwindow.cpp
60 lines (53 loc) · 2.15 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QScrollBar>
#include <QTimer>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
udpsocket = new QUdpSocket(this);
connect(udpsocket,SIGNAL(readyRead()),this,SLOT(readyReadSlot()));
udpsocket->bind(QHostAddress::Any,8765);
//workaround for strange QUdpSocket bug where readyRead signal may not emit and never emit again filling up incoming buffer and the output seems to freeze. Can someone please do something about this?
readtimer=new QTimer(this);
connect(readtimer,SIGNAL(timeout()),this,SLOT(readyReadSlot()));
readtimer->start(50);
}
void MainWindow::readyReadSlot()
{
readtimer->stop();
while (udpsocket->hasPendingDatagrams())
{
//get packet
QByteArray datagram;
datagram.resize(udpsocket->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;
udpsocket->readDatagram(datagram.data(), datagram.size(),&sender, &senderPort);
//turn into hex
QByteArray datagramhex;for(int i=0;i<datagram.size();i++)datagramhex+=QString("%1").arg((uchar) datagram[i], 2, 16, QLatin1Char( '0' )).toUpper()+" ";
//display packet. gerr with qplaintext
const QTextCursor old_cursor = ui->plainTextEdit->textCursor();
const int old_scrollbar_value = ui->plainTextEdit->verticalScrollBar()->value();
const bool is_scrolled_down = old_scrollbar_value == ui->plainTextEdit->verticalScrollBar()->maximum();
ui->plainTextEdit->moveCursor(QTextCursor::End);
ui->plainTextEdit->textCursor().insertText(datagramhex);
if (old_cursor.hasSelection() || !is_scrolled_down)
{
ui->plainTextEdit->setTextCursor(old_cursor);
ui->plainTextEdit->verticalScrollBar()->setValue(old_scrollbar_value);
}
else
{
ui->plainTextEdit->moveCursor(QTextCursor::End);
ui->plainTextEdit->verticalScrollBar()->setValue(ui->plainTextEdit->verticalScrollBar()->maximum());
}
}
readtimer->start();
}
MainWindow::~MainWindow()
{
delete ui;
}