PDA

View Full Version : problem in using QImage



rezas1000
4th September 2014, 20:12
Hello,this code will not run Correctly.the setPixel will not work Correctly.please help me.thank you.

#ifndef PAINT_H
#define PAINT_H

#include <QtWidgets>

class Lines : public QWidget
{
Q_OBJECT

public:
Lines(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *event);
void drawLines(QPainter *qp);

};
#endif // PAINT_H


#include "lines.h"
#include <QPainter>


Lines::Lines(QWidget *parent)
: QWidget(parent)
{

}

void Lines::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter qp(this);
drawLines(&qp);
}

void Lines::drawLines(QPainter *qp)
{
QImage image;
QRgb value;

value = qRgb(255, 255, 255); // 0xffbd9527
image.setPixel(1, 1, value);
qp->drawPoint(1,1);
}

anda_skoa
4th September 2014, 20:58
"image" has zero width and height, so 1,1 is not a valid pixel coordinate in "image".

Cheers,
_

rezas1000
4th September 2014, 21:08
I'm sorry.
I did not understand well.thank you.

wysota
5th September 2014, 05:53
And you are not using the image anywhere after manipulating the image.

anda_skoa
5th September 2014, 07:52
I'm sorry.
I did not understand well.thank you.

You constructed a null image: http://qt-project.org/doc/qt-5/qimage.html#QImage
It does not have any pixels.
"Setting" a non-existant pixel does nothing.

Cheers,
_

rezas1000
5th September 2014, 13:57
I'm sorry.
I changed the code.but it did n't work well.


#include "lines.h"
#include <QPainter>


Lines::Lines(QWidget *parent)
: QWidget(parent)
{

}

void Lines::paintEvent(QPaintEvent *e)
{
Q_UNUSED(e);
QPainter qp(this);
drawLines(&qp);
}

void Lines::drawLines(QPainter *qp)
{ QImage image(1,1,QImage::Format_RGB32);
QRgb value;

value = qRgb(255, 255, 255); // 0xffbd9527
image.setPixel(1, 1, value);
qp->drawPoint(1,1);}

wysota
5th September 2014, 14:00
What exactly would you expect to receive after running this code?


void Lines::drawLines(QPainter *qp)
{ QImage image(1,1,QImage::Format_RGB32);
QRgb value;
value = qRgb(255, 255, 255); // 0xffbd9527
image.setPixel(1, 1, value);
qp->drawPoint(1,1);
}

rezas1000
5th September 2014, 14:54
I want to enable of a pixel.But the code don't work well.

wysota
5th September 2014, 15:21
What is the visual effect of "enabling of a pixel"? How do you know your code "didn't work well"?

rezas1000
5th September 2014, 15:50
void Lines::drawLines(QPainter *qp)
{ QImage image(1,1,QImage::Format_RGB32);
QRgb value;
value = qRgb(255, 255, 255); // 0xffbd9527
image.setPixel(1, 1, value);
qp->drawPoint(1,1);
}
when I run the code, the pixel is not White.but in the code,I used the following code
value = qRgb(255,255,255);
Did you tested the code?

wysota
5th September 2014, 15:54
What pixel? You are creating an image object setting some pixel to white and then you don't do anything with the image anymore. Why would any pixel (even if you provided valid range of coordinates in the first place) become white? Your current code will paint a black dot in the top-left corner (offset by 1) of the widget exactly as you tell it to by the drawPoint() line. I don't have to "test" the code to know it.

rezas1000
5th September 2014, 16:23
Thank you very much My friend.Please correct the code.I want to paint color of White in the pixel.but I can not.

wysota
6th September 2014, 08:16
I could correct your code but then you would come back with another trivial problem caused by the fact that instead of learning C++ first before starting with a programing framework that requires decent knowledge of the language, you immediately try to do things you don't really understand. I know you have trouble understanding English but unfortunately this is the skill you must improve to be able to read documentation of libraries you are using. For C++ I can recommend the free "Thinking in C++" book available online. Reading is a must if you want to become a developer.

As for your current issue, you don't want to do anything with an image so why are you trying to use QImage to do it? If you want to paint a white dot then you should focus on changing the colour of the pen to white.