PySide6.QtGui.QTextDocumentWriter¶
- class QTextDocumentWriter¶
The
QTextDocumentWriter
class provides a format-independent interface for writing aQTextDocument
to files or other devices. More…Added in version 4.5.
Synopsis¶
Methods¶
def
__init__()
def
device()
def
fileName()
def
format()
def
setDevice()
def
setFileName()
def
setFormat()
def
write()
Static functions¶
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
To write a document, construct a
QTextDocumentWriter
object with either a file name or a device object, and specify the document format to be written. You can construct a writer and set the format usingsetFormat()
later.Call
write()
to write the document to the device. If the document is successfully written, this function returnstrue
. However, if an error occurs when writing the document, it will return false.Call
supportedDocumentFormats()
for a list of formats thatQTextDocumentWriter
can write.Since the capabilities of the supported output formats vary considerably, the writer simply outputs the appropriate subset of objects for each format. This typically includes the formatted text and images contained in a document.
- __init__()¶
Constructs an empty
QTextDocumentWriter
object. Before writing, you must callsetFormat()
to set a document format, thensetDevice()
orsetFileName()
.- __init__(device, format)
- Parameters:
device –
QIODevice
format –
QByteArray
Constructs a
QTextDocumentWriter
object to write to the givendevice
in the document format specified byformat
.- __init__(fileName[, format=QByteArray()])
- Parameters:
fileName – str
format –
QByteArray
Constructs an
QTextDocumentWriter
object that will write to a file with the namefileName
, using the document format specified byformat
. Ifformat
is not provided,QTextDocumentWriter
will detect the document format by inspecting the extension offileName
.Returns the device currently assigned, or
None
if no device has been assigned.See also
- fileName()¶
- Return type:
str
If the currently assigned device is a QFile, or if
setFileName()
has been called, this function returns the name of the file to be written to. In all other cases, it returns an empty string.See also
- format()¶
- Return type:
Returns the format used for writing documents.
See also
Sets the writer’s device to the
device
specified. If a device has already been set, the old device is removed but otherwise left unchanged.If the device is not already open,
QTextDocumentWriter
will attempt to open the device in WriteOnly mode by calling open().Note
This will not work for certain devices, such as QProcess, QTcpSocket and QUdpSocket, where some configuration is required before the device can be opened.
See also
- setFileName(fileName)¶
- Parameters:
fileName – str
Sets the name of the file to be written to
fileName
. Internally,QTextDocumentWriter
will create a QFile and open it in WriteOnly mode, and use this file when writing the document.See also
- setFormat(format)¶
- Parameters:
format –
QByteArray
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Sets the format used to write documents to the
format
specified.format
is a case insensitive text string. For example:writer = QTextDocumentWriter() writer.setFormat("odf") # same as writer.setFormat("ODF")
You can call
supportedDocumentFormats()
for the full list of formatsQTextDocumentWriter
supports.See also
- static supportedDocumentFormats()¶
- Return type:
.list of QByteArray
Returns the list of document formats supported by
QTextDocumentWriter
.By default, Qt can write the following formats:
Format
Description
plaintext
Plain text
HTML
HyperText Markup Language
markdown
Markdown (CommonMark or GitHub dialects)
ODF
OpenDocument Format
See also
- write(document)¶
- Parameters:
document –
QTextDocument
- Return type:
bool
Writes the given
document
to the assigned device or file and returnstrue
if successful; otherwise returnsfalse
.- write(fragment)
- Parameters:
fragment –
QTextDocumentFragment
- Return type:
bool
Writes the document fragment specified by
fragment
to the assigned device or file and returnstrue
if successful; otherwise returnsfalse
.