PDA

View Full Version : How to create my own QPixmap Or QImage



sagirahmed
27th October 2010, 05:25
Hi,

I want create my own QPixmap or QImage using my own data. so that I able to set this QPixmap to QLabel instead of drawing the same content again and again in paintEvent()

Thanks & Regards

nish
27th October 2010, 06:51
QPixmap pix;
QPainter p(&pix);
p.drawXYZ();

lablel->setPixmap(pix);

aamer4yu
27th October 2010, 08:11
QPixmap pix;
QPainter p(&pix);
p.drawXYZ();

lablel->setPixmap(pix);

One mistake.. always specify a size for the pixmap / image while creating it.
Something like - QPixmap pix(600,480);
You cannot paint on null pixmap !!

sagirahmed
27th October 2010, 09:29
Hi,

I have tried this code but it is not working.

Added after 55 minutes:

Hi,

Again i tried but it doesn't work. Can you give me small code so that I can get help from it.

Lykurg
27th October 2010, 10:13
What have you tried? Show us your code.

sagirahmed
27th October 2010, 10:28
void DrawImage()
{
QPixmap pix(700,700);
QPainter painter(&pix);
painter.drawText(QPoint(0 ,0),"text");
painter.save();
labelPixmap->setPixmap(pix);
}

This is my code that I have written. It show some garbage color on Label. Not showing the text

Lykurg
27th October 2010, 10:59
First we have [CODE]-tags. Second
painter.save();is nonsense here! Third read the documentation about QPainter::drawText() especially what the point is defining. And forth: You might want to fill the pixmap with a color first because it is initialized with no specific value.

nish
27th October 2010, 12:34
One mistake..
That was not a mistake. That was left as an exercise for the reader.


always specify a size for the pixmap / image while creating it.
Something like - QPixmap pix(600,480);
You cannot paint on null pixmap !!
You PASSED!!

sagirahmed
27th October 2010, 12:40
Hi,

Thanks I am successfully drawn the image using QPainter and set this image to Qlabel.

Thanks & Regards

sagirahmed
28th October 2010, 08:37
Hi,

I want to create a large QPixmap. The size of QPixmap pix(741,2000000). It cause crashing when we execute the code is

QPixmap pix(741,2000000);
QPainter painter(&pix);
QColor clrbg(144,204,114);
pix.fill(clrbg); /// it crash at this point

So help me for creating very large pixmap

Lykurg
28th October 2010, 09:12
If you are not using the [CODE] tags I won't help you!

sagirahmed
28th October 2010, 09:21
Hi,

I want to create a large QPixmap. The size of QPixmap pix(741,2000000). It cause crashing when we execute the code is

QPixmap pix(741,2000000);
QPainter painter(&pix);
QColor clrbg(144,204,114);
pix.fill(clrbg); /// it crash at this point


So help me for creating very large pixmap

aamer4yu
28th October 2010, 09:39
741 * 2000000 = 1482000000 pixels
Considering 32 bit per pixel
741 * 2000000 * 32 bits are required
ie. 741 * 2000000 * 4 == 5928000000 bytes
ie 5789062 KB
ie 5653 MB
ie 5.5 GB

Do you even have that much RAM ??? Where do you think the memory will be allocated ?
Obviously its gonna crash !!

sagirahmed
28th October 2010, 10:07
Hi,

So How can I solve this problem. If I have to create QPixmap of this size(741*2000000)

nish
28th October 2010, 11:51
Hi,

So How can I solve this problem. If I have to create QPixmap of this size(741*2000000)

create them in short parts. and store then on to the disk. while reading back just do the reverse.

sagirahmed
29th October 2010, 06:00
Hi,

How store short part Qpixmap on to disk. that I have I drawn using QPainter.

aamer4yu
29th October 2010, 06:43
Am not sure, but you can try this -
pixmap is a IO device,, so you can use seek() to write to a particular point.
So use that and try writing smaller rects into the pixmap..

tbscope
29th October 2010, 06:57
Be aware that for some picture formats, you can't just load a certain part of it.
Thus, when you want to save such a large file, you're going to get into trouble at some point. Always save files in managable parts and then create software that can stich those parts together. Look at google maps for example.

sagirahmed
29th October 2010, 08:23
Hi,

so can you suggest me other way to create very large Qpixmap or any other class that will handle this. If I have to create QPixmap of this size(741*2000000)
I know take very huge amount of memory.

tbscope
29th October 2010, 14:27
I'll give you an extremely powerfull tip:
Do NOT paint what is NOT on screen.

Unfortunatly, when you're dealing with jpg files for example, and you want to save a stupendously large file, you WILL get into problems very fast.
Either use another format that allows you to stream the file to disk or from disk or rethink what you're doing (like splitting up the very large jpg into 10 or 100 smaller jpg's)

