FileIO Implementation
Remember the FileIO
API we want to create should look like this.
We will leave out the properties, as they are simple setters and getters.
The read method opens a file in reading mode and reads the data using a text stream.
When the text is changed it is necessary to inform others about the change using emit textChanged(m_text)
. Otherwise, property binding will not work.
The write method does the same but opens the file in write mode and uses the stream to write the contents of the text
property.
To make the type visible to QML, we add the QML_ELEMENT
macro just after the Q_PROPERTY
lines. This tells Qt that the type should be made available to QML. If you want to provide a different name than the C++ class, you can use the QML_NAMED_ELEMENT
macro.
As the reading and writing are blocking function calls you should only use this FileIO
for small texts, otherwise, you will block the UI thread of Qt. Be warned!
Last updated