For qt4-qtruby-2.0.3, the network/broadcastreceiver example is broken. The problem seems to be that Qt::ByteArray#data
returns a String which gets passed to the reader method (e.g. Qt::UdpSocket#readDatagram) which modifies the String
object by storing the read data in it, but the modified String contents never get back into the ByteArray object.
For example, broadcastreceiver/receiver.rb contains this method...
def processPendingDatagrams()
while @udpSocket.hasPendingDatagrams do
datagram = Qt::ByteArray.new
datagram.resize(@udpSocket.pendingDatagramSize)
@udpSocket.readDatagram(datagram.data(), datagram.size())
@statusLabel.text = tr('Received datagram: "%d"' % datagram.data)
end
end
...which does not work as expected. Changing datagram to be a String makes it work...
def processPendingDatagrams()
while @udpSocket.hasPendingDatagrams do
datagram = ' ' * @udpSocket.pendingDatagramSize
@udpSocket.readDatagram(datagram, datagram.size)
@statusLabel.text = tr('Received datagram: %s') % datagram.inspect
end
end
Note also the changes in the @statusLabel.text line:
1. "%d" -> %s
2. run tr() on the format string before formatting (seems like it would give tr a better chance)
3. uses String#inspect to ensure text is readable even if it contains non-printable characters.
Dave
|