How to print long widgets consecutively - qt

At QT, I made the ui with QWidget. And I want to print the ui with my printer. But, The ui is too long to print on one page.(height is greater than the A4 size) I want continuous prints without newPage() function.
I use QPrinter and QPainter. But, The result I received was a cropped widget image. I don't want to use newPage() function.
QPrinter printer;
QPainter painter;
QPrintDialog printDialog(&printer);
if (printDialog.exec() == QDialog::Accepted) {
painter.begin(&printer);
ui->scrollArea->widget()->render(&painter);
}
I do not like to apply printer.newPage() manually to every ui.
Is there any option or code that prints automatically on the next page like MS Word?

Related

How to use QStyle::standardIcon/standardPixmap with QStyle::StandardPixmap?

In my code there is a bunch of calls which try to create QIcons from
QStyle standard pixmaps, like:
QIcon groupIcon;
groupIcon.addPixmap( style()->standardPixmap( QStyle::SP_DirClosedIcon ),
QIcon::Normal, QIcon::Off );
groupIcon.addPixmap( style()->standardPixmap( QStyle::SP_DirOpenIcon ),
QIcon::Normal, QIcon::On );
While this works correctly, in that using the icon for a model's
Qt::DecorationRole shows either an open or closed icon based on the item's
expanded state, it has two issues:
It's not hi-dpi friendly, and the icons are tiny
QStyle::standardPixmap is marked as obsolete, with QStyle::standardIcon being described as the preferred approach.
I'm unsure how to translate the above code to QStyle::standardIcon though.
QIcon groupIcon( style()->standardIcon( QStyle::SP_DirClosedIcon ) );
works nicely for closed items, and looks great on hidpi. But I can't see how I would add the SP_DirOpenIcon state. There's no equivalent method like "QIcon::addIcon" like there is QIcon::addPixmap.
What's the correct approach to take here, which is hi-dpi friendly and future proof?
how to translate the above code to QStyle::standardIcon
To be able to use QStyle::standardIcon instead of QStyle::standardPixmap, select the particular pixmap from the icon with QIcon::pixmap.
Here is an example I have prepared for you of how to change your code in order to accomplish that:
QIcon groupIcon;
QSize sz(16, 16);
groupIcon.addPixmap(style()->standardIcon(QStyle::SP_DirClosedIcon).pixmap(sz),
QIcon::Normal, QIcon::Off);
groupIcon.addPixmap(style()->standardIcon(QStyle::SP_DirOpenIcon).pixmap(sz),
QIcon::Normal, QIcon::On);
Here 16 is the requested size. Please note, that:
The pixmap might be smaller than requested, but never larger.
hence adjust this value accordingly.

Printing really huge image on plotter using QPrinter

I have to print on plotter world map with a lot of vector data on it from Qt based app. Plotter has 80 GB hard drive, to print high resolution images. All data is loaded in QGraphicsScene.
QGraphicsScene *scene;
.....
QPrinter *printer = new QPrinter(QPrinter::ScreenResolution);
printer->setPaperSize(QPrinter::A4);
QPainter painter(printer);
painter.setRenderHint(QPainter::Antialiasing);
A4 is using for improve speed during tests.
forever
{
if(printedHeight >= sceneHeight)
{
isPrintingRunning = false;
qDebug() << "Printing finished";
break;
}
...loading next tile strip
scene->render(&painter, toPage(sceneRect), tileStripRect);
scene->clearStrip;
printedHeight += stripHeight;
}
After loading vector data, i'm loading tiles(256x256 px) by horizontal strips, render strip to painter, clear tiles from scene, than again. Because i'm rendering by strips scene doesn't exceed available memory, but QPainter doesn't send data to printer until it will be deleted, that's why my app very quickly run out of memory. There is an opportunity to call QPainter::end() each cycle step, but in this way each strip is printed on differrent page on ordinary printer, so I suppose plotter will cut each strip, as soon as i can't control plotter's knife. So this is not a solution. How can I force QPainter to send data and clear its cache, but stay in the same print session?

