So I needed border in QtextFrame in a pdf (QPrinter's pdf output). Following is the code I used
#include <QPainter>
#include <QTextFrameFormat>
#include <QTextCursor>
#include <QTextFrame>
#include <QTextEdit>
#include <QPrinter>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QTextDocument doc;
QPixmap map(1024, 1024);
map.fill(Qt::white);
QPainter p;
p.begin(&map);
p.fillRect(QRect(0,0, 4, map.height()), QBrush(Qt::red));
p.end();
QTextFrameFormat frameFormat;
QBrush bruh(map);
bruh.setColor(Qt::transparent);
frameFormat.setBackground(bruh);
frameFormat.setPadding(6);
auto cur = new QTextCursor(&doc);
auto frame = cur->insertFrame(frameFormat);
auto curf = new QTextCursor(frame);
curf->insertText("Hello this is qt program!");
QPrinter pdf;
pdf.setOutputFileName("E:\\test.pdf");
pdf.setOutputFormat(QPrinter::PdfFormat);
doc.print(&pdf);
QTextEdit edt;
edt.setDocument(&doc);
edt.show();
return a.exec();
}
But the pdf output is different from the what is shown in QTextEdit
pdf (Only a single black line no content)
Text Edit output which is what I needed
Related
I want to create a PDF with Qt. This is my code:
#include <QPdfWriter>
#include <QTextDocument>
int main() {
QPdfWriter pdfWriter{R"(C:\TEMP\test.pdf)"};
QTextDocument document;
document.setPlainText("Hello world");
document.print(&pdfWriter);
}
It crashes on the last line with
QPaintDevice: Cannot destroy paint device that is being painted
What did I do wrong?
You didn't create a QApplication. Change the code to
#include <QApplication>
#include <QPdfWriter>
#include <QTextDocument>
int main(int argc, char *argv[]) {
QApplication app{argc, argv};
QPdfWriter pdfWriter{R"(C:\TEMP\test.pdf)"};
QTextDocument document;
document.setPlainText("Hello world");
document.print(&pdfWriter);
}
and it will create the PDF. Running app.exec() is not required.
I am trying to draw only the left border in QTextDocument. Since afaik QTextFrame doesn't support selective border so I am trying with assigning a textured brush to the text frame. Something like the following -
#include <QPainter>
#include <QTextFrameFormat>
#include <QTextCursor>
#include <QTextFrame>
#include <QTextEdit>
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
QTextDocument doc;
QPixmap map(1024, 1024);
QPainter p;
p.begin(&map);
p.setBackground(QBrush(Qt::transparent));
p.drawRect(QRect(0,0, 4, map.height()));
p.end();
QTextFrameFormat frameFormat;
QBrush bruh(map);
bruh.setColor(Qt::transparent);
frameFormat.setBackground(bruh);
auto cur = new QTextCursor(&doc);
auto frame = cur->insertFrame(frameFormat);
auto curf = new QTextCursor(frame);
curf->insertText("Hello this is qt program!");
QTextEdit e;
e.setDocument(&doc);
e.show();
return a.exec();
}
But this prints a black background even though the background is set to be transparent(I need a transparent background with only left red border).
I can't figure out what is wrong. Also, can there be any other way to have only a left border to a QTextFrame?
Try this:
QTextDocument doc;
QPixmap map(1024, 1024);
map.fill(Qt::white);
QPainter p;
p.begin(&map);
p.fillRect(QRect(0,0, 4, map.height()),QBrush(Qt::red));
p.end();
Dashed line grid can be achieved on 2D chart by doing:
#include <QApplication>
#include <QtCharts>
using namespace QtCharts;
int main(int argc, char *argv[]) {
QApplication a(argc, argv);
auto chart = new QChart;
chart->addSeries(new QLineSeries);
chart->createDefaultAxes();
chart->axes(Qt::Horizontal)[0]->setGridLinePen(Qt::DashLine);
chart->legend()->hide();
QChartView view(chart);
view.show();
view.resize(400,300);
return a.exec();
}
And 3D chart can be achieved by doing:
#include "mainwindow.h"
#include <Q3DBars>
using namespace QtDataVisualization;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
auto chart = new Q3DBars;
setCentralWidget(QWidget::createWindowContainer(chart));
}
MainWindow::~MainWindow() {}
How to set dashed grid lines on 3D chart?
Why its not saving any file?
#include "mainwindow.h"
#include <QApplication>
#include <QPixmap>
#include <QPainter>
#include <QList>
#include <QScreen>
QPixmap grabScreens() {
auto screens = QGuiApplication::screens();
QList<QPixmap> scrs;
int w = 0, h = 0, p = 0;
foreach (auto scr, screens) {
QPixmap pix = scr->grabWindow(0);
w += pix.width();
if (h < pix.height()) h = pix.height();
scrs << pix;
}
QPixmap final(w, h);
QPainter painter(&final);
final.fill(Qt::black);
foreach (auto scr, scrs) {
painter.drawPixmap(QPoint(p, 0), scr);
p += scr.width();
}
return final;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QPixmap pixmap = grabScreens();
QFile file("file.jpg");
file.open(QIODevice::WriteOnly);
pixmap.save(&file, "JPG", 1);
MainWindow w;
w.show();
return a.exec();
}
The file you're looking for should be in the same folder of the executable.
If you're running your code from Qtcreator, it should be in the build directory, as specified in the Build Settings of the Projects page.
You should consider using QStandardPaths to query for a writable location for the screenshot to be saved to. This will avoid the issue of trying to write to a read-only directory.
I want to generate a pdf report containing text and a QChart. I currently use the QTextDocument and add the chart into it as an image, then I use the QPdfWriter to export to pdf.
#include <QtGui>
#include <QtCore>
#include <QApplication>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
using namespace QtCharts;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create a chart
QLineSeries *series = new QLineSeries();
series->append(0,0);
series->append(1,1);
QChart *chart = new QChart();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axisX()->setTitleText(QString("x [m]"));
chart->axisY()->setTitleText(QString("y [m]"));
chart->setTitle("Simple chart example");
chart->resize(500,500);
// Paint the chart into an image
QImage img(500,500,QImage::Format_RGB32);
QPainter painter(&img);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->show();
chartView->render(&painter);
// Set up pdf writer
QString filename = "myfile.pdf";
QPdfWriter writer(filename);
writer.setPageSize(QPagedPaintDevice::A4);
// Create the document
QTextDocument doc;
QTextCursor cursor(&doc);
cursor.insertText(QString("some text\n"));
cursor.insertImage(img);
// Print the document
doc.print(&writer);
return a.exec();
}
Although it works, I would like to avoid converting the chart into an image because it messes up the image quality. Is there a way to do it?
Here is a workaround, if the image quality is really what matters : you can use QPainter directly on the TextDocument. It requires more work and adjustement, but the quality is not lost.
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Create a chart
QLineSeries *series = new QLineSeries();
series->append(0,0);
series->append(1,1);
QChart *chart = new QChart();
chart->addSeries(series);
chart->createDefaultAxes();
chart->axisX()->setTitleText(QString("x [m]"));
chart->axisY()->setTitleText(QString("y [m]"));
chart->setTitle("Simple chart example");
chart->resize(500,500);
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::TextAntialiasing);
chartView->show();
// Set up pdf writer
QString filename = "myfile.pdf";
QPdfWriter writer(filename);
writer.setPageSize(QPagedPaintDevice::A4);
// Create the document
QTextDocument doc;
QPainter painter(&writer);
QFont font = painter.font();
font.setPixelSize(200);
painter.setFont(font);
painter.drawText(QPoint(700,100),"Some text");
chartView->render(&painter);
doc.drawContents(&painter);
return a.exec();
}