PDA

View Full Version : suitable class for plotting pixels



babu198649
7th December 2007, 13:30
hi
i have an array of 360*250 which represent pixels.

i have to plot it in a window which class does suit my application best.

i have seen the docs of QGraphicsScene and QGraphicsItem but the docs says that it provides mouse related functions for every item in the graphics view .

also ,there are no items im my application, i dont want any mouse events for my application, so which classes would suit my application better which requires fast plotting .

DeepDiver
7th December 2007, 13:36
Have a look at QPixmap.

Good luck,

Tom

babu198649
7th December 2007, 13:48
thank u
the docs also says that QImage is designed and optimized for I/O, and for direct pixel access and manipulation

so on which widget shall i use QImage.(such as aQPixmap on QLabel.)

Uwe
7th December 2007, 16:22
i have an array of 360*250 which represent pixels.
You didn't write what these pixels are representing, but if you want to display a spectrogram ( http://qwt.sourceforge.net/spectrogramscreenshots.html ) you can have a look at Qwt.

Uwe

wysota
7th December 2007, 16:29
And if you want something really simple (plotting some pixels on a simple canvas) then subclass QWidget and reimplement paintEvent() and sizeHint().

high_flyer
7th December 2007, 16:42
In addition to the previous posters in this thread, you can also use QImage to manipulate pixels directly and then show it with a QPixmap.
All the above solutions are good, depending on your problem domain.

babu198649
10th December 2007, 11:05
thanks to all for u r suggestions

i am trying all of the above methods one by one.

unfortunately i got stuck in the first try .


here is the code


void bpg_graphicsview :: bpg_plot()
{
for (int i=0; i<LOOP; i++)
for (int j=0; j<COL; j++)
{

x[0]= i * cos(j*DEGREE_TO_RADIAN);
y[0]= i * sin(j*DEGREE_TO_RADIAN);

x[1]= (i +1)* cos(j*DEGREE_TO_RADIAN);
y[1]= (i +1)* sin(j*DEGREE_TO_RADIAN);

x[2] =(i +1)* cos((j+1)*DEGREE_TO_RADIAN);
y[2] =(i +1)* sin((j+1)*DEGREE_TO_RADIAN);

x[3] = (i)* cos((j+1)*DEGREE_TO_RADIAN);
y[3] = (i)* sin((j+1)*DEGREE_TO_RADIAN);

points[0] = QPointF(x[0], y[0])+=QPointF(this->width()/2, this->height()/2);
points[1] = QPointF(x[1], y[1])+=QPointF(this->width()/2, this->height()/2);
points[2] = QPointF(x[2], y[2])+=QPointF(this->width()/2, this->height()/2);
points[3] = QPointF(x[3], y[3])+=QPointF(this->width()/2, this->height()/2);
qDebug()<<i<<" "<<j;
this->update();
}
qDebug()<<"end";
}

void bpg_graphicsview :: paintEvent(QPaintEvent * event)
{
qDebug()<<"paint Event";

QPainter painter(this);
painter.setBrush(Qt::red);
painter.drawPolygon(points, 4);

}

this code works only when the contents of bpg_plot() are inside the paintEvent function

otherwise it never executes.:confused:.


also,whenever update() is called the paintEvent does not work(qDebug()<<"paint Event"; does not print for every call).

please point where is the bug in the code .

wysota
10th December 2007, 11:22
If you want to use graphics view, you don't need to reimplement the paint event. Instead you have to add items to the scene. If you wish to use my approach, you can use the above code, just transform it so that it uses integer values.

babu198649
10th December 2007, 11:36
yes wysota , i am following u r method ,i made the chage u said(changing to int values)

inspite ,of the change i am not getting any output .

my problem is simple , i change the points for the painter.drawPolygon(points, 4); function which is inside the paintEvent()function.

but the points are changed in the other function and whenever it is changed the update function is called to update() the diagram.

babu198649
10th December 2007, 11:41
is there any way to draw a polygon without using paintEvent() on QWidget.

Uwe
10th December 2007, 11:44
unfortunately i got stuck in the first try .

