Laying out text for letter to be displayed and printed



  • I am trying to port a project of mine from java to cpp/qt

    Part of this involves displaying a page of text which includes a customer invoice with text in different fonts. In java I used a graphics2g pageformat and it was fairly straightforward working out the positions for text and laying out the text line by line. The page could then be sent to the screen or the printer.

    As a tyro in QT I do not know how to approach this and the documentation and examples are a little daunting. Should I be using a QGraphicsView to layout the text or a document or scribe class. I am having difficulty finding exactly where to start and what documentation will take me through this.

    Alan


  • Moderators

    The easiest would be to use a QPainter to do the drawing. It can draw to either a QPixmap to display on the screen or QPrinter for printing.

    Something along the lines of:
    @
    void MyClass::drawStuff(QPaintDevice& device) {
    QPainter p(&device);
    //use p to draw stuff
    }

    void MyClass::drawToScreen() {
    QPixmap pixmap(width, height);
    drawStuff(pixmap);
    //use the pixmap to display, e.g.
    someLabel->setPixmap(pixmap);
    }

    void MyClass::drawToPrinter {
    QPrinter printer;
    QPrintDialog dlg(&printer);
    if(dlg.exec() == QDialog::Accepted)
    drawStuff(printer);
    }
    @


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The code accompanying chapter 10 of "Advanced Qt Programming":http://www.qtrac.eu/aqpbook.html explains the various possibility you have quiet nicely.

    You also have several report generation libraries that you can use.

    Hope it helps



  • Thanks. I'll give them both a try.


Log in to reply
 

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