Here a small code with the same problem; press F8 to print.
#include <QApplication>
#include <QDialog>
#include <QtGui>
{
Q_OBJECT
public:
private:
void magToPdf();
};
{
label
= new QLabel("QT Rules!");
label->setStyleSheet("font-size: 80px");
horizontal->addStretch();
horizontal->addWidget(label);
horizontal->addStretch();
vertical->addStretch();
vertical->addLayout(horizontal);
vertical->addStretch();
setLayout(vertical);
setWindowTitle("Simple QPrint");
setFixedSize(900,667);
}
{
if(event->key() == Qt::Key_F8)
magToPdf();
}
void Dialog::magToPdf()
{
printer.
setOrientation(QPrinter::Landscape);
printer.setOutputFileName(windowTitle() + ".pdf");
printer.
setOutputFormat(QPrinter::PdfFormat);
if(qpm.isNull()) {
return;
}
p.begin(&printer);
p.drawPixmap(85, 32, qpm);
p.end();
}
int main(int argc, char *argv[])
{
Dialog dialog;
dialog.show();
return app.exec();
}
#include "main.moc"
#include <QApplication>
#include <QDialog>
#include <QtGui>
class Dialog: public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent=0);
private:
QLabel *label;
void keyPressEvent(QKeyEvent *event);
void magToPdf();
};
Dialog::Dialog(QWidget *parent)
:QDialog(parent)
{
label = new QLabel("QT Rules!");
label->setStyleSheet("font-size: 80px");
QHBoxLayout *horizontal = new QHBoxLayout;
horizontal->addStretch();
horizontal->addWidget(label);
horizontal->addStretch();
QVBoxLayout *vertical = new QVBoxLayout;
vertical->addStretch();
vertical->addLayout(horizontal);
vertical->addStretch();
setLayout(vertical);
setWindowTitle("Simple QPrint");
setFixedSize(900,667);
}
void Dialog::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_F8)
magToPdf();
}
void Dialog::magToPdf()
{
QPrinter printer;
printer.setOrientation(QPrinter::Landscape);
printer.setOutputFileName(windowTitle() + ".pdf");
printer.setOutputFormat(QPrinter::PdfFormat);
QPixmap qpm = QPixmap::grabWidget(this);
if(qpm.isNull()) {
QMessageBox::critical(0, "Error", "Can not capture the window", QMessageBox::Cancel);
return;
}
QPainter p;
p.begin(&printer);
p.drawPixmap(85, 32, qpm);
p.end();
}
int main(int argc, char *argv[])
{
QApplication app( argc, argv );
Dialog dialog;
dialog.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Bookmarks