本文整理汇总了Python中PyQt5.QtCore.QIODevice.ReadWrite方法的典型用法代码示例。如果您正苦于以下问题:Python QIODevice.ReadWrite方法的具体用法?Python QIODevice.ReadWrite怎么用?Python QIODevice.ReadWrite使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtCore.QIODevice
的用法示例。
在下文中一共展示了QIODevice.ReadWrite方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fromqimage
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def fromqimage(im):
"""
:param im: A PIL Image object, or a file name
(given either as Python string or a PyQt string object)
"""
buffer = QBuffer()
buffer.open(QIODevice.ReadWrite)
# preserve alha channel with png
# otherwise ppm is more friendly with Image.open
if im.hasAlphaChannel():
im.save(buffer, 'png')
else:
im.save(buffer, 'ppm')
b = BytesIO()
try:
b.write(buffer.data())
except TypeError:
# workaround for Python 2
b.write(str(buffer.data()))
buffer.close()
b.seek(0)
return Image.open(b)
示例2: fromqimage
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def fromqimage(im):
"""
:param im: A PIL Image object, or a file name
(given either as Python string or a PyQt string object)
"""
buffer = QBuffer()
buffer.open(QIODevice.ReadWrite)
# preserve alpha channel with png
# otherwise ppm is more friendly with Image.open
if im.hasAlphaChannel():
im.save(buffer, "png")
else:
im.save(buffer, "ppm")
b = BytesIO()
b.write(buffer.data())
buffer.close()
b.seek(0)
return Image.open(b)
示例3: fromqimage
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def fromqimage(im):
"""
:param im: A PIL Image object, or a file name
(given either as Python string or a PyQt string object)
"""
buffer = QBuffer()
buffer.open(QIODevice.ReadWrite)
# preserve alpha channel with png
# otherwise ppm is more friendly with Image.open
if im.hasAlphaChannel():
im.save(buffer, 'png')
else:
im.save(buffer, 'ppm')
b = BytesIO()
try:
b.write(buffer.data())
except TypeError:
# workaround for Python 2
b.write(str(buffer.data()))
buffer.close()
b.seek(0)
return Image.open(b)
示例4: open_serial_link
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def open_serial_link(self, port):
"""
Creates a new serial link instance.
"""
self.input_buffer = []
self.serial = QSerialPort()
self.serial.setPortName(port)
if self.serial.open(QIODevice.ReadWrite):
self.serial.setDataTerminalReady(True)
if not self.serial.isDataTerminalReady():
# Using pyserial as a 'hack' to open the port and set DTR
# as QtSerial does not seem to work on some Windows :(
# See issues #281 and #302 for details.
self.serial.close()
pyser = serial.Serial(port) # open serial port w/pyserial
pyser.dtr = True
pyser.close()
self.serial.open(QIODevice.ReadWrite)
self.serial.setBaudRate(115200)
self.serial.readyRead.connect(self.on_serial_read)
else:
msg = _("Cannot connect to device on port {}").format(port)
raise IOError(msg)
示例5: test_Window_open_serial_link
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def test_Window_open_serial_link(qtapp):
"""
Ensure the serial port is opened in the expected manner.
"""
mock_serial = mock.MagicMock()
mock_serial.setPortName = mock.MagicMock(return_value=None)
mock_serial.setBaudRate = mock.MagicMock(return_value=None)
mock_serial.open = mock.MagicMock(return_value=True)
mock_serial.readyRead = mock.MagicMock()
mock_serial.readyRead.connect = mock.MagicMock(return_value=None)
mock_serial_class = mock.MagicMock(return_value=mock_serial)
with mock.patch("mu.interface.main.QSerialPort", mock_serial_class):
w = mu.interface.main.Window()
w.open_serial_link("COM0")
assert w.input_buffer == []
mock_serial.setPortName.assert_called_once_with("COM0")
mock_serial.setBaudRate.assert_called_once_with(115200)
mock_serial.open.assert_called_once_with(QIODevice.ReadWrite)
mock_serial.readyRead.connect.assert_called_once_with(w.on_serial_read)
示例6: fromqpixmap
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def fromqpixmap(im):
return fromqimage(im)
# buffer = QBuffer()
# buffer.open(QIODevice.ReadWrite)
# # im.save(buffer)
# # What if png doesn't support some image features like animation?
# im.save(buffer, 'ppm')
# bytes_io = BytesIO()
# bytes_io.write(buffer.data())
# buffer.close()
# bytes_io.seek(0)
# return Image.open(bytes_io)
示例7: serialize
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def serialize(items):
"""Serialize a list of WebHistoryItems to a data stream.
Args:
items: An iterable of WebHistoryItems.
Return:
A (stream, data, user_data) tuple.
stream: The reset QDataStream.
data: The QByteArray with the raw data.
user_data: A list with each item's user data.
Warning:
If 'data' goes out of scope, reading from 'stream' will result in a
segfault!
"""
data = QByteArray()
stream = QDataStream(data, QIODevice.ReadWrite)
user_data = [] # type: typing.List[typing.Mapping[str, typing.Any]]
current_idx = None
for i, item in enumerate(items):
if item.active:
if current_idx is not None:
raise ValueError("Multiple active items ({} and {}) "
"found!".format(current_idx, i))
current_idx = i
if items:
if current_idx is None:
raise ValueError("No active item found!")
else:
current_idx = 0
_serialize_items(items, current_idx, stream)
user_data += [item.user_data for item in items]
stream.device().reset()
qtutils.check_qdatastream(stream)
return stream, data, user_data
示例8: serialize
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def serialize(items):
"""Serialize a list of WebHistoryItems to a data stream.
Args:
items: An iterable of WebHistoryItems.
Return:
A (stream, data, user_data) tuple.
stream: The reset QDataStream.
data: The QByteArray with the raw data.
cur_user_data: The user data for the current item or None.
Warning:
If 'data' goes out of scope, reading from 'stream' will result in a
segfault!
"""
data = QByteArray()
stream = QDataStream(data, QIODevice.ReadWrite)
cur_user_data = None
current_idx = None
for i, item in enumerate(items):
if item.active:
if current_idx is not None:
raise ValueError("Multiple active items ({} and {}) "
"found!".format(current_idx, i))
current_idx = i
cur_user_data = item.user_data
if items:
if current_idx is None:
raise ValueError("No active item found!")
else:
current_idx = -1
### src/core/web_contents_adapter.cpp serializeNavigationHistory
# sample data:
# kHistoryStreamVersion
stream.writeInt(HISTORY_STREAM_VERSION) # \x00\x00\x00\x03
# count
stream.writeInt(len(items)) # \x00\x00\x00\x01
# currentIndex
stream.writeInt(current_idx) # \x00\x00\x00\x00
for item in items:
_serialize_item(item, stream)
stream.device().reset()
qtutils.check_qdatastream(stream)
return stream, data, cur_user_data
示例9: on_buttonConnect_clicked
# 需要导入模块: from PyQt5.QtCore import QIODevice [as 别名]
# 或者: from PyQt5.QtCore.QIODevice import ReadWrite [as 别名]
def on_buttonConnect_clicked(self):
# 打开或关闭串口按钮
if self._serial.isOpen():
# 如果串口是打开状态则关闭
self._serial.close()
self.textBrowser.append('串口已关闭')
self.buttonConnect.setText('打开串口')
self.labelStatus.setProperty('isOn', False)
self.labelStatus.style().polish(self.labelStatus) # 刷新样式
return
# 根据配置连接串口
name = self.comboBoxPort.currentText()
if not name:
QMessageBox.critical(self, '错误', '没有选择串口')
return
port = self._ports[name]
# self._serial.setPort(port)
# 根据名字设置串口(也可以用上面的函数)
self._serial.setPortName(port.systemLocation())
# 设置波特率
self._serial.setBaudRate( # 动态获取,类似QSerialPort::Baud9600这样的吧
getattr(QSerialPort, 'Baud' + self.comboBoxBaud.currentText()))
# 设置校验位
self._serial.setParity( # QSerialPort::NoParity
getattr(QSerialPort, self.comboBoxParity.currentText() + 'Parity'))
# 设置数据位
self._serial.setDataBits( # QSerialPort::Data8
getattr(QSerialPort, 'Data' + self.comboBoxData.currentText()))
# 设置停止位
self._serial.setStopBits( # QSerialPort::Data8
getattr(QSerialPort, self.comboBoxStop.currentText()))
# NoFlowControl 没有流程控制
# HardwareControl 硬件流程控制(RTS/CTS)
# SoftwareControl 软件流程控制(XON/XOFF)
# UnknownFlowControl 未知控制
self._serial.setFlowControl(QSerialPort.NoFlowControl)
# 读写方式打开串口
ok = self._serial.open(QIODevice.ReadWrite)
if ok:
self.textBrowser.append('打开串口成功')
self.buttonConnect.setText('关闭串口')
self.labelStatus.setProperty('isOn', True)
self.labelStatus.style().polish(self.labelStatus) # 刷新样式
else:
self.textBrowser.append('打开串口失败')
self.buttonConnect.setText('打开串口')
self.labelStatus.setProperty('isOn', False)
self.labelStatus.style().polish(self.labelStatus) # 刷新样式