[Qt-interest] Model/View with QTextDocument

Stephen Kelly steveire at gmail.com
Fri May 7 02:35:25 CEST 2010


Oliver Heins wrote:

> Hi,
> 
> I wonder whether it's possible to use Qt's Model/View mechanisms in
> conjunction with a QTextDocument.
> 
> My data should be view- and editable in several QTextEdits (or
> subclasses of it), where parts of the data could be displayed in one or
> more QTextEdits.
> 
> Basically, QTextFragments could be linked with the Model:
> 
> <fragment>Hey, this is some text in the document.</fragment><fragment>
> And this is some other.</fragment>
> 
> where each <fragment></fragment> links to a part of data in the model.
> 
> The main problem I see is that new fragments get created (which should
> be okay) and destroyed (that would be fatal, because the linkage to the
> model would be destroyed) on the fly by QTextDocument.  So the above
> example has to remain stable and must not be converted to this:
> 
> <fragment>Hey, this is some text in the document.  And this is some
> other.</fragment>

I don't see the problem here. Why would the destruction of a QTextFragment 
be such a bad thing?

As to the issue you're trying to solve, I think it should be possible, but 
not in a very nice way. Ideally the QTextCursor would have a virtual 
interface for things like insertText etc so that you could intercept all 
actions on the document and forward them to the model, bypassing the 
document. The model would then forward the changes back to the 
QTextDocuments in both QTextEdits. That's not possible though. In fact do 
you really need a model? You can just share QTextDocuments. Otherwise you 
can do what I imagine they do and listen to the documentChange signal and 
try to process that.

#include "mainwindow.h"

#include <QApplication>

int main(int argc, char **argv)
{
  QApplication app(argc, argv);
  
  MainWindow mw;
  mw.show();

  return app.exec();
}


#include "mainwindow.h"

#include <QTextEdit>
#include <QSplitter>
#include <QHBoxLayout>

MainWindow::MainWindow()
{
  QHBoxLayout *layout = new QHBoxLayout(this);
  QSplitter *splitter = new QSplitter(this);
  layout->addWidget(splitter);

  QTextEdit *te1 = new QTextEdit(splitter);
  QTextEdit *te2 = new QTextEdit(splitter);
  te1->setDocument(te2->document());

}

#ifndef MW_H
#define MW_H

#include <QWidget>

class MainWindow : public QWidget
{
  Q_OBJECT
public:
  MainWindow();

};

#endif


> 
> Any chance to achieve this?
> 
> TIA,
>  olli
> 





More information about the Qt-interest-old mailing list