Results 1 to 20 of 20

Thread: suitable class for plotting pixels

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Question suitable class for plotting pixels

    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 .
    Last edited by babu198649; 7th December 2007 at 13:57.

  2. #2
    Join Date
    Oct 2007
    Location
    Munich, Bavaria
    Posts
    144
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: suitable class for painting

    Have a look at QPixmap.

    Good luck,

    Tom

  3. #3
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Question Re: suitable class for painting

    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.)

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: suitable class for plotting pixels

    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

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: suitable class for plotting pixels

    And if you want something really simple (plotting some pixels on a simple canvas) then subclass QWidget and reimplement paintEvent() and sizeHint().

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: suitable class for plotting pixels

    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.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: suitable class for plotting pixels

    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

    Qt Code:
    1. void bpg_graphicsview :: bpg_plot()
    2. {
    3. for (int i=0; i<LOOP; i++)
    4. for (int j=0; j<COL; j++)
    5. {
    6.  
    7. x[0]= i * cos(j*DEGREE_TO_RADIAN);
    8. y[0]= i * sin(j*DEGREE_TO_RADIAN);
    9.  
    10. x[1]= (i +1)* cos(j*DEGREE_TO_RADIAN);
    11. y[1]= (i +1)* sin(j*DEGREE_TO_RADIAN);
    12.  
    13. x[2] =(i +1)* cos((j+1)*DEGREE_TO_RADIAN);
    14. y[2] =(i +1)* sin((j+1)*DEGREE_TO_RADIAN);
    15.  
    16. x[3] = (i)* cos((j+1)*DEGREE_TO_RADIAN);
    17. y[3] = (i)* sin((j+1)*DEGREE_TO_RADIAN);
    18.  
    19. points[0] = QPointF(x[0], y[0])+=QPointF(this->width()/2, this->height()/2);
    20. points[1] = QPointF(x[1], y[1])+=QPointF(this->width()/2, this->height()/2);
    21. points[2] = QPointF(x[2], y[2])+=QPointF(this->width()/2, this->height()/2);
    22. points[3] = QPointF(x[3], y[3])+=QPointF(this->width()/2, this->height()/2);
    23. qDebug()<<i<<" "<<j;
    24. this->update();
    25. }
    26. qDebug()<<"end";
    27. }
    28.  
    29. void bpg_graphicsview :: paintEvent(QPaintEvent * event)
    30. {
    31. qDebug()<<"paint Event";
    32.  
    33. QPainter painter(this);
    34. painter.setBrush(Qt::red);
    35. painter.drawPolygon(points, 4);
    36.  
    37. }
    To copy to clipboard, switch view to plain text mode 

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

    otherwise it never executes..


    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 .

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: suitable class for plotting pixels

    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.

  9. #9
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: suitable class for plotting pixels

    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.

  10. #10
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: suitable class for plotting pixels

    is there any way to draw a polygon without using paintEvent() on QWidget.

  11. #11
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: suitable class for plotting pixels

    Quote Originally Posted by babu198649 View Post
    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

  12. #12
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: suitable class for plotting pixels

    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: suitable class for plotting pixels

    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....

    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
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #14
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: suitable class for plotting pixels

    Quote Originally Posted by babu198649 View Post
    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

  15. #15
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: suitable class for plotting pixels

    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.

  16. #16
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: suitable class for plotting pixels

    do i definitely need to reimplement the paintEvent() function.

  17. #17
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: suitable class for plotting pixels

    Oh ok I see. (sorry for being the slow one)
    Then I join wysota and Uwe - QPainter::drawPie().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  18. #18
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Re: suitable class for plotting pixels

    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.
    Last edited by babu198649; 10th December 2007 at 12:39.

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: suitable class for plotting pixels

    No, there is none.

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

    Qt Code:
    1. #include <QWidget>
    2. #include <QPainter>
    3. #include <QPaintEvent>
    4. #include <QApplication>
    5.  
    6. class Widget : public QWidget {
    7. public:
    8. Widget() : QWidget(){
    9. for(int i=0;i<360;i++){
    10. data[i].c = QColor(qrand()%256, qrand()%256, qrand()%256);
    11. data[i].r = (qrand()%(1000*200))/1000.0;
    12. }
    13. }
    14. QSize sizeHint() const { return QSize(200,200); }
    15.  
    16. protected:
    17. void paintEvent(QPaintEvent *){
    18. QPainter p(this);
    19. p.fillRect(rect(), Qt::white);
    20. for(int i=0;i<360;i++){
    21. p.save();
    22. p.translate(width()/2, height()/2);
    23. p.rotate(i);
    24. p.setPen(data[i].c);
    25. p.drawLine(QPointF(0,0), QPointF(data[i].r, 0));
    26. p.restore();
    27. }
    28. }
    29. private:
    30. struct S {
    31. QColor c;
    32. float r;
    33. };
    34. S data[360];
    35.  
    36. };
    37.  
    38. int main(int argc, char **argv){
    39. QApplication app(argc, argv);
    40. Widget w;
    41. w.show();
    42. return app.exec();
    43. }
    To copy to clipboard, switch view to plain text mode 
    Attached Images Attached Images

  20. The following user says thank you to wysota for this useful post:

    babu198649 (11th December 2007)

  21. #20
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Smile Re: suitable class for plotting pixels

    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

Similar Threads

  1. Connecting to a base class signal?
    By AaronMK in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 22:37
  2. Creating object of other class in Run() method
    By santosh.kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 15:05
  3. Replies: 2
    Last Post: 16th March 2007, 09:04
  4. Replies: 2
    Last Post: 4th May 2006, 19:17
  5. How to propagate from one class to another
    By mahe2310 in forum Qt Programming
    Replies: 15
    Last Post: 20th March 2006, 01:27

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.