[SOLVED] QPlainTextEdit Print question

  • DUH. I mentally read height instead of y in line 34 in the code below. There is no problem and the code snippet will print out the text and is easily modified to print headers and footers.

    I'm a little embarrassed to ask this question but here goes. Some time ago I wrote some code to print out headers and footers for a plain text editor. Now I want to expand this to include line numbers but I forgot why some things in my original code works and I need to rediscover why it works (I understood it when I first wrote it, I think).

    One line which has nothing to do with headers or footers in particular stumps me. A simplified code snippet illustrates the problem:
    @// current printer supplied as an argument


    QPainter painter(currentPrinter);

    // set the page drawing area (smaller than the entire page area because of margins)
    QRect drawingArea(currentPrinter->pageRect());

    // find the content drawing area size for one page
    QSizeF pageContentSize(drawingArea.width(),drawingArea.height());

    // set the document page size

    // set up the bounding rectangles
    QRect contentRect=QRect(QPoint(0,0),printDocument->size().toSize());

    // translate the painter orgin to the drawing area

    // initialize the loop rectangle
    QRect currentRect=QRect(QPoint(0,0),pageContentSize.toSize());

    // loop until currentRect no longer intersects with contentRect
    // restore/save the painter and draw the current contents page
    painter.translate(0,-currentRect.y()); // ???

      // adjust y printer coordinate to print the next page by translating currentRect to the next page
      // advance to a new page if there is anything left to print

    }// end currentRect while loop

    The conceptual problem I have is line 34 in the above code.
    @ painter.translate(0,-currentRect.y()); // ??? @

    Looking at other code snippets such as:
    it appears that this line is unnecessary.

    Indeed if eliminate this line of code then the first page of the document is printed correctly. Unfortunately all subsequent pages are not printed correctly. If I include this line of code then every page is printed correctly.

    My question is why is this line of code necessary? In fact how does the first page get printed correctly when this line of code is included?

    Any help will be appreciated (FYI: I'm using 4.8 on windows 7).

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.