Print a PDF file with Qt - c++

I know that there are similar questions about this issues but not a single one seems to address my problem.
I have two options In my widget: one that allows the user to save the display as PDF file and one that allows the user to print the display directly.
My problem is that there seems to be quite some minor differences if I print the saved PDF file with Windows vs when I print the interface with Qt. A possible solution that I begin to follow is that when I print the interface first to save the interface in PDF as a temporary file and after to try to print with Qt that file.
Unfortunately it seems that I can not use directly the PDF file without convert it in a pixmap or something similar.
This is how I manage to export in PDF format:
QTextDocument doc;
if (!ComputeDoc(doc))
return false;
QPrinter printer;
printer.setOutputFileName(filePath);
printer.setPaperSize(format);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setOrientation (orientation);
printer.setFullPage(true);
if(ok && !filePath.isEmpty())
{
QPainter painter(&printer);
painter.setBackgroundMode(Qt::TransparentMode);
painter.setRenderHint(
(QPainter::RenderHint)(int)(
QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::HighQualityAntialiasing |
QPainter::SmoothPixmapTransform),
true);
painter.setPen(Qt::white);
doc.drawContents(&painter);
}
And this how I originally try to print the file:
QTextDocument doc;
if (!ComputeDoc(doc))
return false;
QPrinter printer;
printer.setPaperSize(format);
printer.setOrientation (orientation);
printer.setFullPage(true);
QPrintDialog printDialog(&printer, this);
if(ok && printDialog.exec() == QDialog::Accepted)
{
QPainter painter(&printer);
painter.setBackgroundMode(Qt::TransparentMode);
painter.setRenderHint(
(QPainter::RenderHint)(int)(
QPainter::Antialiasing |
QPainter::TextAntialiasing |
QPainter::HighQualityAntialiasing |
QPainter::SmoothPixmapTransform),
true);
painter.setPen(Qt::white);
doc.drawContents(&painter);
}
As you can see from the print the inner frame is not centered and in the left part (the image is flipped to right) of the paper there is a line that in PDF is not. Sorry for this example but I can not post the entire page. I hope this will give a small clarification.
Does anyone know a solution to print a PDF file from the disk with qt? Or maybe how to make Windows to print to file from qt?

Related

File Not Being Created in QT C++ using QFileDialog

there I'm trying to make a simple notepad application. I've got a new file menu item. When the user clicks on that, it checks whether the text field is empty or not. If that is not empty then it prompts to save the work. If the user opts for yes then it asks for the location using QFileDialog. However, this code doesn't create the file at the provided destination. Can anyone figure out this code?
if(this->checkTextField()==false){ //checkks whether the textField is empty or not
QMessageBox::StandardButton reply = QMessageBox::question(this,this->appName,this->document_modified);
if(reply == QMessageBox::Yes){
QString filename = QFileDialog::getSaveFileName(this,"Save the File",QDir::homePath(),this->textFilter);
QFile file(filename);
QTextStream out(&file);
QString text = ui->textEdit->toPlainText();
out << text;
file.flush();
file.close();
}
}
Simply creating a QFile will neither create nor open a file on the filesystem. You have to open it to write something to it/read from it.

QPrinter output to pdf is not equivalent to the paper/page size (A4)

I am trying to output a QWidget ui to a pdf file like this;
const QString filename = "class1bills.pdf";
printer = new QPrinter(QPrinter::HighResolution);
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setOrientation(QPrinter::Portrait);
printer->setPaperSize(QPrinter::A4);
printer->setPageSize(QPrinter::A4);
printer->setPageMargins (15,15,15,15,QPrinter::Millimeter);
printer->setFullPage(false);
printer->setOutputFileName(filename);
painter = new QPainter(printer);
class1Bill->render(painter, QPoint(), QRegion(), QWidget::DrawChildren | QWidget::DrawWindowBackground);
painter->begin(printer);
painter->end();
assert(QFile::exists(filename));
class1Bill is an object of the class that inherits QWidget. Everything works fine but when i open the pdf file, i expect the widget ui to appear fitting the A4 page size but it appears very small that i didn't even see it at first glance. How do i make the widget ui fit the A4 paper size i set?
Set printer->setFullPage(true); in order to fit paper size

