QPixmap canvas(width(), height());QPainter p;p.begin(&canvas);p.setPen(Qt::blue);p.drawText(0, 0, QString(text));p.end();imageLabel->setPixmap(canvas);
void ImageViewer::open(){ QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath()); QFile file(fileName); if (!file.open( QFile::ReadOnly | QFile::Text)) { QMessageBox::information(this, tr("HTTP"), tr("Unable to open the file %1: %2.") .arg(fileName).arg(file.errorString())); } QByteArray data = file.readAll(); QTextCodec *codec = QTextCodec::codecForName("Windows-1251"); QString str = codec->toUnicode(data); file.close(); QString text = str; QPixmap canvas(800, 600); QPainter p(&canvas); QColor color(0xff, 0xff, 0xff); QFont serifFont("MS Shell Dlg", 10); p.begin(&canvas); p.setPen(color); p.fillRect(0, 0, 800, 600, color); p.setFont(serifFont); for (int i = 0; i < text.size(); ++i) { //color.setHsv(255, 255, 255); QColor color(0, 0, 0); p.setPen(color); p.drawText(10, 20, QString(text)); } p.end(); imageLabel->setPixmap(canvas);
p.drawText( x, y, text );
p.drawText( 0, QFontMetrics( p.font() ).ascent(), text );
void ImageViewer::open(){ QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath()); QFile file(fileName); QString text; QTextStream stream(&file); QPixmap canvas(width(), height()); QPainter p(&canvas); QColor color(0xff, 0xff, 0xff); QFont serifFont("MS Shell Dlg", 8); p.begin(&canvas); p.setPen(color); p.fillRect(0, 0, width(), height(), color); p.setFont(serifFont); if (file.open( QFile::ReadOnly | QFile::Text)) { QString line; int k = 1; while ( !stream.atEnd() ) { line = stream.readLine(); // line of text excluding '\n' qDebug() << k++ << line; //text += line; for (int i = 0; i < line.size(); ++i) { //color.setHsv(255, 255, 255); QColor color(0, 0, 0); p.setPen(color); p.drawText(10, (k-1)*20, QString(line)); } } } else { QMessageBox::information(this, tr("HTTP"), tr("Unable to open the file %1: %2.") .arg(fileName).arg(file.errorString())); } QTextCodec *codec = QTextCodec::codecForName("Windows-1251"); file.close(); p.end(); imageLabel->setPixmap(canvas);
Q_D(QTextDocument) --> QTextDocumentPrivate* const d = d_func();
QPrinter printer(QPrinter::HighResolution); printer.setOrientation(QPrinter::Landscape); printer.setOutputFormat(QPrinter::PdfFormat); printer.setOutputFileName(fileName); QPainter painter(&printer); QRect rect = painter.viewport(); QSize size = imageLabel->pixmap()->size(); size.scale(rect.size(), Qt::KeepAspectRatio); painter.setViewport(rect.x(), rect.y(), size.width(), size.height()); painter.setWindow(imageLabel->pixmap()->rect()); painter.drawPixmap(0, 0, *imageLabel->pixmap());
printer.setOrientation(QPrinter::Landscape);