1

I need to build a widget, which bases on QTextDocument.

MyWidget.h

class MyWidget: public QFrame
{
public:
   explicit MyWidget( QWidget *p_parent );

private:
   QTextDocument m_textDocument;
};

MyWidget.cpp

MyWidget::MyWidget( QWidget *p_parent ) : QFrame( p_parent )
{
  QVBoxLayout *layout = new QVBoxLayout( this );
  layout->setMargin( 0 );
  layout->setSpacing( 0 );

  m_textDocument = new QTextDocument( this );
  layout->addWidget( m_textDocument );   // does not work
  layout->addLayout(m_textDocument->documentLayout());  // I have tried this, but incompatible
}

I can not addWidget because QTextDocument is an object. How can I do to build my widget?

songvan
  • 369
  • 5
  • 21

1 Answers1

4

You don't use a QTextDocument as a widget, because a Document is the idea in memory of what a document is, it does not have a Graphicsl Representation.

If you are looking for a Visual representation of a Document, you can use QTextEdit as follows:

auto *textEdit = new QTextEdit(parent);
textEdit->setDocument(myTextDocument);
layout->addWidget(textEdit);
Tomaz Canabrava
  • 2,320
  • 15
  • 20