Your code tries to paint a polygon, what is something very different than plotting pixels ( for what you got answers ).
Maybe it's better to go one step back and explain more detailed, what your "pixels" are and how do you want to display them.

Do you want to display something like curves in a diagram ?

Uwe

babu198649
10th December 2007, 12:03
ok ume i explain in detail
i have an array of size[360*250] .it represents the degrees of a circle and radius.(for each corresponding degree and radius i have a value in the matrix.)

based on the value in the matrix i have to represent the circle(at every degree and radius) in different color.

so i made a polygon (the area between the angles and a unit radius) and colouring the circle based on the value in the matrix.

thus i need to fill the polygon with different color after computing the polygon coordinates.

high_flyer
10th December 2007, 12:11
have an array of size[360*250] .it represents the degrees of a circle and radius.(for each corresponding degree and radius i have a value in the matrix.)
Do you mean you have a matrix of 250 circles?

based on the value in the matrix i have to represent the circle(at every degree and radius) in different color.
By represent, do you mean draw?
and what do you mean with "at every degree and radius"?
A circle has a radius, and all the degrees in it....:confused:


so i made a polygon (the area between the angles and a unit radius) and colouring the circle based on the value in the matrix.
here I lost you completely...
Why don't you use QPainter::drawEllipse()?
And set the brush with the color you want?

You might want to have a look at QPainter::drawChord() and QPainter::drawPie() too

Uwe
10th December 2007, 12:18
based on the value in the matrix i have to represent the circle(at every degree and radius) in different color.

Ok, then forgot all you read about QImage and QPixmap.

I recommend to follow Witolds suggestion and use a simple QWidget ( QGraphicsView is pointless overhead here, that gives you more trouble than solution ). Then use QPainter::drawArc ( or drawPie ? )

HTH,
Uwe

babu198649
10th December 2007, 12:21
Do you mean you have a matrix of 250 circles?

No

i have only one circle , the circle is divided in to [360]*[250] parts which i called it as matrix.

for every value in matrix representing the circle in different color .

so dividing the circle in to polygons(360*250 polygons) and filling the polygons with different colors.

after finding the points of every polygon i fill the polygon(this is where i got struck) ,before finding the points for the next.

finding the polygon values occur in the loops.

babu198649
10th December 2007, 12:23
do i definitely need to reimplement the paintEvent() function.

high_flyer
10th December 2007, 12:24
Oh ok I see. (sorry for being the slow one)
Then I join wysota and Uwe - QPainter::drawPie().

babu198649
10th December 2007, 12:31
thanks for all of u r cooperation .

even though i am drawing a circle i have an algorithm that has four points and i should definitely need the polygon function.

but is there any way to draw polygon on QWidget without using paintEvent function.

wysota
10th December 2007, 15:11
No, there is none.

You want to obtain something like in the attachment (it draws lines instead of polygons, but the idea is similar)?


#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QApplication>

class Widget : public QWidget {
public:
Widget() : QWidget(){
for(int i=0;i<360;i++){
data[i].c = QColor(qrand()%256, qrand()%256, qrand()%256);
data[i].r = (qrand()%(1000*200))/1000.0;
}
}
QSize sizeHint() const { return QSize(200,200); }

protected:
void paintEvent(QPaintEvent *){
QPainter p(this);
p.fillRect(rect(), Qt::white);
for(int i=0;i<360;i++){
p.save();
p.translate(width()/2, height()/2);
p.rotate(i);
p.setPen(data[i].c);
p.drawLine(QPointF(0,0), QPointF(data[i].r, 0));
p.restore();
}
}
private:
struct S {
QColor c;
float r;
};
S data[360];

};

int main(int argc, char **argv){
QApplication app(argc, argv);
Widget w;
w.show();
return app.exec();
}

babu198649
11th December 2007, 06:41
thank u wysota
i realize the pain u have taken to code the above program( this is the second time u r taking pain for me).
the problem was with save() and restore(). initiallly i have not used these functions.

at first i followed the Basic Drawing Example and i have not noticed the save() and restore() function.
i should have been careful while reading docs to save u the pain

thanks again:)