PDA

View Full Version : How to disable the text antialias option when printing QGraphicsScene to a printer?



momo
13th August 2015, 08:57
Hello everyone,

I want to print some texts without antialias option to a printer, so I added some code like this:

QFont font = painter->font();
font.setStyleStrategy(QFont::NoAntialias);
painter->setFont(font);
painter->drawText( sRect, str, _pd->m_textOp );
The texts perfomed well in the viewport, but in the print preview and printed pages that texts was still antialiased.
11317
Left part of the picture shows that how a text displayed in viewport, and right part shows that text displayed in the print preview window.
The small antialiased text performed terrible in the black and white printer so I have to disable that option, is there anyone who can help me?

anda_skoa
13th August 2015, 10:09
Hmm, I guess you already checked, but jsut in case: is antialising "on" on the painter?

Cheers,
_

momo
13th August 2015, 13:40
Hmm, I guess you already checked, but jsut in case: is antialising "on" on the painter?

Cheers,
_

Thank you for your reply.
I've tried to use RenderHint to turn off the option, like this:

QFont font = painter->font();
font.setStyleStrategy(QFont::NoAntialias);
painter->setFont(font);
painter->setRenderHint(QPainter::Antialiasing, false);
painter->setRenderHint(QPainter::TextAntialiasing, false);

but it still not working, the text in the viewport is showed correct, but when I open print preview window, the text in the window is antialiasing again. :(

anda_skoa
13th August 2015, 14:08
Does it only affect the preview or the print itself?
E.g. try printing into a PDF.

Cheers,
_

momo
14th August 2015, 02:32
Does it only affect the preview or the print itself?
E.g. try printing into a PDF.

Cheers,
_

I've been tring to print to PDF, it is not working. But I rendered the scene to image, it had not any problem.
So I guess there have some special processes in the QPrintPreviewWidget.
Or it does the matter of the point size of the text?(I used point size to define the size of text)

anda_skoa
14th August 2015, 08:18
I've been tring to print to PDF, it is not working.

Which approach did you try?
Setting the output file name on QPrinter or using QPdfWriter?



But I rendered the scene to image, it had not any problem.

Ok, that is already good.



So I guess there have some special processes in the QPrintPreviewWidget.

It could depend on the paint device that the QPainter is operating on.



Or it does the matter of the point size of the text?(I used point size to define the size of text)
Could be, but I think you would then also observe the problem when rendering into an image.

Cheers,
_

momo
19th August 2015, 17:17
I found several clues there days, maybe it is not the problem of antialias,
I tried to draw an image and an text and then printed them to a pdf file,
Here is the part of the test code:

void MyWidget::slotPaintRequested(QPrinter *printer)

{
QPainter painter( printer );

QImage image1("E:/aaa.png");
painter.drawImage(QPoint(0, 10), image1);

QFont font;
font.setPointSize(10);
painter.setFont(font);
painter.drawText(QPoint(20, 30), "Hello World");

QImage image2("E:/aaa.png");
painter.drawImage(QPoint(100, 10), image2);
}
here is the picture:
1132811327
The first pic is the one showed in preview dialog, the second is the one in the pdf file(both zoom out to 800%).
We can see the image in pdf file is blured and the text near to the image was affected too.
In some complex situations(some images in the scene), these images would affected the most area.
I could not find the solution yet, is there any method to avoid there problem?

anda_skoa
19th August 2015, 17:41
Can you check if there is any difference if you use QPdfWriter?

Cheers,
_

momo
20th August 2015, 01:48
I used the print function of QPrintPreviewDialog and set the selected printer to "Foxit Reader PDF Printer".
I found that when I setted the quality of export pdf to a higher level, the problem is relieved but not avoided, and I printed them to a real printer, is had the same problem.

momo
20th August 2015, 07:40
I tried another test, and found some clues.
11329
This image is an rect filled with red.
11330
And this one is filled with transparent(not white).
First time,I painted the first image, a line, and some text, then I printed them, the picture is like this:
11331
Second time, I painted the first image, the second image, a line, and some text(which means there had an invisible image between the frist image and the other shapes), then I printed them, the picture is like this:
11332
We can see the line and the text in the area of transparent image had become Blurry.

anda_skoa
20th August 2015, 09:28
I used the print function of QPrintPreviewDialog and set the selected printer to "Foxit Reader PDF Printer".
I found that when I setted the quality of export pdf to a higher level, the problem is relieved but not avoided, and I printed them to a real printer, is had the same problem.

Repeating myself: what if you directly generate PDF using QPdfWriter?

Cheers,
_

momo
20th August 2015, 09:48
Our project is developed by Qt 4.8.3 so I could not use QPdfWriter.:crying: