The QIODevice class is the base interface class of all I/O devices in Qt.More...
Header: | #include <QIODevice> |
qmake: | QT += core |
Inherits: | QObject |
Inherited By: | QAbstractSocket,QBuffer,QFileDevice,QLocalSocket,QNetworkReply, andQProcess |
Note: All functions in this class arereentrant.
flags | OpenMode |
enum | OpenModeFlag { NotOpen, ReadOnly, WriteOnly, ReadWrite, Append, …, ExistingOnly } |
QIODevice(QObject *parent) | |
QIODevice() | |
virtual | ~QIODevice() |
virtual bool | atEnd() const |
virtual qint64 | bytesAvailable() const |
virtual qint64 | bytesToWrite() const |
virtual bool | canReadLine() const |
virtual void | close() |
void | commitTransaction() |
int | currentReadChannel() const |
int | currentWriteChannel() const |
QString | errorString() const |
bool | getChar(char *c) |
bool | isOpen() const |
bool | isReadable() const |
virtual bool | isSequential() const |
bool | isTextModeEnabled() const |
bool | isTransactionStarted() const |
bool | isWritable() const |
virtual bool | open(QIODevice::OpenModemode) |
QIODevice::OpenMode | openMode() const |
qint64 | peek(char *data, qint64maxSize) |
QByteArray | peek(qint64maxSize) |
virtual qint64 | pos() const |
bool | putChar(charc) |
qint64 | read(char *data, qint64maxSize) |
QByteArray | read(qint64maxSize) |
QByteArray | readAll() |
int | readChannelCount() const |
qint64 | readLine(char *data, qint64maxSize) |
QByteArray | readLine(qint64maxSize = 0) |
virtual bool | reset() |
void | rollbackTransaction() |
virtual bool | seek(qint64pos) |
void | setCurrentReadChannel(intchannel) |
void | setCurrentWriteChannel(intchannel) |
void | setTextModeEnabled(boolenabled) |
virtual qint64 | size() const |
qint64 | skip(qint64maxSize) |
void | startTransaction() |
void | ungetChar(charc) |
virtual bool | waitForBytesWritten(intmsecs) |
virtual bool | waitForReadyRead(intmsecs) |
qint64 | write(const char *data, qint64maxSize) |
qint64 | write(const char *data) |
qint64 | write(const QByteArray &byteArray) |
int | writeChannelCount() const |
void | aboutToClose() |
void | bytesWritten(qint64bytes) |
void | channelBytesWritten(intchannel, qint64bytes) |
void | channelReadyRead(intchannel) |
void | readChannelFinished() |
void | readyRead() |
virtual qint64 | readData(char *data, qint64maxSize) = 0 |
virtual qint64 | readLineData(char *data, qint64maxSize) |
void | setErrorString(const QString &str) |
void | setOpenMode(QIODevice::OpenModeopenMode) |
virtual qint64 | writeData(const char *data, qint64maxSize) = 0 |
QIODevice provides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data, such asQFile,QBuffer andQTcpSocket. QIODevice is abstract and cannot be instantiated, but it is common to use the interface it defines to provide device-independent I/O features. For example, Qt's XML classes operate on a QIODevice pointer, allowing them to be used with various devices (such as files and buffers).
Before accessing the device,open() must be called to set the correctOpenMode (such asReadOnly orReadWrite). You can then write to the device withwrite() orputChar(), and read by calling eitherread(),readLine(), orreadAll(). Callclose() when you are done with the device.
QIODevice distinguishes between two types of devices: random-access devices and sequential devices.
You can useisSequential() to determine the type of device.
QIODevice emitsreadyRead() when new data is available for reading; for example, if new data has arrived on the network or if additional data is appended to a file that you are reading from. You can callbytesAvailable() to determine the number of bytes that are currently available for reading. It's common to usebytesAvailable() together with thereadyRead() signal when programming with asynchronous devices such asQTcpSocket, where fragments of data can arrive at arbitrary points in time. QIODevice emits thebytesWritten() signal every time a payload of data has been written to the device. UsebytesToWrite() to determine the current amount of data waiting to be written.
Certain subclasses of QIODevice, such asQTcpSocket andQProcess, are asynchronous. This means that I/O functions such aswrite() orread() always return immediately, while communication with the device itself may happen when control goes back to the event loop. QIODevice provides functions that allow you to force these operations to be performed immediately, while blocking the calling thread and without entering the event loop. This allows QIODevice subclasses to be used without an event loop, or in a separate thread:
Calling these functions from the main, GUI thread, may cause your user interface to freeze. Example:
QProcess gzip;gzip.start("gzip",QStringList()<<"-c");if (!gzip.waitForStarted())returnfalse;gzip.write("uncompressed data");QByteArray compressed;while (gzip.waitForReadyRead()) compressed+= gzip.readAll();
By subclassing QIODevice, you can provide the same interface to your own I/O devices. Subclasses of QIODevice are only required to implement the protectedreadData() andwriteData() functions. QIODevice uses these functions to implement all its convenience functions, such asgetChar(),readLine() andwrite(). QIODevice also handles access control for you, so you can safely assume that the device is opened in write mode ifwriteData() is called.
Some subclasses, such asQFile andQTcpSocket, are implemented using a memory buffer for intermediate storing of data. This reduces the number of required device accessing calls, which are often very slow. Buffering makes functions likegetChar() andputChar() fast, as they can operate on the memory buffer instead of directly on the device itself. Certain I/O operations, however, don't work well with a buffer. For example, if several users open the same device and read it character by character, they may end up reading the same data when they meant to read a separate chunk each. For this reason, QIODevice allows you to bypass any buffering by passing the Unbuffered flag toopen(). When subclassing QIODevice, remember to bypass any buffer you may use when the device is open in Unbuffered mode.
Usually, the incoming data stream from an asynchronous device is fragmented, and chunks of data can arrive at arbitrary points in time. To handle incomplete reads of data structures, use the transaction mechanism implemented by QIODevice. SeestartTransaction() and related functions for more details.
Some sequential devices support communicating via multiple channels. These channels represent separate streams of data that have the property of independently sequenced delivery. Once the device is opened, you can determine the number of channels by calling thereadChannelCount() andwriteChannelCount() functions. To switch between channels, callsetCurrentReadChannel() andsetCurrentWriteChannel(), respectively. QIODevice also provides additional signals to handle asynchronous communication on a per-channel basis.
See alsoQBuffer,QFile, andQTcpSocket.
This enum is used withopen() to describe the mode in which a device is opened. It is also returned byopenMode().
Constant | Value | Description |
---|---|---|
QIODevice::NotOpen | 0x0000 | The device is not open. |
QIODevice::ReadOnly | 0x0001 | The device is open for reading. |
QIODevice::WriteOnly | 0x0002 | The device is open for writing. Note that, for file-system subclasses (e.g.QFile), this mode implies Truncate unless combined with ReadOnly, Append or NewOnly. |
QIODevice::ReadWrite | ReadOnly | WriteOnly | The device is open for reading and writing. |
QIODevice::Append | 0x0004 | The device is opened in append mode so that all data is written to the end of the file. |
QIODevice::Truncate | 0x0008 | If possible, the device is truncated before it is opened. All earlier contents of the device are lost. |
QIODevice::Text | 0x0010 | When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32. |
QIODevice::Unbuffered | 0x0020 | Any buffer in the device is bypassed. |
QIODevice::NewOnly | 0x0040 | Fail if the file to be opened already exists. Create and open the file only if it does not exist. There is a guarantee from the operating system that you are the only one creating and opening the file. Note that this mode implies WriteOnly, and combining it with ReadWrite is allowed. This flag currently only affectsQFile. Other classes might use this flag in the future, but until then using this flag with any classes other thanQFile may result in undefined behavior. (since Qt 5.11) |
QIODevice::ExistingOnly | 0x0080 | Fail if the file to be opened does not exist. This flag must be specified alongside ReadOnly, WriteOnly, or ReadWrite. Note that using this flag with ReadOnly alone is redundant, as ReadOnly already fails when the file does not exist. This flag currently only affectsQFile. Other classes might use this flag in the future, but until then using this flag with any classes other thanQFile may result in undefined behavior. (since Qt 5.11) |
Certain flags, such asUnbuffered
andTruncate
, are meaningless when used with some subclasses. Some of these restrictions are implied by the type of device that is represented by a subclass. In other cases, the restriction may be due to the implementation, or may be imposed by the underlying platform; for example,QTcpSocket does not supportUnbuffered
mode, and limitations in the native API preventQFile from supportingUnbuffered
on Windows.
The OpenMode type is a typedef forQFlags<OpenModeFlag>. It stores an OR combination of OpenModeFlag values.
Constructs a QIODevice object with the givenparent.
Constructs a QIODevice object.
[signal]
void QIODevice::aboutToClose()This signal is emitted when the device is about to close. Connect this signal if you have operations that need to be performed before the device closes (e.g., if you have data in a separate buffer that needs to be written to the device).
[signal]
void QIODevice::bytesWritten(qint64bytes)This signal is emitted every time a payload of data has been written to the device's current write channel. Thebytes argument is set to the number of bytes that were written in this payload.
bytesWritten() is not emitted recursively; if you reenter the event loop or callwaitForBytesWritten() inside a slot connected to the bytesWritten() signal, the signal will not be reemitted (althoughwaitForBytesWritten() may still return true).
See alsoreadyRead().
[signal]
void QIODevice::channelBytesWritten(intchannel,qint64bytes)This signal is emitted every time a payload of data has been written to the device. Thebytes argument is set to the number of bytes that were written in this payload, whilechannel is the channel they were written to. UnlikebytesWritten(), it is emitted regardless of thecurrent write channel.
channelBytesWritten() can be emitted recursively - even for the same channel.
This function was introduced in Qt 5.7.
See alsobytesWritten() andchannelReadyRead().
[signal]
void QIODevice::channelReadyRead(intchannel)This signal is emitted when new data is available for reading from the device. Thechannel argument is set to the index of the read channel on which the data has arrived. UnlikereadyRead(), it is emitted regardless of thecurrent read channel.
channelReadyRead() can be emitted recursively - even for the same channel.
This function was introduced in Qt 5.7.
See alsoreadyRead() andchannelBytesWritten().
[signal]
void QIODevice::readChannelFinished()This signal is emitted when the input (reading) stream is closed in this device. It is emitted as soon as the closing is detected, which means that there might still be data available for reading withread().
This function was introduced in Qt 4.4.
[signal]
void QIODevice::readyRead()This signal is emitted once every time new data is available for reading from the device's current read channel. It will only be emitted again once new data is available, such as when a new payload of network data has arrived on your network socket, or when a new block of data has been appended to your device.
readyRead() is not emitted recursively; if you reenter the event loop or callwaitForReadyRead() inside a slot connected to the readyRead() signal, the signal will not be reemitted (althoughwaitForReadyRead() may still return true).
Note for developers implementing classes derived fromQIODevice: you should always emit readyRead() when new data has arrived (do not emit it only because there's data still to be read in your buffers). Do not emit readyRead() in other conditions.
See alsobytesWritten().
[virtual]
QIODevice::~QIODevice()The destructor is virtual, andQIODevice is an abstract base class. This destructor does not callclose(), but the subclass destructor might. If you are in doubt, callclose() before destroying theQIODevice.
[virtual]
bool QIODevice::atEnd() constReturnstrue
if the current read and write position is at the end of the device (i.e. there is no more data available for reading on the device); otherwise returnsfalse
.
For some devices, atEnd() can return true even though there is more data to read. This special case only applies to devices that generate data in direct response to you callingread() (e.g.,/dev
or/proc
files on Unix and macOS, or console input /stdin
on all platforms).
See alsobytesAvailable(),read(), andisSequential().
[virtual]
qint64 QIODevice::bytesAvailable() constReturns the number of bytes that are available for reading. This function is commonly used with sequential devices to determine the number of bytes to allocate in a buffer before reading.
Subclasses that reimplement this function must call the base implementation in order to include the size of the buffer ofQIODevice. Example:
See alsobytesToWrite(),readyRead(), andisSequential().
[virtual]
qint64 QIODevice::bytesToWrite() constFor buffered devices, this function returns the number of bytes waiting to be written. For devices with no buffer, this function returns 0.
Subclasses that reimplement this function must call the base implementation in order to include the size of the buffer ofQIODevice.
See alsobytesAvailable(),bytesWritten(), andisSequential().
[virtual]
bool QIODevice::canReadLine() constReturnstrue
if a complete line of data can be read from the device; otherwise returnsfalse
.
Note that unbuffered devices, which have no way of determining what can be read, always return false.
This function is often called in conjunction with thereadyRead() signal.
Subclasses that reimplement this function must call the base implementation in order to include the contents of theQIODevice's buffer. Example:
bool CustomDevice::canReadLine()const{return buffer.contains('\n')||QIODevice::canReadLine();}
See alsoreadyRead() andreadLine().
[virtual]
void QIODevice::close()First emitsaboutToClose(), then closes the device and sets itsOpenMode toNotOpen. The error string is also reset.
See alsosetOpenMode() andOpenMode.
Completes a read transaction.
For sequential devices, all data recorded in the internal buffer during the transaction will be discarded.
This function was introduced in Qt 5.7.
See alsostartTransaction() androllbackTransaction().
Returns the index of the current read channel.
This function was introduced in Qt 5.7.
See alsosetCurrentReadChannel(),readChannelCount(), andQProcess.
Returns the index of the current write channel.
This function was introduced in Qt 5.7.
See alsosetCurrentWriteChannel() andwriteChannelCount().
Returns a human-readable description of the last device error that occurred.
See alsosetErrorString().
Reads one character from the device and stores it inc. Ifc isnullptr
, the character is discarded. Returnstrue
on success; otherwise returnsfalse
.
See alsoread(),putChar(), andungetChar().
Returnstrue
if the device is open; otherwise returnsfalse
. A device is open if it can be read from and/or written to. By default, this function returnsfalse
ifopenMode() returnsNotOpen
.
See alsoopenMode() andOpenMode.
Returnstrue
if data can be read from the device; otherwise returns false. UsebytesAvailable() to determine how many bytes can be read.
This is a convenience function which checks if theOpenMode of the device contains theReadOnly flag.
See alsoopenMode() andOpenMode.
[virtual]
bool QIODevice::isSequential() constReturnstrue
if this device is sequential; otherwise returns false.
Sequential devices, as opposed to a random-access devices, have no concept of a start, an end, a size, or a current position, and they do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential.
Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential.
See alsobytesAvailable().
Returnstrue
if theText flag is enabled; otherwise returnsfalse
.
See alsosetTextModeEnabled().
Returnstrue
if a transaction is in progress on the device, otherwisefalse
.
This function was introduced in Qt 5.7.
See alsostartTransaction().
Returnstrue
if data can be written to the device; otherwise returns false.
This is a convenience function which checks if theOpenMode of the device contains theWriteOnly flag.
See alsoopenMode() andOpenMode.
[virtual]
bool QIODevice::open(QIODevice::OpenModemode)Opens the device and sets itsOpenMode tomode. Returnstrue
if successful; otherwise returnsfalse
. This function should be called from any reimplementations of open() or other functions that open the device.
See alsoopenMode() andOpenMode.
Returns the mode in which the device has been opened; i.e.ReadOnly orWriteOnly.
See alsosetOpenMode() andOpenMode.
Reads at mostmaxSize bytes from the device intodata, without side effects (i.e., if you callread() after peek(), you will get the same data). Returns the number of bytes read. If an error occurs, such as when attempting to peek a device opened inWriteOnly mode, this function returns -1.
0 is returned when no more data is available for reading.
Example:
bool isExeFile(QFile*file){char buf[2];if (file->peek(buf,sizeof(buf))==sizeof(buf))return (buf[0]=='M'&& buf[1]=='Z');returnfalse;}
This function was introduced in Qt 4.1.
See alsoread().
This is an overloaded function.
Peeks at mostmaxSize bytes from the device, returning the data peeked as aQByteArray.
Example:
bool isExeFile(QFile*file){return file->peek(2)=="MZ";}
This function has no way of reporting errors; returning an emptyQByteArray can mean either that no data was currently available for peeking, or that an error occurred.
This function was introduced in Qt 4.1.
See alsoread().
[virtual]
qint64 QIODevice::pos() constFor random-access devices, this function returns the position that data is written to or read from. For sequential devices or closed devices, where there is no concept of a "current position", 0 is returned.
The current read/write position of the device is maintained internally byQIODevice, so reimplementing this function is not necessary. When subclassingQIODevice, useQIODevice::seek() to notifyQIODevice about changes in the device position.
See alsoisSequential() andseek().
Writes the characterc to the device. Returnstrue
on success; otherwise returnsfalse
.
See alsowrite(),getChar(), andungetChar().
Reads at mostmaxSize bytes from the device intodata, and returns the number of bytes read. If an error occurs, such as when attempting to read from a device opened inWriteOnly mode, this function returns -1.
0 is returned when no more data is available for reading. However, reading past the end of the stream is considered an error, so this function returns -1 in those cases (that is, reading on a closed socket or after a process has died).
See alsoreadData(),readLine(), andwrite().
This is an overloaded function.
Reads at mostmaxSize bytes from the device, and returns the data read as aQByteArray.
This function has no way of reporting errors; returning an emptyQByteArray can mean either that no data was currently available for reading, or that an error occurred.
Reads all remaining data from the device, and returns it as a byte array.
This function has no way of reporting errors; returning an emptyQByteArray can mean either that no data was currently available for reading, or that an error occurred.
Returns the number of available read channels if the device is open; otherwise returns 0.
This function was introduced in Qt 5.7.
See alsowriteChannelCount() andQProcess.
[pure virtual protected]
qint64 QIODevice::readData(char *data,qint64maxSize)Reads up tomaxSize bytes from the device intodata, and returns the number of bytes read or -1 if an error occurred.
If there are no bytes to be read and there can never be more bytes available (examples include socket closed, pipe closed, sub-process finished), this function returns -1.
This function is called byQIODevice. Reimplement this function when creating a subclass ofQIODevice.
When reimplementing this function it is important that this function reads all the required data before returning. This is required in order forQDataStream to be able to operate on the class.QDataStream assumes all the requested information was read and therefore does not retry reading if there was a problem.
This function might be called with a maxSize of 0, which can be used to perform post-reading operations.
See alsoread(),readLine(), andwriteData().
This function reads a line of ASCII characters from the device, up to a maximum ofmaxSize - 1 bytes, stores the characters indata, and returns the number of bytes read. If a line could not be read but no error occurred, this function returns 0. If an error occurs, this function returns the length of what could be read, or -1 if nothing was read.
A terminating '\0' byte is always appended todata, somaxSize must be larger than 1.
Data is read until either of the following conditions are met:
For example, the following code reads a line of characters from a file:
QFile file("box.txt");if (file.open(QFile::ReadOnly)) {char buf[1024];qint64 lineLength= file.readLine(buf,sizeof(buf));if (lineLength!=-1) {// the line is available in buf }}
The newline character ('\n') is included in the buffer. If a newline is not encountered before maxSize - 1 bytes are read, a newline will not be inserted into the buffer. On windows newline characters are replaced with '\n'.
This function callsreadLineData(), which is implemented using repeated calls togetChar(). You can provide a more efficient implementation by reimplementingreadLineData() in your own subclass.
See alsogetChar(),read(), andwrite().
This is an overloaded function.
Reads a line from the device, but no more thanmaxSize characters, and returns the result as a byte array.
This function has no way of reporting errors; returning an emptyQByteArray can mean either that no data was currently available for reading, or that an error occurred.
[virtual protected]
qint64 QIODevice::readLineData(char *data,qint64maxSize)Reads up tomaxSize characters intodata and returns the number of characters read.
This function is called byreadLine(), and provides its base implementation, usinggetChar(). Buffered devices can improve the performance ofreadLine() by reimplementing this function.
readLine() appends a '\0' byte todata; readLineData() does not need to do this.
If you reimplement this function, be careful to return the correct value: it should return the number of bytes read in this line, including the terminating newline, or 0 if there is no line to be read at this point. If an error occurs, it should return -1 if and only if no bytes were read. Reading past EOF is considered an error.
[virtual]
bool QIODevice::reset()Seeks to the start of input for random-access devices. Returns true on success; otherwise returnsfalse
(for example, if the device is not open).
Note that when using aQTextStream on aQFile, calling reset() on theQFile will not have the expected result becauseQTextStream buffers the file. Use theQTextStream::seek() function instead.
See alsoseek().
Rolls back a read transaction.
Restores the input stream to the point of thestartTransaction() call. This function is commonly used to rollback the transaction when an incomplete read was detected prior to committing the transaction.
This function was introduced in Qt 5.7.
See alsostartTransaction() andcommitTransaction().
[virtual]
bool QIODevice::seek(qint64pos)For random-access devices, this function sets the current position topos, returning true on success, or false if an error occurred. For sequential devices, the default behavior is to produce a warning and return false.
When subclassingQIODevice, you must call QIODevice::seek() at the start of your function to ensure integrity withQIODevice's built-in buffer.
See alsopos() andisSequential().
Sets the current read channel of theQIODevice to the givenchannel. The current input channel is used by the functionsread(),readAll(),readLine(), andgetChar(). It also determines which channel triggersQIODevice to emitreadyRead().
This function was introduced in Qt 5.7.
See alsocurrentReadChannel(),readChannelCount(), andQProcess.
Sets the current write channel of theQIODevice to the givenchannel. The current output channel is used by the functionswrite(),putChar(). It also determines which channel triggersQIODevice to emitbytesWritten().
This function was introduced in Qt 5.7.
See alsocurrentWriteChannel() andwriteChannelCount().
[protected]
void QIODevice::setErrorString(constQString &str)Sets the human readable description of the last device error that occurred tostr.
See alsoerrorString().
[protected]
void QIODevice::setOpenMode(QIODevice::OpenModeopenMode)Sets theOpenMode of the device toopenMode. Call this function to set the open mode if the flags change after the device has been opened.
See alsoopenMode() andOpenMode.
Ifenabled is true, this function sets theText flag on the device; otherwise theText flag is removed. This feature is useful for classes that provide custom end-of-line handling on aQIODevice.
The IO device should be opened before calling this function.
See alsoisTextModeEnabled(),open(), andsetOpenMode().
[virtual]
qint64 QIODevice::size() constFor open random-access devices, this function returns the size of the device. For open sequential devices,bytesAvailable() is returned.
If the device is closed, the size returned will not reflect the actual size of the device.
See alsoisSequential() andpos().
Skips up tomaxSize bytes from the device. Returns the number of bytes actually skipped, or -1 on error.
This function does not wait and only discards the data that is already available for reading.
If the device is opened in text mode, end-of-line terminators are translated to '\n' symbols and count as a single byte identically to theread() andpeek() behavior.
This function works for all devices, including sequential ones that cannotseek(). It is optimized to skip unwanted data after apeek() call.
For random-access devices, skip() can be used to seek forward from the current position. NegativemaxSize values are not allowed.
This function was introduced in Qt 5.10.
See alsopeek(),seek(), andread().
Starts a new read transaction on the device.
Defines a restorable point within the sequence of read operations. For sequential devices, read data will be duplicated internally to allow recovery in case of incomplete reads. For random-access devices, this function saves the current position. CallcommitTransaction() orrollbackTransaction() to finish the transaction.
Note:Nesting transactions is not supported.
This function was introduced in Qt 5.7.
See alsocommitTransaction() androllbackTransaction().
Puts the characterc back into the device, and decrements the current position unless the position is 0. This function is usually called to "undo" agetChar() operation, such as when writing a backtracking parser.
Ifc was not previously read from the device, the behavior is undefined.
Note:This function is not available while a transaction is in progress.
[virtual]
bool QIODevice::waitForBytesWritten(intmsecs)For buffered devices, this function waits until a payload of buffered written data has been written to the device and thebytesWritten() signal has been emitted, or untilmsecs milliseconds have passed. If msecs is -1, this function will not time out. For unbuffered devices, it returns immediately.
Returnstrue
if a payload of data was written to the device; otherwise returnsfalse
(i.e. if the operation timed out, or if an error occurred).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
If called from within a slot connected to thebytesWritten() signal,bytesWritten() will not be reemitted.
Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returnsfalse
.
Warning:Calling this function from the main (GUI) thread might cause your user interface to freeze.
See alsowaitForReadyRead().
[virtual]
bool QIODevice::waitForReadyRead(intmsecs)Blocks until new data is available for reading and thereadyRead() signal has been emitted, or untilmsecs milliseconds have passed. If msecs is -1, this function will not time out.
Returnstrue
if new data is available for reading; otherwise returns false (if the operation timed out or if an error occurred).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
If called from within a slot connected to thereadyRead() signal,readyRead() will not be reemitted.
Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returnsfalse
.
Warning:Calling this function from the main (GUI) thread might cause your user interface to freeze.
See alsowaitForBytesWritten().
Writes at mostmaxSize bytes of data fromdata to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.
See alsoread() andwriteData().
This is an overloaded function.
Writes data from a zero-terminated string of 8-bit characters to the device. Returns the number of bytes that were actually written, or -1 if an error occurred. This is equivalent to
...QIODevice::write(data, qstrlen(data));...
This function was introduced in Qt 4.5.
See alsoread() andwriteData().
This is an overloaded function.
Writes the content ofbyteArray to the device. Returns the number of bytes that were actually written, or -1 if an error occurred.
See alsoread() andwriteData().
Returns the number of available write channels if the device is open; otherwise returns 0.
This function was introduced in Qt 5.7.
See alsoreadChannelCount().
[pure virtual protected]
qint64 QIODevice::writeData(constchar *data,qint64maxSize)Writes up tomaxSize bytes fromdata to the device. Returns the number of bytes written, or -1 if an error occurred.
This function is called byQIODevice. Reimplement this function when creating a subclass ofQIODevice.
When reimplementing this function it is important that this function writes all the data available before returning. This is required in order forQDataStream to be able to operate on the class.QDataStream assumes all the information was written and therefore does not retry writing if there was a problem.
© 2024 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
Qt Group includes The Qt Company Oy and its global subsidiaries and affiliates.