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. QPrinter
QtWS25 Last Chance

QPrinter

Scheduled Pinned Locked Moved Unsolved General and Desktop
qprinter
6 Posts 2 Posters 4.3k Views
  • 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.
  • collycrkC Offline
    collycrkC Offline
    collycrk
    wrote on last edited by
    #1

    I'm green around the 'gills'. And can not seem to get my arms around printing a form.
    The UI is simple and has three widgets on it.
    I can preview the form but it is blank, I can click the printer Icon on the preview and see the printer. But clicking print on the printer interface does not print anything, not even the blank form.
    Here is the code:
    void TestPrint::on_actionPrint_triggered()
    {
    QPrinter printer;

    printer.setOutputFormat(QPrinter::NativeFormat);
    printer.setOrientation(QPrinter::Portrait);
    
    QPrintPreviewDialog preview(&printer);
    
    preview.exec();
    

    }
    Will some one please help me, or just shoot me. I have spent months on this.
    I am using:
    Qt Creator 3.5.1 (opensource)
    Based on Qt 5.5.1 (MSVC 2013, 32 bit)
    Built on Oct 15 2015 01:55:47
    From revision b4c52505ca
    I
    Merry Christmas (2015) to any and all who may view this post.

    How to print a dialog in Qt seems to be beyond my comprehension. Would you like to teach me this one skill. If so, lets talk!

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

      hi
      I prefer not to shoot you :)
      Printing on QPrinter is much like drawing on screen.
      I see you are using QPrintPreviewDialog
      The trick with this object is that it will send a signal when it wants something to be printed
      void QPrintPreviewDialog::paintRequested(QPrinter * printer)
      so in your code you must hook up this signal
      connect(&preview, SIGNAL(paintRequested(QPrinter *)),this, SLOT(printPreview(QPrinter *)));
      and in the
      printPreview slot, you must use the QPrinter parameter to actually draw something.
      maybe read this for overview
      http://www.informit.com/articles/article.aspx?p=1405545&seqNum=5
      here is sample that show the actual "print" function
      http://www.dazzle.plus.com/linux/QtCreator/part11.htm

      so to print a from, you would also need to scale it a bit.
      since screen and paper have different resolution, you often want to scale the widgets up
      a bit else they get really tiny.
      http://doc.qt.io/qt-4.8/printing.html
      see Printing Widgets
      the
      myWidget->render(&painter);
      is the main thing to notice. it ask the widget to draw itself on the paint device which is
      connected to the printer.
      So you can do that for the form object.

      1 Reply Last reply
      0
      • collycrkC Offline
        collycrkC Offline
        collycrk
        wrote on last edited by
        #3

        Thank you mrjj for the information.

        This I have put into testprint.h;
        Q_OBJECT
        class QPrinter; // is this the correct place to put this?
        public: //Added this public myself.
        void on_action_Print_preview_triggered();

        This is in testprint.cpp:
        void TestPrint::on_action_Print_preview_triggered()
        {
        // display print preview dialog
        QPrinter printer( QPrinter::ScreenResolution );
        QPrintPreviewDialog preview( &printer, this );
        connect( &preview, SIGNAL(paintRequested(QPrinter*)), SLOT(print(QPrinter*)) );
        preview.exec();

        }
        Have not coded the print action at this point.
        I am getting a compile error on the bolded line above.
        testprint.cpp:40: error: incomplete type 'TestPrint::QPrinter' used in nested name specifier
        QPrinter printer( QPrinter::ScreenResolution );
        ^
        What does this error mean?

        How to print a dialog in Qt seems to be beyond my comprehension. Would you like to teach me this one skill. If so, lets talk!

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

          HI
          class QPrinter; //
          this looks like a forward declaration
          I don't think you need that.

          but you need
          #include <QPrinter>
          and
          Add to .pro:
          QT += printsupport

          for it to know the type.

          "incomplete type "
          Well i think you miss the #include <QPrinter>
          The incomplete part comes from the forward
          class QPrinter;
          as u tell there is a class called QPrinter. and that all it knows.
          So when you try to call a function in that class, it complains as
          you only told name. not any functions.
          But you just need include file and all be jolly

          1 Reply Last reply
          0
          • collycrkC Offline
            collycrkC Offline
            collycrk
            wrote on last edited by
            #5

            Here is all the code:
            .pro
            QT += core gui
            QT += printsupport

            greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

            TARGET = MyPrint
            TEMPLATE = app

            SOURCES += main.cpp
            testprint.cpp

            HEADERS += testprint.h

            FORMS += testprint.ui

            .h
            #ifndef TESTPRINT_H
            #define TESTPRINT_H

            #include <QMainWindow>

            namespace Ui {
            class TestPrint;
            }

            class TestPrint : public QMainWindow
            {
            Q_OBJECT

            public:
            void on_action_Print_preview_triggered();
            void on_action_Print_triggered();

            public:
            explicit TestPrint(QWidget *parent = 0);
            ~TestPrint();

            private slots:
            void on_pushButton_clicked();
            void on_action_Exit_triggered();

            private:
            Ui::TestPrint *ui;
            };

            #endif // TESTPRINT_H

            .cpp
            #include <QtPrintSupport/QAbstractPrintDialog> //Base implementation for print dialogs used to configure printers
            #include <QPageSetupDialog> //Configuration dialog for the page-related options on a printer
            #include <QPrintDialog> //Dialog for specifying the printer's configuration
            #include <QPrintEngine> //Defines an interface for how QPrinter interacts with a given printing subsystem
            #include <QPrintPreviewDialog> //Dialog for previewing and configuring page layouts for printer output
            #include <QPrintPreviewWidget> //Widget for previewing page layouts for printer output
            #include <QPrinter> //Paint device that paints on a printer
            #include <QPrinterInfo> //Gives access to information about existing printers
            #include <QPainter>

            TestPrint::TestPrint(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::TestPrint)
            {
            ui->setupUi(this);
            }

            TestPrint::~TestPrint()
            {
            delete ui;
            }

            void TestPrint::on_pushButton_clicked()
            {
            ui->lineEdit->setText("Merry Christmas, and a Happy new year.");
            }

            void TestPrint::on_action_Exit_triggered()
            {
            this->close();
            }

            void TestPrint::on_action_Print_preview_triggered()
            {
            // display print preview dialog
            QPrinter printer( QPrinter::ScreenResolution );
            QPrintPreviewDialog preview( &printer, this );
            connect( &preview, SIGNAL(paintRequested(QPrinter*)), SLOT(print(QPrinter*)) );
            preview.exec();
            }

            void TestPrint::on_action_Print_triggered()
            {
            // display print dialog and if accepted print
            QPrinter printer( QPrinter::ScreenResolution );
            QPrintDialog dialog( &printer, this );
            if ( dialog.exec() == QDialog::Accepted ) print( printer ); //errors print not declared in this scope

              /*************************************** print ***************************************/
            
              //void  MainWindow::print( QPrinter* printer )
              //{
                // create painter for drawing print page
            
                QPainter painter( &printer );
                int      w = printer.pageRect().width();
                int      h = printer.pageRect().height();
                QRect    page( 0, 0, w, h );
            
                // create a font appropriate to page size
                QFont    font = painter.font();
                font.setPixelSize( (w+h) / 100 );
                painter.setFont( font );
            
                // draw labels in corners of page
                painter.drawText( page, Qt::AlignTop    | Qt::AlignLeft, "QSimulate" );
                painter.drawText( page, Qt::AlignBottom | Qt::AlignLeft, QString(getenv("USER")) );
                //painter.drawText( page, Qt::AlignBottom | Qt::AlignRight,
                               //   QDateTime::currentDateTime().toString( Qt::DefaultLocaleShortDate ) );
            
                // draw simulated landscape
                page.adjust( w/20, h/20, -w/20, -h/20 );
                //painter.drawText( &painter, page );
                painter.drawText( &painter, page );       //testprint.cpp:78: error: no matching function for call to 'QPainter::drawText(QPainter*, QRect&)'
                 painter.drawText( &painter, page );
                                                  ^
              Thanks for your help. I feel closer to getting a printed page than ever.
            

            }

            The two lines with //errors has the error

            How to print a dialog in Qt seems to be beyond my comprehension. Would you like to teach me this one skill. If so, lets talk!

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

              Hi
              it seems you made print in MainWindow::print
              it should be in
              TestPrint::print( QPrinter* printer )

              That is why is says "print not declared in this scope" as
              u seem to have pasted the code below it ?

              That is ok. just correct code ( get rid of print call)

              so first error is that you have no print function.
              next error is that you miss the last argument for the text.
              so yes, you are close to printing :)

              if ( dialog.exec() == QDialog::Accepted )   
              {
                  // create painter for drawing print page
                  QPainter painter( &printer );
                  int      w = printer.pageRect().width();
                  int      h = printer.pageRect().height();
                  QRect    page( 0, 0, w, h );
              
                  // create a font appropriate to page size
                  QFont    font = painter.font();
                  font.setPixelSize( (w+h) / 100 );
                  painter.setFont( font );
              
                  // draw labels in corners of page
                  painter.drawText( page, Qt::AlignTop    | Qt::AlignLeft, "QSimulate" );
                  painter.drawText( page, Qt::AlignBottom | Qt::AlignLeft, QString(getenv("USER")) );
                  //painter.drawText( page, Qt::AlignBottom | Qt::AlignRight,
                                 //   QDateTime::currentDateTime().toString( Qt::DefaultLocaleShortDate ) );
              
                  // draw simulated landscape
                  page.adjust( w/20, h/20, -w/20, -h/20 ); 
                  painter.drawText( &painter, page );       //testprint.cpp:78: error: no matching function for call to 'QPainter::drawText(QPainter*, QRect&)'
                   // here you have no text??
                   // look at the others, you need the last argument  
              }                                      ^
              
              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