Question

I have a custom QGraphicsItem that display large (100s of MB compressed, mutli-channel) images. It's working pretty well right now.

The idea is only load scanlines that are needed for current view scale and graphics view port. And divide the display into tiles that are built in separate threads. The paint() function only constructs the tiles and feed them to QThreadPool. It also checks for and draw any tiles that are done. There are more code handling the events between the threads/runnables and the item. But this is the basic part.

The goal is to provide a responsive UI. When the item is updating itself, UI is not blocked. It all work well until I tried to figure how to print it.

The problem is I need to know a particular call to my paint function is resulted from a print instead of on screen update. This is needed to block the main thread until all my tiles are done and plotted to the painter (or the printed page will be blank since paint() returns right away)

It also helps to adjust the sample rate since the printer has higher resolution than screen.

So my question comes down to this: How can I find out if the QPainter passed to my paint() is a screen or a printer. Better yet, can I tell if the invocation is for print preview or actual print?

Thanks.

Was it helpful?

Solution

Just an idea, don't have a printer right now to test it.

There is a device() method in QPainter class which returns the paint device on which this painter is currently painting, or 0 if the painter is not active. The paint device could be implemented by the QWidget, QImage, QPixmap, QGLPixelBuffer, QPicture, and QPrinter subclasses. So I believe if you will check if your device is of QPrinter type, this would mean you're printing right now.

Smth like this:

QPaintDevice* device = painter->device();
if (dynamic_cast<QPrinter*>(device)!=NULL)
    qDebug() << "QPrinter";
else if (dynamic_cast<QWidget*>(device)!=NULL)
    qDebug() << "QWidget";
else if (dynamic_cast<QImage*>(device)!=NULL)
    qDebug() << "QImage";
else if (dynamic_cast<QPixmap*>(device)!=NULL)
    qDebug() << "QPixmap";
else if (dynamic_cast<QPicture*>(device)!=NULL)
    qDebug() << "QPicture";
else
    qDebug() << "something else";

hope this helps, regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top