[Qt-interest] Pen width with QPen::setCosmetic
Sherif Ghali
sherif.ghali at yahoo.com
Sun Sep 12 18:59:55 CEST 2010
The program below generates 4 pdf files. In the first, setCosmetic_f_1.pdf, we
call QPen::setCosmetic(false), and the x-scale and y-scale are equal. The two
lines appear with the same width. In the second, setCosmetic_f_3.pdf, the
y-scale is 3 times the x-scale, but because setCosmetic is still false, the
horizontal line is three times wider than the vertical line, as expected.
The file setCosmetic_t_1.pdf is generated with setCosmetic(true). The scale is
uniform and the two line widths are equal. All is still well.
This brings me to my question. Because setCosmetic(true) is used to
produce setCosmetic_t_3.pdf, the horizontal line ought to have been produced
with the same thickness as the vertical one. It isn't. What am I missing?
This example uses pdf output for convenience, but the same behavior is exhibted
by two other QPaintDevices: QWidget and QSvgGenerator.
(It is of course possible to enforce a 1:1 scale before painting, but it's also
very convenient to use different scales and get uniform pen width.)
Sherif
#include <QApplication>
#include <QPrinter>
#include <QPainter>
#include <QPainterPath>
void generate_pdf(const bool is_cosmetic, const qreal yscale)
{
QPrinter device;
device.setOutputFormat(QPrinter::PdfFormat);
device.setOutputFileName(QString("setCosmetic_%1_%2.pdf").arg(is_cosmetic?"t":"f").arg(yscale));
device.setOrientation(QPrinter::Landscape);
QPainter painter(&device);
QTransform scale;
scale.scale(1.0, yscale);
painter.setTransform(scale);
QPainterPath path;
const qreal w = device.width();
const qreal h = device.height();
path.moveTo( QPointF( 0.0, h / 2.0 / yscale) );
path.lineTo( QPointF( w, h / 2.0 / yscale) );
path.moveTo( QPointF(w / 2.0, 0.0) );
path.lineTo( QPointF(w / 2.0, h / yscale) );
QPen pen(Qt::blue);
pen.setWidth(50.0);
pen.setCosmetic(is_cosmetic);
painter.strokePath( path, pen );
painter.end();
}
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
generate_pdf(false, 1.0);
generate_pdf(false, 3.0);
generate_pdf(true, 1.0);
generate_pdf(true, 3.0);
}
More information about the Qt-interest-old
mailing list