PDA

View Full Version : Pass string to print slot (QPrintPreviewDialog)



Rafaelpsmed
11th February 2015, 01:14
Hi

I have 2 qplaintextedit with 2 buttons. Each button will pass the text from one qplaintextedit to a string x and call the function printreport and the then print (thats the idea at least)
I'm trying to implement a QPreviewDialog like this:



void::mainwindow::printreport(QString x)
{
QPrinter printer;
printer.setPageSize(QPrinter::A4);
printer.setOrientation(QPrinter::Portrait);
printer.setPageMargins (10,10,10,10,QPrinter::Millimeter);

QPrintPreviewDialog preview(&printer);

connect(&preview, SIGNAL(paintRequested(QPrinter *)),this, SLOT(printpage(QPrinter*, QString x)));
preview.showMaximized();
preview.exec();

}

void mainwindow::printpage(QPrinter *printer, QString x)
{
QPainter painter(printer);

QRect reccomum(0,0,720,30);

painter.drawText(100, 100, "Hello World! 123");
painter.drawText(reccomum,Qt::AlignLeft,x);
}

This doesn't work. The printpreview dialog shows a gray area. I get this error message:
QObject::connect: No such slot mainwindow:: printpage(QPrinter*, QString x) in ..\Receitas\mainwindow.cpp:432
QObject::connect: (receiver name: 'mainwindow')

When i delete QString x both from void mainwindow:: printpage and from the signal:slot (that is: leaving only SLOT(print(QPrinter*)) the page is created, but without the text from the qplaintextedit.

My question is: how can i implement this printpage slot passing the string x?

Thanks in advance.

anda_skoa
11th February 2015, 08:04
A slot can never have more arguments than the signal it is connected to.
How would the computer know at the point of the emit which value to pass?

Couple of options:
You can store the string in a member of mainwindow.
You can store the string as a dynamic property on the QPrinter.
You could delegate printing to an instance of a specialized class that has the string to print as its member.

Cheers,
_