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. [Solved] QPrinter Simple Example Failure: QPainter::begin returned false
QtWS25 Last Chance

[Solved] QPrinter Simple Example Failure: QPainter::begin returned false

Scheduled Pinned Locked Moved General and Desktop
10 Posts 3 Posters 12.6k 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.
  • E Offline
    E Offline
    edhana
    wrote on 12 Aug 2014, 17:19 last edited by
    #1

    HI,
    I'm trying a simple example of how to print using QPrinter(Qt 5.3.1) in Linux Ubuntu 12.04 without showing the QPrintDialog, but it seems that I'm missing something in the configuration. My project setup seems to be correct, I can build and run the project. I manage to print to a PDF file successfully, but when I try to print to a network printer (which is the default printer of my system) I get the following error: QPainter::begin(): Returned false

    This error is thrown in the following line: @document.print(&printer);@

    This error occur even if I set the printer name and other configurations like page size and orientarion, and even if I show/use the QPrintDialog to configure the printer.

    Do you know what can I be missing?

    My complete code:
    mainwindow.cpp

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtPrintSupport/QPrintDialog>
    #include <QtPrintSupport/QPrinterInfo>
    #include <QPainter>
    #include <QDebug>
    #include <QTextDocument>

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(released()), this, SLOT(print()));
    }

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

    void MainWindow::print()
    {

    QTextDocument document;
    document.setHtml("testing");
    
    QPrinter printer;
    QPrintDialog dialog(&printer, this);
    document.print(&printer);
    

    }@

    mainwindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtPrintSupport/QPrinter>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

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

    public slots:
    void print();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H@

    printer-test.pro:
    @QT += core gui

    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    qtHaveModule(printsupport): QT += printsupport

    TARGET = printer-test
    TEMPLATE = app

    SOURCES += main.cpp
    mainwindow.cpp

    HEADERS += mainwindow.h

    FORMS += mainwindow.ui@

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 12 Aug 2014, 18:17 last edited by
      #2

      Hi,

      What happens if you show the dialog ?

      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
      • E Offline
        E Offline
        edhana
        wrote on 12 Aug 2014, 18:57 last edited by
        #3

        I did change the code to show the dialog, but got the same error.

        Code:
        @void MainWindow::print()
        {
        QTextDocument document;
        document.setHtml("testing");

        QPrinter printer;
        QPrintDialog dialog(&printer, this);
        if(dialog.exec&#40;&#41; == QDialog::Accepted){
            document.print(&printer);
        }
        

        }@

        Error: QPainter::begin(): Returned false

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 12 Aug 2014, 20:14 last edited by
          #4

          Did you check that the printer returned has the correct parameters like printProgram or printerName ?

          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
          • E Offline
            E Offline
            edhana
            wrote on 13 Aug 2014, 11:29 last edited by
            #5

            SGaist, thanks for your feedback. I changed my print function to this:

            @void MainWindow::print()
            {
            QTextDocument document;
            document.setHtml("testing");

            QPrinter printer;
            QPrintDialog dialog(&printer, this);
            if(dialog.exec&#40;&#41; == QDialog::Accepted&#41;{
                QPrinterInfo pinfo(printer&#41;;
                qDebug() << "Printer valid: " << printer.isValid();
                qDebug() << "Printer Name: " << printer.printerName();
                qDebug() << "Printer program: " << printer.printProgram();
                qDebug() << "Is printer null: " << pinfo.isNull();
                qDebug() << "Printer State: " << pinfo.state();
                qDebug() << "Is printer default: " << pinfo.isDefault();
                qDebug() << "Is printer remote: " << pinfo.isRemote();
            
                document.print(&printer);
            }
            

            }@

            and I got the following response when selecting the default remote printer of my OS from the QPrintDialog.

            Printer valid: true
            Printer Name: ""
            Printer program: ""
            Is printer null: true
            Printer State: 3
            Is printer default: false
            Is printer remote: false
            QPainter::begin(): Returned false

            The same occur with the other printers of my system. The save to PDF option return the same result.

            update: I tried to use the cups library as a workaround to print to the default printer and everything worked very well.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 13 Aug 2014, 11:37 last edited by
              #6

              Looks like you have no printer

              Does it happen when running your code from Qt Creator ?

              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
              • E Offline
                E Offline
                edhana
                wrote on 13 Aug 2014, 12:16 last edited by
                #7

                As I said in the update of the last reply. I managed to print using the default cups development library( with the cups/cups.h header).

                Yes I'm running the code from the Qt Creator.

                I changed the code again to use the QPrinterInfo class. This is what I did:

                @void MainWindow::print()
                {
                QTextDocument document;
                document.setHtml("testing");

                QStringList pnames = QPrinterInfo::availablePrinterNames();
                QString defaultPrinterName = QPrinterInfo::defaultPrinterName();
                QPrinter *printer;
                
                for(int i =0; i < pnames.size(); i++){
                    qDebug() << "Listed Printer Name: " << pnames.at(i);
                    if(pnames.at(i) == defaultPrinterName){
                        qDebug() << "Default printer found: " << defaultPrinterName;
                        printer = new QPrinter;
                        printer->setPrinterName(defaultPrinterName);
                    }
                }
                
                if(printer){
                    QPrinterInfo pinfo(*printer);
                    qDebug() << "Printer valid: " << printer->isValid();
                    qDebug() << "Printer Name: " << printer->printerName();
                    qDebug() << "Printer program: " << printer->printProgram();
                    qDebug() << "Is printer null: " << pinfo.isNull();
                    qDebug() << "Printer State: " << pinfo.state();
                    qDebug() << "Is printer default: " << pinfo.isDefault();
                    qDebug() << "Is printer remote: " << pinfo.isRemote();
                }
                

                }@

                The response was:
                Listed Printer Name: "Enviar_para_o_OneNote_2010:4"
                Listed Printer Name: "Fax:3"
                Listed Printer Name: "HP_LJ300-400_color_M351-M451_PCL_6:2"
                Default printer found: "HP_LJ300-400_color_M351-M451_PCL_6:2"
                Listed Printer Name: "Microsoft_XPS_Document_Writer:1"

                Printer valid: true
                Printer Name: ""
                Printer program: ""
                Is printer null: true
                Printer State: 3
                Is printer default: false
                Is printer remote: false

                So even using the name retrieve by the system to set the printer the QPrinter object is not properly configured.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 13 Aug 2014, 21:50 last edited by
                  #8

                  You should rather use something like:

                  @printer = new QPrinter(QPrinterInfo::printerInfo(defaultPrinterName));@

                  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
                  • H Offline
                    H Offline
                    hdeldar
                    wrote on 14 Oct 2014, 06:37 last edited by
                    #9

                    I solved this problem by copying the "<QtDir>\5.3\msvc2013\plugins\printsupport" folder in to exe directory.

                    1 Reply Last reply
                    0
                    • E Offline
                      E Offline
                      edhana
                      wrote on 14 Oct 2014, 13:38 last edited by
                      #10

                      Thanks hdeldar. I did the same. Just forgot to post the result.

                      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