Printing a grid and 2 tables in a QPdfWriter
-
Hi!
I would like to print a grid and two tables, like the image attached, to a PDF file. As you can see, some lines in the tables are wider than the width of the page, and the second table is not completely written.
I want to use as much space possible for the grid, and the tables should to be all visible in a font size big enough to be readable.
This first implementation does not calculated the positions, spaces and sizes generically because I do not know
QPainter
very well, and I got confused between things that are measured in pixel, like spaces and positions, and others that are not, like font size.I am setting a
QPdfWriter
like this:QPdfWriter _pdf(_file_name); _pdf.setPageSize(QPageSize::A4); _pdf.setPageMargins(QMargins(10, 10, 10, 10), QPageLayout::Millimeter);
To draw the grid I am doing something like this:
QPainter _painter(&p_pdf); const int _x0{0}; const int _y0{0}; const int _width{275}; const int _height{275}; const int _cols{ui->tblGrid->columnCount()}; const int _rows{ui->tblGrid->rowCount()}; const QColor _cell_background{250, 250, 250}; QPen line_pen(Qt::black); line_pen.setWidth(5); _painter.setPen(line_pen); for (int _row = 0; _row < _rows; ++_row) { int _yi = _y0 + (_row * _height); int _yf = _yi + _height; for (int _col = 0; _col < _cols; ++_col) { int _xi = _x0 + (_col * _width); int _xf = _xi + _width; Content *_content = reinterpret_cast<Content *>(ui->tblGrid->cellWidget(_row, _col)); if (!_content->is_unused()) { _painter.drawLine(_xi, _yi, _xf, _yi); _painter.drawLine(_xf, _yi, _xf, _yf); _painter.drawLine(_xf, _yf, _xi, _yf); _painter.drawLine(_xi, _yf, _xi, _yi); _painter.fillRect(QRect{QPoint{_xi, _yi}, QPoint{_xf, _yf}}, _cell_background); } // part of the code that draws the little numbers } } }
In this code above I would like to center the grid, and to calculate the best cell size instead of using the constant
275
.For the tables I am using this:
using lines = std::map<uint16_t, std::pair<word, explanation>>; . . . const uint8_t _font_size{6}; const uint8_t _space_between_lines{160}; auto _print = [&](const lines &p_lines, int p_y) { QFont _font_words; _font_words.setPointSize(_font_size); _painter.setFont(_font_words); uint16_t _count{0}; for (const lines::value_type &_value : p_lines) { const QString _text{QString::number(_value.first) + " - " + QString{_value.second.second.to_string().c_str()} + " (" + QString::number(_value.second.first.size()) + " letras)"}; int _y{p_y + _count * (_font_size + _space_between_lines)}; _painter.drawText(0, _y, _text); ++_count; } }; const int _y_horizontals_title{static_cast<int>(_rows * _height) + 500}; { QFont _font_words; _font_words.setPointSize(12); _painter.setFont(_font_words); _painter.drawText(0, _y_horizontals_title, "Horizontais"); } _print(_horizontals, _y_horizontals_title + 200); . . . // here is the code that prints the other table
As you can see, a lot of "magic" numbers that I would like to get rid off.
I appreciate very much any help!!
P.S. The text of the tables are in Portuguese