QTableWidget print table on paper - qt

I am writing program using Qt. Program result - QTableWidget object. Can I print this table on paper without any conversion to excel, pdf, etc? Is this possible?

QPrinter is a paint device, you can render your widget with that.
QPrinter printer;
QPrintDialog printer_dialog(&printer);
if (printer_dialog.exec() == QDialog::Accepted) {
QPainter painter(&printer);
your_widget->render(&painter);
}

Take of look at QDocument. Use e.g. a QTextstream to construct a html-structure from your table. Use setHtml to set the document content. Now you can use print to print with a QPrinter.

Related

Display image using QImage without using pixmap in Qt?

I have a requirement to read pixel values from the picture displayed on the GraphicScene layout. How can I display image using QImage without using pixmap in Qt so that I am able to read the pixel values?
On most platforms, a QPixmap is a thin wrapper around a QImage. The conversions between the two are cheap - especially the pixmap-to-image conversion. Thus, you can use the QGraphicsPixmapItem and use item->pixmap().toImage() without much worry. To confirm that QPixmap is indeed a wrapper, the following check will do:
bool isPixmapThin(const QPixmap &pix) {
auto const a = pix.toImage();
auto const b = pix.toImage();
return a.bits() == b.bits();
}
In all cases, ensure that the image you take from the pixmap won't detach, i.e. always make it const (as in the code example above).

Qt: How to clean up a QImage from memory

How to clear or clean up a QIMage
Following method of mine get a const reference to a QIMage.
MyMethod(const QImage & img) {
// save it to a file
img.save("/path/to/save/the/qimage");
// now I want to clan up img from memory. How should I do it?
}
Question:
How should I clean up the QImage object from memory after use?
Note:
Note that it is a const & QImage. So, answer would involve casting the QImage into non-const?
Also, I am looking at trying to get a QImageData pointer to the data & delete it. Not sure if that is the correct approach here. Do suggest.
You need a non-const reference or a pointer. With a pointer the answer is obvious. With a reference you just assign a default-constructed QImage to it.
MyMethod(QImage & img) {
img.save("/path/to/save/the/qimage");
img = QImage();
}
However, this may still not clean up the memory occupied by the image, if there are additional QImage instances referencing that same image. To overcome this hurdle you need to avoid multiple QImage instances referencing the same image. A Qimage instance is like a shared pointer in this regard.
A const-cast would be considered to reveal a design flaw in your case. I'd recommend against it.

How to work with QGraphicsScene::addPixmap when it only accepts const QPixmap?

I'd like to display some QImage through QGraphicsScene, my code's very straightforward:
mainwindow.h
QImage *sourceImage;
QGraphicsView *imageView;
QGraphicsScene *imageScene;
mainwindow.cpp
imageScene = new QGraphicsScene;
imageView = new QGraphicsView;
imageView->setScene(imageScene);
sourceImage = new QImage;
sourceImage.load(":/targetimage.png");
imageScene.addPixmap(QPixmap::fromImage(sourceImage));
And then the complier points out exactly what I did wrong: QGraphicsScene::addPixmap accepts only const QPixmap as argument, and I was trying to convert QImage to const QPixmap, which is not allowed because QPixmap::fromImage within only accept const QImage, like a const hell.
The official documentation on this method doesn't make much sense to me either, if I'd like to make for example, an image viewer, and during runtime I'd sure load different images into QImage sourceImage, and how can I accomplish that using a const QImage?
This problem has been agonizing, thanks for any advice. Moreover could you light me a bit if there's any vision on the philosophical reason why guys in Qt make these methods const?
Try
imageScene.addPixmap(QPixmap::fromImage(*sourceImage));
Some advice:
there is no need to allocate the QImage on the heap (using new).
Use:
QImage sourceImage;
Then you do not need to dereference the pointer when calling QPixmap::fromImage
Just to clarify: the constness has nothing to do with the error.

How to associate a MFC DC with QPrinter?

I was just wondering if it is possible to use my existing MFC created DC to associate with a QPrinter so that it directly uses the existing DC rather creates its own with QPrintDialog?
Does QPrinter make use of Qt's backingstore as QWidget does, i.e., does it create an off-screen image before printing?
First thing:
QPrintDialog has nothing to do with your question.
You can set-up a QPrinter without using QPrintDialog.
Anyway: You paint into a QPainter where QPrinter is "only" the printing device. Printing only starts when you end the QPainter. So there is a backing store when using QPrinter.
You can't convert a DC to a QPainter so QPrinter can't do anything with a DC. The closest thing would be to get a Bitmap from the DC and print it to a fresh QPainter.

Is there a simple way to restore widget sizes in Qt?

I'm looking for a way to preserve the size of windows in a Qt app.
I've seen that there is the possibility of using the following method for every widget:
saveGeometry()
But really, I don't find this a satisfactioning method. Is there something like setAutosaveGeometry(True)?
I'm especially looking for a way to store the widths of table columns.
The QHeaderView class also has two methods for saving and restoring it's state to and from a QByteArray: saveState()and restoreState()
A table view's headers are accessible via the horizontalHeader() and verticalHeader() methods.
saveGeometry returns a QByteArray value, you need to store it somewhere.
Example:
void MainWindow::closeEvent(QCloseEvent *event){
QSettings settings;
settings.setValue("geometry", saveGeometry());
}
For reading the geometry call the restoreGeometry function:
MainWindow::MainWindow(QWidget *parent):QMainWindow(parent) {
[...]
QSettings settings;
restoreGeometry(settings.value("geometry").toByteArray());
[...]
}
To learn more about window geometry please read the documentation
See the Qt documentation on Restoring a Window's Geometry.

Resources