本文整理汇总了Python中PyQt4.QtCore.QBuffer.seek方法的典型用法代码示例。如果您正苦于以下问题:Python QBuffer.seek方法的具体用法?Python QBuffer.seek怎么用?Python QBuffer.seek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt4.QtCore.QBuffer
的用法示例。
在下文中一共展示了QBuffer.seek方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Connection
# 需要导入模块: from PyQt4.QtCore import QBuffer [as 别名]
# 或者: from PyQt4.QtCore.QBuffer import seek [as 别名]
class Connection(QTcpSocket):
"""Abstracts server-client connections.
Whenever data is received, a Connection attempts to parse a Message and arguments from the data, and emits messageReceived for each parsed message. See QTcpSocket documentation for other members."""
messageSent = pyqtSignal(int, list)
"""Emitted whenever a message is sent. Arguments: the message type, list of arguments."""
messageReceived = pyqtSignal(int, list)
"""Emitted whenever a message is recieved. Arguments: the message type, list of arguments."""
messageReceived2 = pyqtSignal()
def __init__(self, id = None, client = False, parent = None):
"""Creates a connection.
id -- Optional socket descriptor.
client -- Whether this connection is opened from the client side or server side.
parent -- Parent object."""
QTcpSocket.__init__(self, parent)
if id:
if not self.setSocketDescriptor(id):
self.done()
return
self.id = id
self.player = None
self.client = client
self.buffer = QBuffer()
self.buffer.open(QBuffer.ReadWrite)
self.readyRead.connect(self._readIncomingData)
def abort(self):
"""Aborts the connection."""
super(Connection, self).abort()
def _readIncomingData(self):
bytesWritten = self.buffer.write(self.readAll())
self.buffer.seek(0)
result = self._parse()
bytesRead = 0
while result:
bytesRead += result[2]
msg, args = result[:2]
if self.client:
log.debug("Received %s %s", msg, args)
elif self.player and self.player.name:
log.debug("Received %s %s from %s", msg, args, self.player)
else:
log.debug("Received %s %s from %s", msg, args, self.peerAddress().toString())
self.messageReceived.emit(msg, args)
self.messageReceived2.emit()
result = self._parse()
#remove the successfully parsed data
size = self.buffer.size()
self.buffer.close()
data = self.buffer.data()
self.buffer.setData(data.right(size - bytesRead))
self.buffer.open(QBuffer.ReadWrite)
self.buffer.seek(self.buffer.size())
def _parse(self):
if self.buffer.bytesAvailable() >= 4:
stream = QDataStream(self.buffer)
msg = Message.fromInt(stream.readInt32())
if msg == None:
return
args = []
bytesRead = 4
for aType in msg.argTypes:
if aType == str:
if self.buffer.bytesAvailable() < 4:
return
length = stream.readInt32()
if self.buffer.bytesAvailable() < length:
return
args.append(stream.readRawData(length))
bytesRead += 4 + length
elif aType == int:
if self.buffer.bytesAvailable() < 4:
return
args.append(stream.readInt32())
bytesRead += 4
elif aType == long:
if self.buffer.bytesAvailable() < 8:
return
args.append(stream.readInt64())
bytesRead += 8
return (msg, args, bytesRead)
def sendMessage(self, msg, args, id = None):
"""Sends a message.
msg -- The message type.
args -- List of message arguments.
id -- An optional socket descriptor. If specified, then the message will only be sent if this connection's socket descriptor matches id."""
if id:
if self.socketDescriptor() != id:
return
msg = Message.fromInt(msg)
if not msg.validateArgs(args):
log.warning("Message %d and args %s have invalid types. Message not sent.", msg, args)
return
if self.client:
#.........这里部分代码省略.........
示例2: Window
# 需要导入模块: from PyQt4.QtCore import QBuffer [as 别名]
# 或者: from PyQt4.QtCore.QBuffer import seek [as 别名]
class Window(QWidget):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
format = QAudioFormat()
format.setChannels(1)
format.setFrequency(22050)
format.setSampleSize(16)
format.setCodec("audio/pcm")
format.setByteOrder(QAudioFormat.LittleEndian)
format.setSampleType(QAudioFormat.SignedInt)
self.output = QAudioOutput(format, self)
self.frequency = 440
self.volume = 0
self.buffer = QBuffer()
self.data = QByteArray()
self.deviceLineEdit = QLineEdit()
self.deviceLineEdit.setReadOnly(True)
self.deviceLineEdit.setText(QAudioDeviceInfo.defaultOutputDevice().deviceName())
self.pitchSlider = QSlider(Qt.Horizontal)
self.pitchSlider.setMaximum(100)
self.volumeSlider = QSlider(Qt.Horizontal)
self.volumeSlider.setMaximum(32767)
self.volumeSlider.setPageStep(1024)
self.playButton = QPushButton(self.tr("&Play"))
self.pitchSlider.valueChanged.connect(self.changeFrequency)
self.volumeSlider.valueChanged.connect(self.changeVolume)
self.playButton.clicked.connect(self.play)
formLayout = QFormLayout()
formLayout.addRow(self.tr("Device:"), self.deviceLineEdit)
formLayout.addRow(self.tr("P&itch:"), self.pitchSlider)
formLayout.addRow(self.tr("&Volume:"), self.volumeSlider)
buttonLayout = QVBoxLayout()
buttonLayout.addWidget(self.playButton)
buttonLayout.addStretch()
horizontalLayout = QHBoxLayout(self)
horizontalLayout.addLayout(formLayout)
horizontalLayout.addLayout(buttonLayout)
def changeFrequency(self, value):
self.frequency = 440 + (value * 2)
def play(self):
if self.output.state() == QAudio.ActiveState:
self.output.stop()
if self.buffer.isOpen():
self.buffer.close()
self.createData()
self.buffer.setData(self.data)
self.buffer.open(QIODevice.ReadOnly)
self.buffer.seek(0)
self.output.start(self.buffer)
def changeVolume(self, value):
self.volume = value
def createData(self):
# Create 2 seconds of data with 22050 samples per second, each sample
# being 16 bits (2 bytes).
self.data.clear()
for i in xrange(2 * 22050):
t = i / 22050.0
value = int(self.volume * sin(2 * pi * self.frequency * t))
self.data.append(struct.pack("<h", value))