Get the QPixmap from a QLabel - c++

I have a QLabel called Picture which Pixmap has been set to a QImage.
I now want to modify the image in another function.
Is it possible to get the Pixmapfrom the Picture?
I tried the following, which results in an unhandled exception:
QPixmap const *pm = ui.Picture->pixmap();
QImage image(pm->toImage());
Is there any other way?

I think you shall check the return value of the pixmap() function.
const QPixmap* pixmap = ui.Picture->pixmap();
if ( pixmap )
{
QImage image( pixmap->toImage() );
}
Maybe you just called the function when the image was not set and that caused the exception.

Related

How to resize a cv::Mat in order to use it for a Qlabel (My program throws SIGSEV)

I have a problem when resizing a cv::Mat and trying to use it for a pixmap in a Qlabel.
If I resize the mat and dont use it for the Qlabel, the program works fine, and if I do not resize the mat and use it for the pixmap with that size, it also works fine. The problem comes when I try to do both at the same time, for any reason I do not understand.
According to the debug, I recieve the SIGSEV here (from my mainWindow to the deepest error):
void MainWindow::print_img(const QImage &img)
{
ui->map->setPixmap(QPixmap::fromImage(img.rgbSwapped()));
}
---------
QImage rgbSwapped() const &
{ return rgbSwapped_helper(); }
---------
res = QImage(d->width, d->height, d->format);
---------
QImage::QImage(int width, int height, Format format)
: QImage(QSize(width, height), format)
---------
d = QImageData::create(size, format);
---------
d->data = (uchar *)malloc(d->nbytes);
Thanks in advance for the help.
I solved it by moving the map (where i got my image from) from the class variables to the run() function, so I do not have to resize the map.

Qt + OpenCV: How to display cv::Mat more effectively?

I want to display 3 channel cv::Mat on Qt interface and usually I use this expressions:
e
QImage qImage = QImage( (uchar*)cvImage.data, cvImage.cols, cvImage.rows, cvImage.cols*3, QImage::Format_RGB888 );
QPixmap pixmap = QPixmap::fromImage(qImage);
myLabel.setPixmap(pixmap);
But conversion to QPixmap is slow enough. Do you know guys how to avoid the conversion? May be the function setImage can help, but I don't know how to use it...

Drawing line to QImage

I am trying to draw line to QImage and show it in Qlabel. However I have some issues that I cannot solve.
QPixmap px;
px.fromImage (imgRaw); // define in header file QImage imgRaw;
QPainter p (&px);
p.setPen (Qt::red);
p.drawLine (mouseStart_X, mouseStart_Y, mouseReleased_X, mouseReleased_Y);
p.end ();
ui->lblRightImg->setPixmap (px);
ui->lblRightImg->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
ui->lblRightImg->setScaledContents(true);
When I used this code above it gives such error :
QPainter::begin: Paint device returned engine == 0, type: 2
QPainter::setPen: Painter not active
QPainter::end: Painter not active, aborted
Then I change my code because it tries to draw in null pixmap so after changing code like this:
QPixmap px(100, 100);
px.fromImage (imgRaw); // define in header file QImage imgRaw;
Then it gives noisy image(black and gray broken image)
Could you please help me to solve this issue ?
EDIT :
Also tried:
QPixmap px = QPixmap::fromImage (imgRaw);
Then it gives same image without any drawing..
fromImage is a static function of QPixmap and does not affect your 'object', it returns the pixmap you want. Try using following code to initialize your pixmap:
QPixmap px = QPixmap::fromImage(imgRaw);

qpixmap.scaled function not working

I'm new in qt and base on my research i can change the size of a pixmap/image using the scaled function. but when i run the program the image size is still the same. here is the code.
QStringList headers;
QString headerValues="header1,header2";
headers=headerValues.split(",");
ui->tableWidget_2->setColumnCount(2);
ui->tableWidget_2->setHorizontalHeaderLabels(headers);
QStringList verticalHeaders;
QString verticalHeaderValues="vertical1,vertical2";
verticalHeaders=verticalHeaderValues.split(",");
ui->tableWidget_2->setRowCount(1);
ui->tableWidget_2->setVerticalHeaderLabels(verticalHeaders );
ui->tableWidget_2->horizontalHeader()->setDefaultSectionSize(150);
ui->tableWidget_2->verticalHeader()->setDefaultSectionSize(150);
ui->tableWidget_2->horizontalHeader()->setResizeMode(QHeaderView::Fixed);
ui->tableWidget_2->verticalHeader()->setResizeMode(QHeaderView::Fixed);
QString filename="/home/marcvincento/Desktop/Private/Projects/fixButtons/Printer.png";
QPixmap pic(filename);
pic.scaled ( 20, 20, Qt::IgnoreAspectRatio, Qt::FastTransformation );
QBrush brush(pic);
QTableWidgetItem* item=new QTableWidgetItem();
item->setBackground(brush);
item->setText("ADD 5,1");
ui->tableWidget_2->setItem(0,0,item);
If you read the Qt documentation of QPixmap, you will see that the scaled() functions are declared const and return a QPixmap. That means the pixmap itself is not modified. What happens is that these functions return a scaled version of the pixmap. You will have to store that somewhere. In your case, you can just pass the scaled pixmap to the QBrush constructor:
QPixmap pic(filename);
QBrush brush(pic.scaled(20, 20, Qt::IgnoreAspectRatio,
Qt::FastTransformation));
If you would need to use the same scaled pixmap again later on and have no use for the original, unscaled pixmap, then you should construct the pic pixmap directly from a scaled version of the source, so that you won't end up performing the same scaling operation multiple times:
QPixmap pic(QPixmap(filename).scaled(20, 20, Qt::IgnoreAspectRatio,
Qt::FastTransformation));
If you do need both the scaled and unscaled versions, then obviously you'd need two pixmaps; one of the original and one for the scaled version:
QPixmap pic(filename);
QPixmap picScaled(pic.scaled(20, 20, Qt::IgnoreAspectRatio,
Qt::FastTransformation));

How to create image file from QGraphicsScene/QGraphicsView?

Given a QGraphicsScene, or QGraphicsView, is it possible to create an image file (preferably PNG or JPG)? If yes, how?
After just dealing with this problem, there's enough improvement here to warrant a new answer:
scene->clearSelection(); // Selections would also render to the file
scene->setSceneRect(scene->itemsBoundingRect()); // Re-shrink the scene to it's bounding contents
QImage image(scene->sceneRect().size().toSize(), QImage::Format_ARGB32); // Create the image with the exact size of the shrunk scene
image.fill(Qt::transparent); // Start all pixels transparent
QPainter painter(&image);
scene->render(&painter);
image.save("file_name.png");
I have not tried this, but this is the idea of how to do it.
You can do this in several ways
One form is as follows:
QGraphicsView* view = new QGraphicsView(scene,this);
QString fileName = "file_name.png";
QPixmap pixMap = view->grab(view->sceneRect().toRect());
pixMap.save(fileName);
//Uses QWidget::grab function to create a pixmap and paints the QGraphicsView inside it.
The other is to use the render function QGraphicsScene::render():
QImage image(fn);
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
image.save("file_name.png")
grabWidget is deprecated, use grab. And you can use a QFileDialog
QString fileName= QFileDialog::getSaveFileName(this, "Save image", QCoreApplication::applicationDirPath(), "BMP Files (*.bmp);;JPEG (*.JPEG);;PNG (*.png)" );
if (!fileName.isNull())
{
QPixmap pixMap = this->ui->graphicsView->grab();
pixMap.save(fileName);
}