Qt Set image to label reference variable

I have some pictures in the resource file and their file names correspond to their staffIds. this is how I set the picture into my QLabel but nothing is shown.
QString staffId;
staffId=ui->lineEdit_staffID->text();
QPixmap managerPic(":/staff/\'"+staffId+"\'.jpg");
managerInterface.ui->label_mpic->setScaledContents(true);
managerInterface.ui->label_mpic->setPixmap(managerPic);
I'm with #Mike here, most probably the single quotes aren't part of your filenames. You can use the debugger to see what is passed to the QPixmap constructor, or put the name into a separate QString variable and write it to qDebug() to see what it contains.
In general you better use QString::arg() to build strings instead of concatenation; usually it's easier to read and understand:
QPixmap managerPic(QString(":/staff/\'%1\'.jpg").arg(staffId));
QPixmap managerPic(QString(":/staff/%1.jpg").arg(staffId));

Qt: How to use QGraphicsItemAnimation for multiple items

I am trying to control multiple items animation by using QGraphicsItemAnimation. For my understanding of QGraphicsItemAnimation, i shall have a QTimeline and several QGraphicsItems which control by QGraphicsItemAnimation.
At first, i did not use the QGraphicsItemAnimation, but only QTimeline, and connect QTimeline and QGraphicsItem.
Like this:
connect(timeLine, SIGNAL(valueChanged(qreal)), graphicsItem, SLOT(scale(qreal)));
void GraphicsItem::scale(qreal value)
{
setTransform(QTransform()
.translate(mZoomFactor.x(), mZoomFactor.y())
.scale(value + 1, value + 1)
.translate(-mZoomFactor.x(), -mZoomFactor.y()));
}
But using this way make the animation look like lose frame.
So i referred the Qt example, however QGraphicsItemAnimation do not provide such function like setTransformAt() or setOpacityAt(). Is there any Qt example for complex animation?

QPlainTextEdit truncate history linewise

I have a GUI application whose main part is a QPlainTextEdit. It is used to display a log of the application, and as such the associated text grows line by line ad infinitum.
As the application is intended to run very long, I need to limit the memory that will be allocated for this log. Therefore I want to have some maxNumLines or maxNumCharacters parameter that will make sure the history will be truncated when reached, i.e. the head lines will be removed as new lines are appended (a.k.a. log rotation).
To achieve this I found the functions
// get the associated text
QString toPlainText () const
// set the associated text
void setPlainText ( const QString & text )
Therefore something like this untested code would probably do the trick:
QString &tmp = pte.toPlainText();
while (tmp.size() > maxNumCharacters) {
// remove lines from the head of the string until the desired size is reached
// removes nothing if "\n" could not be found
tmp.remove(0, tmp.indexOf("\n")+1);
}
pte.setPlainText( tmp );
Is this the way to go to remove the first line(s) from the QPlainTextEdit? Are there probably other Qt Text GUI elements that would better fit to this task (set a maximum number of lines and truncate at the head of the list), e.g. somehow display a QStringList in which I could store the lines (s.t. I could easily erase(0))?
Or does the QPlainTextEdit eventually implement such upper bound for the size of the associated QString after all?
Apparantly the property maximumBlockCount is exactly what I need:
If you want to limit the total number of paragraphs in a QPlainTextEdit, as it is for example useful in a log viewer, then you can use the maximumBlockCount property. The combination of setMaximumBlockCount() and appendPlainText() turns QPlainTextEdit into an efficient viewer for log text.
For reference:
http://doc.qt.io/qt-5/qplaintextedit.html#maximumBlockCount-prop
I had exactly the same problem a months back, and I ended up using a QListView. Although using the model/view/delegate architecture is a bit more fiddly, it scales much better in the long run. For example once the basic architecture is in place, adding a filter that displays only error or warning entries becomes trivial, or creating a delegate so that the background of error entries are painted red is also straightforward.

Resources