Results 1 to 9 of 9

Thread: Crazy QPlainTextEdit::document() behavior

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

    Default Crazy QPlainTextEdit::document() behavior

    Hi,

    I have following class (simplified...):
    Qt Code:
    1. class MyLabel : public QLabel
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit MyLabel(QWidget *parent = 0, Qt::WindowFlags f = 0);
    6.  
    7. protected:
    8. void paintEvent(QPaintEvent *event);
    9.  
    10. private:
    11. QPlainTextEdit *m_doc; //it is hidden, only for storing reason.
    12. };
    To copy to clipboard, switch view to plain text mode 

    So assume there is some content in the text edit and I do this:
    Qt Code:
    1. void MyLabel::paintEvent(QPaintEvent *event)
    2. {
    3. Q_UNUSED(event);
    4. QPainter p(this);
    5. m_doc->document()->setPageSize(rect().size());
    6. m_doc->document()->drawContents(&p, rect());
    7. }
    To copy to clipboard, switch view to plain text mode 
    nothing is shown, but when I have this:
    Qt Code:
    1. void MyLabel::paintEvent(QPaintEvent *event)
    2. {
    3. Q_UNUSED(event);
    4. QPainter p(this);
    5. QTextDocument *d = m_doc->document()->clone();
    6. d->setPageSize(rect().size());
    7. d->drawContents(&p, rect());
    8. d->deleteLater();
    9. }
    To copy to clipboard, switch view to plain text mode 
    everything is painted well. But why I have to clone the document??? That's not very performant... What might be the reason why I am not able to use QPlainTextEdit::document() directly?

    Thanks

  2. #2
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2
    Thanked 21 Times in 21 Posts

    Default Re: Crazy QPlainTextEdit::document() behavior

    Maybe this link could be useful to you.
    http://www.mail-archive.com/pyqt@riv.../msg19787.html

    excerpt :
    And yet in the latter case, drawContents() renders nothing at all. Cursory
    investigation points to a different QAbstractTextDocumentLayout implementation
    under the hood depending on the TextEdit flavor in use.
    Last edited by toutarrive; 10th March 2010 at 17:10.

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

    Default Re: Crazy QPlainTextEdit::document() behavior

    Ok thanks, that is really strange. But I feel better now, that I am not the only one having that trouble...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Crazy QPlainTextEdit::document() behavior

    Why don't you use QTextDocument directly? What do you need QPlainTextEdit for?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Crazy QPlainTextEdit::document() behavior

    Quote Originally Posted by wysota View Post
    Why don't you use QTextDocument directly? What do you need QPlainTextEdit for?
    Today I figured out that my way wont work - beside that issue So I don't need it any more but I have had to use QPlainTextEdit since I need the functionality of getting the underlaying word at a specific point (mousepress) and therefore QPlainTextEdit::cursorForPosition() was perfect.

    So for future: Do you know such a feature (QPoint -> QTextCursor) with QTextDocument?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Crazy QPlainTextEdit::document() behavior

    QTextDocument has a layout which has a QAbstractTextDocumentLayout::hitTest() method that returns a position in the document based on coordinates. Knowing the position you can create a QTextCursor and use QTextCursor::setPosition() to position the cursor in appropriate place so that you can fetch the word underneath.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    Lykurg (11th March 2010)

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

    Default Re: Crazy QPlainTextEdit::document() behavior

    Great! Thanks a lot. Haven't know about the hitTest() function.

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Crazy QPlainTextEdit::document() behavior

    Until I had a look at the docs today, I have been unaware of it as well "Use the docs, Luke... use the docs..."
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Crazy QPlainTextEdit::document() behavior

    Quote Originally Posted by wysota View Post
    "Use the docs, Luke... use the docs..."
    Unfortunately my name is not Luke.
    Yeah, ok, temporary - hope so - incompetence...

Similar Threads

  1. QPlainTextEdit
    By Carlsberg in forum Qt Programming
    Replies: 4
    Last Post: 13th June 2009, 07:12
  2. Help document
    By weixj2003ld in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2009, 13:04
  3. a crazy problem~~~who can help me : (
    By sunking_426 in forum Qt Programming
    Replies: 4
    Last Post: 2nd January 2009, 15:35
  4. Crazy over the QGraphicsScene/Item
    By Morea in forum Qt Programming
    Replies: 6
    Last Post: 20th November 2006, 10:18

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
  •  
Qt is a trademark of The Qt Company.