Есть главный виджет приложения у которого есть функция print, выглядящая следующи образом:
void print(QPrinter * printer) {
QPainter painter;
painter.begin(printer);
QFont norm("Courier New", 8, QFont::Normal);
QFont bold("Courier New", 8, QFont::Normal);
painter.setFont(QFont("Times", 10, QFont::Normal));
painter.drawText(20, 20, tr("Events Log generated ") +
QDate::currentDate().toString("dd.MM.yyyy") + tr(" at ") + QTime::currentTime().toString("hh:mm:ss") + "\n" + "\n");
QList<int> maxStrList;
m_model->getColumnsMaxLength(maxStrList);
int x = 50, y = 60;
for(int i = 0; i<m_model->rowCount(); i++)
{
if(i != 0)
{
y = y+20;
x = 50;
}
for(int j = 0; j<m_model->columnCount(); j++)
{
if(j == 0)
painter.setFont(norm);
else
painter.setFont(bold);
if(y > printer->pageRect().height())
{
printer->newPage();
y = 60;
painter.setFont(QFont("Times", 10, QFont::Normal));
painter.drawText(20,20,tr("Error Log generated ") + QDate::currentDate().toString("dd.MM.yyyy") + tr(" at ") + QTime::currentTime().toString("hh:mm:ss") + "\n" + "\n");
}
if(m_model->getEventItem(i).getValue(j).isEmpty() == false)
{
QString dataStr = calclulateSpacing(m_model->getEventItem(i).getValue(j),maxStrList.at(j));
dataStr = dataStr.trimmed();
if(x + (dataStr.size()*8) > printer->pageRect().width())
{
x = printer->pageRect().width() - dataStr.size()*8;
y = y + 20;
}
painter.drawText(x,y, dataStr);
x = x+(dataStr.size()*8);
}
}
}
painter.end();
}
void onPrint() {
QPrinter printer;
if (QPrintDialog(&printer, this).exec() == QDialog::Accepted)
print(&printer);
}
Посмотри на QPrintPreviewDialog там есть и пример.
Посмотри на QPrintPreviewDialog там есть и пример.
Не нашел.
Переделал функцию onPrint() в :
QPrinter printer;
QPrintPreviewDialog * pd = new QPrintPreviewDialog(&printer, this);
connect(pd,SIGNAL(paintRequested()), this, SLOT(print(QPrinter *)));
if(pd->exec() == QDialog::Accepted)
print(&printer);
Не совсем понятно из описания на какой слот завязать сигнал паинт реквестед.:
The QPrintPreviewDialog class provides a dialog for previewing and configuring page layouts for printer output.
Using QPrintPreviewDialog in your existing application is straightforward:
Create the QPrintPreviewDialog.
You can construct a QPrintPreviewDialog with an existing QPrinter object, or you can have QPrintPreviewDialog create one for you, which will be the system default printer.
Connect the paintRequested() signal to a slot.
When the dialog needs to generate a set of preview pages, the paintRequested() signal will be emitted. You can use the exact same code for the actual printing as for having the preview generated, including calling QPrinter::newPage() to start a new page in the preview. Connect a slot to the paintRequested() signal, where you draw onto the QPrinter object that is passed into the slot.
Call exec().
Call QPrintPreviewDialog::exec() to show the preview dialog.
Нашел решение, всем спасибо, можно тему закрывать. Кому интересно решение по этой ссылке : http://www.qtcentre.org/threads/16069-Pratical-example-of-QPrintPreviewDialog
Форум Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)