Qt: save entire QScrollArea contents as an image

I am trying to save the contents of a QScrollArea as an image.
Currently, I am doing it this way...
QPixmap pixmap(ui->overview->rect().size());
ui->overview->render(&pixmap, QPoint(),QRegion(ui->overview->rect()));
QString saveFilename = QFileDialog::getSaveFileName(this, "Save as", "Choose a filename","PNG(*.png);; TIFF(*.tiff *.tif);; JPEG(*.jpg *.jpeg)");
if(!pixmap.save(saveFilename))
{
QMessageBox::warning(this, "Error","File could not be saved", QMessageBox::Ok);
}
But if the contents exceed one screen (and you need to scroll to see the entire image) and I save that,
the image only shows the part of the screen it's currently on.
How can I save the full contents of the scrollArea so that the image shows the entire thing, and not just a part of it?
QImage img(ui->scrollAreaWidgetContents->size(),QImage::Format::Format_ARGB32);
QPainter painter(&img);
ui->scrollAreaWidgetContents->render(&painter);
bool istrue = img.save("/file.jpg");

QPrinter's PDF data, how to grab it?

As the title says, how do i grab the internal PDF data from QPrinter without going the extra mile of outputting to a temporary file and reading that in for further processing?
I have checked the documentation and found nothing what would let me do something like
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
QTextDocument doc;
doc.setHtml("<p>Test me!</p>");
doc.print(&printer);
QByteArray foo = printer.data();
Any ideas? :)
The only way I know would be by using QTemporaryFile
QPrinter printer(QPrinter::HighResolution);
printer.setPageSize(QPrinter::A4);
printer.setOutputFormat(QPrinter::PdfFormat);
QTemporaryFile pdf;
pdf.open();
printer.setOutputFileName(pdf.fileName());
QTextDocument doc;
doc.setHtml("<p>Test me!</p>");
doc.print(&printer);
QByteArray foo = pdf.readAll();

Importing the entries to be filled in the UI, from a file, in qt4

I created a UI in qt4. Now I should be giving an option to the user to fill the entries in the UI, from an existing file on system,which the user can browse for.
Now, Iam able to set the line edit entries in my UI from the file that the user specifies but I am not able to set the highlighted text in the comboboxes to what the file has. This might be very vague, Iam not able to explain this properly.Here is the code snippet i used:
//browsing for the file
path = QFileDialog::getOpenFileName(
this,
"Choose a file to import data from",
QString::null);
QFileInfo fi(path);
ui->lineEdit_21->setText( path );
//opening the file specified by user, for reading
name = fi.fileName();
dir = fi.path();
QDir::setCurrent(dir);
QFile read(name);
QString str;
if (!read.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QTextStream in(&read);
while (!in.atEnd())
{
QString line = in.readLine();
//filling the UI from the file
if(line.contains("AP SSID :", Qt::CaseInsensitive))
{
str = line.section(':', 1, 1);
ui -> lineEdit->setText(str);
}
}
This works fine but now how do I change the selected entry in a combobox, in accordance with the file?
if(line.contains("FREQUENCY :", Qt::CaseInsensitive))
{
str = line.section(':', 1, 1);
ui -> comboBox_2->setEditText(str);
}
I tried this but this is not working. My combobox_2 has two frequencies 2.4GHz and 5GHz. If the user selected file has 2.4GHz then I want the combobox to update itself such that 2.4GHz entry is highlighted. Hope I have made my point. Please help.
Thanks
You have to use the findText function in order to get the index of the given text in the combobox.
int frequencyIndex = ui->comboBox_2->findText(str);
if (frequnecyIndex != -1)
ui->comboBox_2->setCurrentIndex(frequencyIndex);