Results 1 to 8 of 8

Thread: What's wrong with QTextLayout ?!

  1. #1
    Join Date
    Oct 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Exclamation What's wrong with QTextLayout ?!

    Hi,
    My QTextDocument is full of text.
    However, when I run this code QTextLayout::lineCount() return always 0.

    Qt Code:
    1. QTextBlock block = document->begin();
    2. while(block.isValid())
    3. {
    4. QTextLayout *layout= block.layout();
    5. int nbL=layout->lineCount();
    6. qDebug( "count lines = %i", nbL );
    7. }
    To copy to clipboard, switch view to plain text mode 

    Am I missing something?
    What's wrong with it?

    Plz help me guys, thanks.

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

    Default Re: What's wrong with QTextLayout ?!

    What does block.text() return?
    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.


  3. #3
    Join Date
    Oct 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: What's wrong with QTextLayout ?!

    block.text() does return the paragraph of each block.

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

    Default Re: What's wrong with QTextLayout ?!

    Ok but I'm asking what it returns in your program
    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
    Oct 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: What's wrong with QTextLayout ?!

    Smart question !!
    Well, hmm ok, am sorry I forgot the "block= block.next" , just in the thread ..

    Here is a better exemple :

    Qt Code:
    1. QTextDocument* document= new QTextDocument();
    2. QTextCursor cursorTest(document);
    3. cursorTest.movePosition(QTextCursor::Start);
    4.  
    5. cursorTest.insertText("Hello there");
    6. cursorTest.insertBlock();
    7. cursorTest.insertText("How are you?");
    8. QTextBlock block = document->begin();
    9. while(block.isValid())
    10. {
    11. QTextLayout *layout= block.layout();
    12. qDebug()<<"Block content : "<<block.text() ;
    13. qDebug()<<"Nb lines : "<<layout->lineCount();
    14. block = block.next();
    15. }
    To copy to clipboard, switch view to plain text mode 

    this code return :
    >Block content : "Hello there"
    >Nb lines : 0
    >Block content : "How are you?"
    >Nb lines : 0

    is this normal?!

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

    Default Re: What's wrong with QTextLayout ?!

    It can be normal. The docs say not all layouts support counting lines. And I don't see how could the document calculate the number of lines of the block if you didn't set the width of the document. Use QTextDocument::setTextWidth() and see if it changes anything.
    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. #7
    Join Date
    Oct 2009
    Posts
    14
    Thanks
    3
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: What's wrong with QTextLayout ?!

    Unfortunately, setting the document text width didn't help.
    However, I noticed in the QTextDocument doc a warning mentioned for some other functions (not the lineCount() ) :
    Warning: This function only returns a valid value after the layout has been done.
    So, I tried to call lineCount() after drawing the doc and it works.
    This could explain the matter.

    But am still in trouble:
    Is there any way to "predict" the number of line of a block before being drawn?

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

    Default Re: What's wrong with QTextLayout ?!

    If you don't know the width of text then no, because the layout is delayed for as long as possible. For simple layouts you may use QFontMetrics to guess the needed width of text. Alternatively you may try somehow forcing the layout to recalculate without drawing it on the screen (or whatever your destination paint device is).
    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.


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

    Cortex (20th December 2009)

Similar Threads

  1. Replies: 1
    Last Post: 3rd February 2011, 11:38
  2. Replies: 7
    Last Post: 17th July 2009, 10:40
  3. QextSerialPort reading error: wrong values
    By Lawand in forum Qt Programming
    Replies: 9
    Last Post: 6th May 2009, 20:29
  4. mouseMoveEvent contains wrong position?
    By draftpunk in forum Qt Programming
    Replies: 10
    Last Post: 12th September 2008, 02:59
  5. Replies: 2
    Last Post: 2nd June 2008, 09:45

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.