[Solved] QPrinter Simple Example Failure: QPainter::begin returned false
-
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 falseThis 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_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();public slots:
void print();private:
Ui::MainWindow *ui;
};#endif // MAINWINDOW_H@
printer-test.pro:
@QT += core guigreaterThan(QT_MAJOR_VERSION, 4): QT += widgets
qtHaveModule(printsupport): QT += printsupportTARGET = printer-test
TEMPLATE = appSOURCES += main.cpp
mainwindow.cppHEADERS += mainwindow.h
FORMS += mainwindow.ui@
-
Hi,
What happens if you show the dialog ?
-
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() == QDialog::Accepted){ document.print(&printer); }
}@
Error: QPainter::begin(): Returned false
-
Did you check that the printer returned has the correct parameters like printProgram or printerName ?
-
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() == QDialog::Accepted){ 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(); 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 falseThe 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.
-
Looks like you have no printer
Does it happen when running your code from Qt Creator ?
-
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: falseSo even using the name retrieve by the system to set the printer the QPrinter object is not properly configured.
-
You should rather use something like:
@printer = new QPrinter(QPrinterInfo::printerInfo(defaultPrinterName));@