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);
Related
I've tried this:
QBrush brush(QPixmap(":/new/prefix1/car.jpg"));
painter.setBrush(brush);
QRectF car(positions[i],120, 20, 10 );
painter.drawRect(car);
It shows the image but it repeats itself during a simulation in QPaint.
I want a way to fill a rectangle with an image but i'm not finding any specific methods for Rect. Any tricks for that?
Use QPainters drawPixmap. There is an overloaded function, that takes both a QPixmap and a QRect into which the pixmap will be painted:
http://doc.qt.io/qt-5/qpainter.html#drawPixmap-9
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.
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));
I'm trying to paint some points of my image and I don't know why it doesn't work. I have defined a QImage and I want to modify some points.
QImage *cou= new QImage(height,largeur,QImage::Format_Mono);
cou->fill(1);
QPainter *fig=new QPainter (cou);
for (i=0;i<size_;i++)
{
fig-> drawPoint(floor(propa[i]),nbmax[i]);
}
When I execute the code I obtain
QPainter::begin: Paint device returned engine == 0, type: 3
and in the following lines:
QPainter::drawPoints: Painter not active
QPainter::begin: Paint device returned engine == 0, type: 3
The error means that the image you're trying to paint is a null image. Use isNull on couto check this.
The cause of image being null may be the wrong height and largeur params when the image is constructed, or you're out of memory
QPaintEngine* eng = cou->painterEngine();
if(eng) {
// create QPainter ...
}
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);
}