Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Print text without "QTextDocument"
Forum Updated to NodeBB v4.3 + New Features

Print text without "QTextDocument"

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cccm62
    wrote on last edited by
    #1

    I try to print labels on a Zebra from Qt
    I use ZPL code to send directly to a printer "Generic / Text Only".

    For information, I noticed that:
    . When I print from Notepad (Windows): Printing is good
    . When I print from WordPad (Windows): Printing is the same as with Qt (add spaces)

    I think there must be something wrong with the layout engine (wordwrap? Text "formatted")

    So when I use this way QTextDocument

    @ QTextDocument doc ("ZPL Command");
    Doc.print (& printer);@

    Spaces are added in all text labels
    Will it have a way to print "as notepad"?

    NB: Same problem as: http://qt-project.org/forums/viewthread/36994/

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You could try with QPainter directly. Have a look at the "Printing with Qt" chapter of Qt's documentation.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cccm62
        wrote on last edited by
        #3

        Hi,

        Thanks for the reply, I found the time to test with QPainter, but nothing print. Is there an error in this code?

        @QWebElement e = frame->findFirstElement("...");
        // ....
        QPainter painter;
        painter.begin(&printer);
        painter.drawText(QPoint(50, 50), e.toPlainText());
        painter.end();@

        Otherwise, I partially solved the problem by setting the font
        With default font, many spaces are added. When I set it to "Courier" 7pt, and with CR before each text, most problems are solved:

        @QWebElement e = frame->findFirstElement("...");
        // ....
        QTextDocument doc;
        QFont newFont(doc.defaultFont());
        newFont.setFamily("Courier New");
        newFont.setPointSize(7);
        doc.setDefaultFont(newFont);
        doc.setPlainText(e.toPlainText());
        doc.print(&printer);@

        But it would certainly be better with QPainter

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Are you sure you have a valid printer ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cccm62
            wrote on last edited by
            #5

            Yes, it's the same for QPainter & QTextDocument

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              How do you create the printer ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • C Offline
                C Offline
                cccm62
                wrote on last edited by
                #7

                @QPrinter printer(QPrinter::HighResolution);
                printer.setPrinterName(printerName);
                printer.setPageMargins(0,0,0,0,QPrinter::Millimeter);@

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved