I have a subclass of QTextDocument that overrides QTextDocument::loadResource(int type, const QUrl &name)
.
I want to clone it.
Ideas ?
You can't use QTextDocument::clone
for that. Neither can you reimplement it because it's not virtual. You should create another clone method (you can name it clone
but I'd give it another name to avoid confusion) and implement cloning yourself. You can use toHtml
and setHtml
to copy content from one object to another:
MyTextDocument* MyTextDocument::my_clone(QObject * parent = 0) {
MyTextDocument* other = new MyTextDocument(parent);
other->setHtml(toHtml());
return other;
}
There are however many other properties that should be copied. See how QTextDocument::clone
method is implemented:
QTextDocument *QTextDocument::clone(QObject *parent) const
{
Q_D(const QTextDocument);
QTextDocument *doc = new QTextDocument(parent);
QTextCursor(doc).insertFragment(QTextDocumentFragment(this));
doc->rootFrame()->setFrameFormat(rootFrame()->frameFormat());
QTextDocumentPrivate *priv = doc->d_func();
priv->title = d->title;
priv->url = d->url;
priv->pageSize = d->pageSize;
priv->indentWidth = d->indentWidth;
priv->defaultTextOption = d->defaultTextOption;
priv->setDefaultFont(d->defaultFont());
priv->resources = d->resources;
priv->cachedResources.clear();
#ifndef QT_NO_CSSPARSER
priv->defaultStyleSheet = d->defaultStyleSheet;
priv->parsedDefaultStyleSheet = d->parsedDefaultStyleSheet;
#endif
return doc;
}
If these properties are important to you, you need to copy them manually in your my_clone
implementation. You can're use QTextDocumentPrivate
class because it's internal. So you can't just copy default implementation. But there is a normal way to set each of listed properties in the API.
Be aware of forward compability issues. If new properties appeared in newer Qt versions, your implementation will not copy them in opposite to default clone
implementation. That could be a problem so this approach is not perfect.