def run(self): self.mutex.lock() serverName = self.hostName serverPort = self.port self.mutex.unlock() while not self.quit: Timeout = 5 * 1000 socket = QTcpSocket() socket.connectToHost(serverName, serverPort) if not socket.waitForConnected(Timeout): self.error.emit(socket.error(), socket.errorString()) return while socket.bytesAvailable() < 2: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return instr = QDataStream(socket) instr.setVersion(QDataStream.Qt_4_0) blockSize = instr.readUInt16() block = QByteArray() outstr = QDataStream(block, QIODevice.WriteOnly) outstr.setVersion(QDataStream.Qt_4_0) while socket.bytesAvailable() < blockSize: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return self.mutex.lock() outstr.writeUInt16(0) outstr.writeQString("Message to Server") outstr.device().seek(0) outstr.writeUInt16(block.size() - 2) socket.write(block) # socket.write(block) fortune = instr.readQString() self.newFortune.emit(fortune) self.cond.wait(self.mutex) serverName = self.hostName serverPort = self.port self.mutex.unlock()
def dropEvent(self, event): if event.mimeData().hasFormat("application/x-lista-item"): data = event.mimeData().data("application/x-lista-item") stream = QDataStream(data, QIODevice.ReadOnly) text = stream.readQString() id = stream.readInt16() # icon = QIcon() # stream >> icon item = QListWidgetItem(text, self) item.setData(1, id) # item.setIcon(icon) event.setDropAction(Qt.MoveAction) event.accept() else: event.ignore()
def dropEvent(self, event): if event.mimeData().hasFormat("application/x-lista-item"): data = event.mimeData().data("application/x-lista-item") stream = QDataStream(data, QIODevice.ReadOnly) self._player_name = stream.readQString() self._player_id = stream.readInt16() # sor eseten p1, oszlopnál p2 for i in range(self.sorok_szama): self.parent.eredmenyek[self._csoport_number][ self._csoport_sor][i]._set_p1_id(self._player_id) self.parent.eredmenyek[self._csoport_number][i][ self._csoport_sor]._set_p2_id(self._player_id) # print("id: ", self._player_id, "csoport: ", self._get_csoport_number(), "sor: ", self._get_csoport_sor()) # icon = QIcon() # stream >> icon event.setDropAction(Qt.MoveAction) event.accept() self.update() else: event.ignore()
def run(self): self.mutex.lock() serverName = self.hostName serverPort = self.port self.mutex.unlock() while not self.quit: Timeout = 5 * 1000 socket = QTcpSocket() socket.connectToHost(serverName, serverPort) if not socket.waitForConnected(Timeout): self.error.emit(socket.error(), socket.errorString()) return while socket.bytesAvailable() < 2: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return instr = QDataStream(socket) instr.setVersion(QDataStream.Qt_4_0) blockSize = instr.readUInt16() while socket.bytesAvailable() < blockSize: if not socket.waitForReadyRead(Timeout): self.error.emit(socket.error(), socket.errorString()) return self.mutex.lock() fortune = instr.readQString() self.newFortune.emit(fortune) self.cond.wait(self.mutex) serverName = self.hostName serverPort = self.port self.mutex.unlock()
class QDataStreamShift(unittest.TestCase): '''Test case for << and >> operators''' def setUp(self): self.ba = QByteArray() self.stream = QDataStream(self.ba, QIODevice.WriteOnly) self.read_stream = QDataStream(self.ba, QIODevice.ReadOnly) def testQCharValid(self): '''QDataStream <<>> QChar - valid''' self.stream.writeQChar(42) res = self.read_stream.readQChar() self.assertEqual(res, py3k.unichr(42)) def testQCharNull(self): '''QDataStream <<>> QChar - null''' self.stream.writeQChar(None) res = self.read_stream.readQChar() self.assertEqual(res, py3k.unicode_('\x00')) def testQByteArrayValid(self): '''QDataStream <<>> QByteArray - valid''' self.stream << QByteArray("hello") res = QByteArray() self.read_stream >> res self.assertEqual(res, QByteArray("hello")) def testQByteArrayEmpty(self): '''QDataStream <<>> QByteArray - empty''' self.stream << QByteArray("") res = QByteArray() self.read_stream >> res self.assertEqual(res, QByteArray("")) self.assertTrue(res.isEmpty()) self.assertFalse(res.isNull()) def testQByteArrayNull(self): '''QDataStream <<>> QByteArray - null''' self.stream << QByteArray() res = QByteArray() self.read_stream >> res self.assertEqual(res, QByteArray()) self.assertTrue(res.isEmpty()) self.assertTrue(res.isNull()) def testQStringValid(self): '''QDataStream <<>> QString - valid''' self.stream.writeQString('Ka-boom') res = self.read_stream.readQString() self.assertEqual(res, py3k.unicode_('Ka-boom')) def testQStringEmpty(self): '''QDataStream <<>> QString - empty''' self.stream.writeQString('') res = self.read_stream.readQString() self.assertEqual(res, py3k.unicode_('')) def testQStringNull(self): '''QDataStream <<>> QString - null''' self.stream.writeQString(None) res = self.read_stream.readQString() self.assertEqual(res, py3k.unicode_('')) def testQBitArrayNull(self): '''QDataStream <<>> QBitArray - null''' self.stream << QBitArray() res = QBitArray() self.read_stream >> res self.assertEqual(res, QBitArray()) def testQBitArrayValid(self): '''QDataStream <<>> QBitArray - valid''' self.stream << create_bitarray('01010101') res = QBitArray() self.read_stream >> res self.assertEqual(res, create_bitarray('01010101')) def testQDateNull(self): '''QDataStream <<>> QDate - null''' self.stream << QDate() res = QDate() self.read_stream >> res self.assertEqual(res, QDate()) self.assertFalse(res.isValid()) self.assertTrue(res.isNull()) def testQDateValid(self): '''QDataStream <<>> QDate - valid''' self.stream << QDate(2012, 12, 21) res = QDate() self.read_stream >> res self.assertEqual(res, QDate(2012, 12, 21)) self.assertTrue(res.isValid()) self.assertFalse(res.isNull()) def testQTimeNull(self): '''QDataStream <<>> QTime - null''' self.stream << QTime() res = QTime() self.read_stream >> res self.assertEqual(res, QTime()) self.assertFalse(res.isValid()) self.assertTrue(res.isNull()) def testQTimeValid(self): '''QDataStream <<>> QTime - valid''' self.stream << QTime(12, 12, 21) res = QTime() self.read_stream >> res self.assertEqual(res, QTime(12, 12, 21)) self.assertTrue(res.isValid()) self.assertFalse(res.isNull()) def testQDateTimeNull(self): '''QDataStream <<>> QDateTime - null''' self.stream << QDateTime() res = QDateTime() self.read_stream >> res self.assertEqual(res, QDateTime()) self.assertFalse(res.isValid()) self.assertTrue(res.isNull()) def testQDateTimeValid(self): '''QDataStream <<>> QDateTime - valid''' time = QTime(23, 23, 23) date = QDate(2009, 1, 1) self.stream << QDateTime(date, time) res = QDateTime() self.read_stream >> res self.assertEqual(res, QDateTime(date, time)) self.assertTrue(res.isValid()) self.assertFalse(res.isNull())