intelegenter
12th March 2019, 15:23
try to inspect this code.
it use QPainter to draw image in widget from ROI (Region Of Interests) on image.
it works on video card, so, you don't need to resize image on CPU when it scrolled. This code just draw the part of image to widget rect.
imagewidget.h


#ifndef IMAGEMODEL_H
#define IMAGEMODEL_H

#include <QObject>
#include <QWidget>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QResizeEvent>
#include <QPainter>
#include <QDebug>
#include <QAbstractSlider>

class ImageWidget : public QWidget
{
Q_OBJECT
public:
static QImage defaultImage();
explicit ImageWidget(QWidget *parent);
void setImage(QImage* image,bool bResetRoi);
QRectF getRoi();
void setRoi(QRectF newRoi);
QSize getImgSize();

public slots:
void on_setVScrollValue(int value);
void on_setHScrollValue(int value);

signals:
void resetRoi(QRectF roi);
private:
QImage m_img;
QRectF m_roi;
QRectF m_borders;

void paintEvent(QPaintEvent *event);
void resizeEvent(QResizeEvent *event);
};

#endif // IMAGEMODEL_H



imagewidget.cpp


#include "imagewidget.h"

QImage ImageWidget::defaultImage()
{
QImage img(100,100,QImage::Format::Format_Invalid);
QPainter painter(&img);
painter.setPen(QPen(Qt::black));
painter.setBrush(QBrush(Qt::black,Qt::SolidPattern ));
painter.drawRect(QRect(QPoint(0,0),QSize(100,100)) );
return img;
}

ImageWidget::ImageWidget(QWidget* parent)
: QWidget(parent)
{
m_roi = rect() - contentsMargins();
if(m_roi.width() > m_roi.height())
m_borders.setSize(QSizeF(m_roi.width(),m_roi.width ()));
else
m_borders.setSize(QSizeF(m_roi.height(),m_roi.heig ht()));
qDebug()<<m_roi;
}

void ImageWidget::setImage(QImage *image,bool bResetRoi)
{
if(!image) return;
if(!bResetRoi)
{
m_img = *image;
update();
return;
}
m_img = *image;
m_roi = QRect(QPoint(0,0),size()) - contentsMargins();
qDebug()<<m_roi;
m_borders.setSize(m_img.size() * 1.2);
m_borders.moveCenter(m_img.rect().center());
update();
}

QRectF ImageWidget::getRoi()
{
return m_roi;
}

void ImageWidget::setRoi(QRectF newRoi)
{
bool bNormal = true;
if(newRoi.height() < m_borders.height())
{
if(newRoi.top() < m_borders.top())
{
newRoi.moveTop(m_borders.top());
bNormal = false;
}
if(newRoi.bottom() > m_borders.bottom())
{
newRoi.moveBottom(m_borders.bottom());
bNormal = false;
}
}
else
{
newRoi.moveCenter(QPointF(newRoi.center().x(),
m_borders.center().y())
);
}
if(newRoi.width() < m_borders.width())
{
if(newRoi.left() < m_borders.left())
{
newRoi.moveLeft(m_borders.left());
bNormal = false;
}
if(newRoi.right() > m_borders.right())
{
newRoi.moveRight(m_borders.right());
bNormal = false;
}
}
else
{
newRoi.moveCenter(QPointF(m_borders.center().x(),
newRoi.center().y())
);
}
if(!bNormal) emit resetRoi(newRoi);
m_roi = newRoi;
qDebug()<<m_roi;
}

QSize ImageWidget::getImgSize()
{
if(m_img.isNull())
return size();
return m_img.size();
}

void ImageWidget::on_setVScrollValue(int value)
{
m_roi.moveTop(value);
qDebug()<<m_roi;
emit resetRoi(m_roi);
update();
}

void ImageWidget::on_setHScrollValue(int value)
{
m_roi.moveLeft(value);
qDebug()<<m_roi;
emit resetRoi(m_roi);
update();
}

void ImageWidget::paintEvent(QPaintEvent *event)
{
if(m_img.isNull())
{
QWidget::paintEvent(event);
return;
}
if(m_img.format() == QImage::Format_Invalid)
{
QWidget::paintEvent(event);
return;
}
QPainter painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransf orm, true);
painter.setRenderHint(QPainter::HighQualityAntiali asing, true);
QBrush blackBrush;
blackBrush.setColor(QColor(Qt::black));
blackBrush.setStyle(Qt::SolidPattern);
painter.setBackground(QBrush(Qt::black));
painter.setBrush(blackBrush);

painter.drawRect(event->rect());
painter.drawImage(event->rect(),m_img,m_roi.toRect());
}


void ImageWidget::resizeEvent(QResizeEvent *event)
{
QWidget::resizeEvent(event);
}