Results 1 to 11 of 11

Thread: QTextDocuments and QPlainTextEdits

  1. #1
    Join Date
    Jul 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTextDocuments and QPlainTextEdits

    I'm using Qt Creator with Qt4. That's what I use to compile, build, and design the interface. This is in C++.

    I have a simple problem. When I try to call setDocument on a QPlainTextEdit, it will compile, but during runtime, when I open a file it says:
    Qt Code:
    1. QPlainTextEdit::setDocument: Document set does not support QPlainTextDocumentLayout
    To copy to clipboard, switch view to plain text mode 

    Here's the snippet of code that does this:
    Qt Code:
    1. void CodeView::loadFile(QString filename)
    2. {
    3. QFile file(filename);
    4. if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    5. QString text = file.readAll();
    6. QTextDocument document;
    7. document.setPlainText(text);
    8. ui->plainTextEdit->setDocument(&document);
    9. }
    10. file.close();
    11. }
    To copy to clipboard, switch view to plain text mode 

    If I call the setPlainText method on my QTextDocument, should it not already be in QPlainTextDocumentLayout?

    Thanks,
    Jordy

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextDocuments and QPlainTextEdits

    i think you should create the QTextDocument on heap instead of stack

    Qt Code:
    1. QTextDocument* document = new QTextDocument(ui->plainTextEdit) ;
    2. document->setPlainText(text);
    3. ui->plainTextEdit->setDocument(document);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextDocuments and QPlainTextEdits

    Thank you for the tip, although it doesn't fix the problem and I still get the same error.

    EDIT: This is my new function definition, by the way:
    Qt Code:
    1. void CodeView::loadFile(QString filename)
    2. {
    3. QFile file(filename);
    4. if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    5. QString text = file.readAll();
    6. QTextDocument *document = new QTextDocument(ui->plainTextEdit);
    7. document->setPlainText(text);
    8. ui->plainTextEdit->setDocument(document);
    9. }
    10. file.close();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by JordyD; 8th July 2009 at 16:31. Reason: updated contents

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextDocuments and QPlainTextEdits

    Why do you want to set a QTextDocument anyway? Simple us:
    Qt Code:
    1. void CodeView::loadFile(QString filename)
    2. {
    3. QFile file(filename);
    4. if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    5. ui->plainTextEdit->setPlainText(file.readAll());
    6. file.close();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextDocuments and QPlainTextEdits

    äh, and to your original question:
    Quote Originally Posted by JordyD View Post
    when I open a file it says:
    Qt Code:
    1. QPlainTextEdit::setDocument: Document set does not support QPlainTextDocumentLayout
    To copy to clipboard, switch view to plain text mode 
    see the docs where you can read:
    The document must have a document layout that inherits QPlainTextDocumentLayout (see QTextDocument::setDocumentLayout()).
    and you havn't set this.

  6. #6
    Join Date
    Jul 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextDocuments and QPlainTextEdits

    Quote Originally Posted by Lykurg View Post
    Why do you want to set a QTextDocument anyway? Simple us:
    Qt Code:
    1. void CodeView::loadFile(QString filename)
    2. {
    3. QFile file(filename);
    4. if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    5. ui->plainTextEdit->setPlainText(file.readAll());
    6. file.close();
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 
    I also have a class that inherits from QSyntaxHighlighter, and it only accepts QTextDocuments to highlight.

  7. #7
    Join Date
    Jul 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextDocuments and QPlainTextEdits

    Thanks! I got it now.

    Here's what it looks like:
    Qt Code:
    1. void CodeView::loadFile(QString filename)
    2. {
    3. QFile file(filename);
    4. if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    5. QString text = file.readAll();
    6. QTextDocument *document = new QTextDocument(ui->plainTextEdit);
    7. document->setPlainText(text);
    8. QPlainTextDocumentLayout *layout = new QPlainTextDocumentLayout(document);
    9. document->setDocumentLayout(layout);
    10. ui->plainTextEdit->setDocument(document);
    11. }
    12. file.close();
    13. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QTextDocuments and QPlainTextEdits

    Quote Originally Posted by JordyD View Post
    I also have a class that inherits from QSyntaxHighlighter, and it only accepts QTextDocuments to highlight.
    Might be, but in your function you create a local document and set it and then forget about it. And there is no need to do so. Really, better use setPlainText(). This also creates/alters the existing document. For your other class you can still use document().

    EDIT: but if you really want use this, instead of creating a new one, just use the old one!

  9. #9
    Join Date
    Jul 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextDocuments and QPlainTextEdits

    Quote Originally Posted by Lykurg View Post
    Might be, but in your function you create a local document and set it and then forget about it. And there is no need to do so. Really, better use setPlainText(). This also creates/alters the existing document. For your other class you can still use document().

    EDIT: but if you really want use this, instead of creating a new one, just use the old one!
    I took that advice, and now my function is like this:
    Qt Code:
    1. void CodeView::loadFile(QString filename)
    2. {
    3. QFile file(filename);
    4. if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
    5. ui->plainTextEdit->setPlainText(file.readAll());
    6. }
    7. file.close();
    8. }
    To copy to clipboard, switch view to plain text mode 

    Question: Where do I throw the highlighter initialization (
    Qt Code:
    1. Highlighter highlighter(ui->plainTextEdit->document());
    To copy to clipboard, switch view to plain text mode 
    )? CodeView's constructor?

  10. #10
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QTextDocuments and QPlainTextEdits

    wherever but only if you create it on heap:
    Qt Code:
    1. new SuperDuperHighlighter(ui->plainTextEdit->document())
    To copy to clipboard, switch view to plain text mode 
    because QTextDocument becomes a parent so will delete highlighter in destructor.
    And the parent of QTextDocument (QPlainTextEdit would be my guess) will delete QTextDocument, and so on, so if you just take care of passing parents correctly then you can make on stack only MainWindow and it would be grandgrandgrand...parent to any object so they can be created on heap and would be deleted at the end of main().
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  11. #11
    Join Date
    Jul 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTextDocuments and QPlainTextEdits

    Quote Originally Posted by faldżip View Post
    wherever but only if you create it on heap:
    Qt Code:
    1. new SuperDuperHighlighter(ui->plainTextEdit->document())
    To copy to clipboard, switch view to plain text mode 
    because QTextDocument becomes a parent so will delete highlighter in destructor.
    And the parent of QTextDocument (QPlainTextEdit would be my guess) will delete QTextDocument, and so on, so if you just take care of passing parents correctly then you can make on stack only MainWindow and it would be grandgrandgrand...parent to any object so they can be created on heap and would be deleted at the end of main().
    Neat.

    I'll put it in CodeView's constructor, so whenever the contents are re-loaded it will be called to highlight.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.