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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
package Sender;
use strict;
use warnings;
use QtCore4;
use QtGui4;
use QtCore4::isa qw( Qt::Dialog );
use QtCore4::slots
startBroadcasting => [],
broadcastDatagram => [];
sub statusLabel() {
return this->{statusLabel};
}
sub startButton() {
return this->{startButton};
}
sub quitButton() {
return this->{quitButton};
}
sub buttonBox() {
return this->{buttonBox};
}
sub udpSocket() {
return this->{udpSocket};
}
sub timer() {
return this->{timer};
}
sub messageNo() {
return this->{messageNo};
}
sub NEW
{
my ($class, $parent) = @_;
$class->SUPER::NEW($parent);
this->{statusLabel} = Qt::Label(this->tr('Ready to broadcast datagrams on port 45454'));
this->{startButton} = Qt::PushButton(this->tr('&Start'));
this->{quitButton} = Qt::PushButton(this->tr('&Quit'));
this->{buttonBox} = Qt::DialogButtonBox();
this->buttonBox->addButton(this->startButton, Qt::DialogButtonBox::ActionRole());
this->buttonBox->addButton(this->quitButton, Qt::DialogButtonBox::RejectRole());
this->{timer} = Qt::Timer(this);
# [0]
this->{udpSocket} = Qt::UdpSocket(this);
# [0]
this->{messageNo} = 1;
this->connect(this->startButton, SIGNAL 'clicked()', this, SLOT 'startBroadcasting()');
this->connect(this->quitButton, SIGNAL 'clicked()', this, SLOT 'close()');
this->connect(this->timer, SIGNAL 'timeout()', this, SLOT 'broadcastDatagram()');
my $mainLayout = Qt::VBoxLayout();
$mainLayout->addWidget(this->statusLabel);
$mainLayout->addWidget(this->buttonBox);
this->setLayout($mainLayout);
this->setWindowTitle(this->tr('Broadcast Sender'));
}
sub startBroadcasting
{
this->startButton->setEnabled(0);
timer->start(1000);
}
sub broadcastDatagram
{
this->statusLabel->setText(sprintf this->tr('Now broadcasting datagram %s'), this->messageNo);
# [1]
my $datagram = Qt::ByteArray('Broadcast message ' . this->messageNo);
udpSocket->writeDatagram($datagram->data(), $datagram->size(),
Qt::HostAddress(Qt::HostAddress::Broadcast()), 45454);
# [1]
++this->{messageNo};
}
1;
|