Results 1 to 12 of 12

Thread: QGraphicsView and QGraphicsScene

  1. #1
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QGraphicsView and QGraphicsScene

    hello,
    I wrote an anpplication where i use QGraphicsView widget. This widget is synchornized with a scene object (QGraphicsScene class). Because i want to show graphs on scene it is easy to put raster lines like scene.addLine(..).But how to easily draw pixels? I want to draw them similar way to WinApi, like:

    SetPixel(memdc, j,((tab[j][i]-ymin)/skala)*(-1)+chartH, RGB(120,70,60));

    Is there any simple way? Of course the best way is sth like a buffer which i can show at the end of drawing. Am i supposed to use QImage and then show it?
    Or maybe there is possibilty to draw it on scene. I tried with QPolygon but it is only possible to show QPolygon object in one place, right? Please, give me some ideas.

    By the way: i dont want to use any Qwt library.
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

  2. #2
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView and QGraphicsScene

    No ideas?

    I just wanna set pixels on QGraphicsScene scene. My idea is to create QImage, set pixexs on it, and finally show. Is there any better ability?
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

  3. #3
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QGraphicsView and QGraphicsScene

    Thats probably how you should do it. QGraphicsScene is about managing graphical objects, not pixels.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

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

    Default Re: QGraphicsView and QGraphicsScene

    Create a graphics view item class for representing points or use QGraphicsEllipseItem with a small radius.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView and QGraphicsScene

    Drawing point as an elipse with minimal radius is not very elegant.
    I did sth like that:
    (my QGraphicsView has 370 x 270 dimensions)

    Qt Code:
    1. Widget::Widget(QWidget *parent) :
    2. QWidget(parent),
    3. ui(new Ui::Widget)
    4. {
    5. ui->setupUi(this);
    6.  
    7. scene=new QGraphicsScene();
    8. scene->setSceneRect(0,0,350,250);
    9.  
    10. timer=new QTimer();
    11.  
    12. ui->graphicsView->setScene(scene);
    13.  
    14.  
    15. ui->graphicsView->adjustSize();
    16.  
    17. image=QPixmap(350,250);
    18. image.fromImage(nice);
    19.  
    20. image.fill(Qt::white);
    21. scene->addPixmap(image); scene->addPixmap(image);
    22. text1=scene->addText("hello",QFont());//text1->setPos(100,110);
    23.  
    24.  
    25.  
    26.  
    27.  
    28.  
    29. nice.setPixel(20,20,qRgb(255, 255, 39));
    30.  
    31. scene->update();
    32. ui->graphicsView->update();
    33. }
    To copy to clipboard, switch view to plain text mode 

    And nothing is happening. No pixel with different color. Why?
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

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

    Default Re: QGraphicsView and QGraphicsScene

    Quote Originally Posted by Molier View Post
    Drawing point as an elipse with minimal radius is not very elegant.
    What you are trying to do is even less elegant. I'd say that if I want to say "I want a red dot here" then inserting an object representing a dot is exactly that. Thinking like you if I wanted to have a line drawn I would draw a line on an image and insert that image into the scene. Doesn't make much sense if you ask me...

    And nothing is happening. No pixel with different color. Why?
    Why would there be a pixel with different colour if you are placing a blank white image into the scene?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView and QGraphicsScene

    Maybe. I will more explain. I have WinApi code. Using WinApi, u draw on buffer. Then u swap it to graphical output. I wanna do it easily and elegant.
    With QPolygons i will have to make an array of polygons or sth. I don't want to.
    This is just white, because i did not want that as a black (default is black). But note that AFTER THAT i put SetPixel. So why it does not work properly?? (even i erase image.fill(Qt::white); i still see nothing has changed).
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

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

    Default Re: QGraphicsView and QGraphicsScene

    Quote Originally Posted by Molier View Post
    Maybe. I will more explain. I have WinApi code. Using WinApi, u draw on buffer. Then u swap it to graphical output. I wanna do it easily and elegant.
    Maybe you shouldn't be using Graphics View then?

    This is just white, because i did not want that as a black (default is black). But note that AFTER THAT i put SetPixel. So why it does not work properly?? (even i erase image.fill(Qt::white); i still see nothing has changed).
    You modify object A and expect object B to change. Or actually you create object B from a copy of object A then create object C from a copy of object B and when you modify object A you expect object C to change.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView and QGraphicsScene

    I know what u mean. I modified QPixmap instead of QImage. But when i did sth like that:
    Qt Code:
    1. ui->setupUi(this);
    2.  
    3. scene=new QGraphicsScene();
    4. scene->setSceneRect(0,0,350,250);
    5.  
    6. //timer=new QTimer();
    7.  
    8. ui->graphicsView->setScene(scene);
    9.  
    10.  
    11. ui->graphicsView->adjustSize();
    12.  
    13. image=QPixmap(350,250);
    14.  
    15. nice=QImage(350,250,QImage::Format_ARGB32);
    16. //image.fill(Qt::white);
    17. nice.fill(qRgb(0, 0, 39));
    18. image.fromImage(nice);
    19. scene->addPixmap(image); scene->addPixmap(image);
    20. text1=scene->addText("hello",QFont());//text1->setPos(100,110);
    21.  
    22.  
    23.  
    24.  
    25.  
    26.  
    27. nice.setPixel(20,20,qRgb(255, 255, 100));
    28.  
    29.  
    30. scene->update();
    31. ui->graphicsView->update();
    To copy to clipboard, switch view to plain text mode 

    I have black scene.
    In case with QGraphicsView: it is absolutely usefull down here. Why? Beucase i wanna show them in one place.Just as on th picture:



    Added after 42 minutes:


    QPixmap QPixmap::fromImage( const QImage & image, Qt::ImageConversionFlags flags = Qt::AutoColor ).

    So it looks like it is taken as reference variable. But still no changes to see. Any ideas? I really dont want to use addEllipse, or addLine(20,20,1,1,(...)). It will draw a point maybe, but it will take more time. It is not elegant. Even it is default mechanism.
    So how to SetPixel on image, show that on scene, and then once again change sth on image and show too? Any ideas?


    Added after 1 4 minutes:


    No response? I start thinking about
    Qt Code:
    1. addEllipse(..)
    To copy to clipboard, switch view to plain text mode 
    Last edited by Molier; 28th November 2010 at 01:04.
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

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

    Default Re: QGraphicsView and QGraphicsScene

    Quote Originally Posted by Molier View Post
    I have black scene.
    Somehow I'm not suprised since you are adding a blank pixmap to the scene.

    So it looks like it is taken as reference variable. But still no changes to see. Any ideas?
    It returns a copy.

    I really dont want to use addEllipse, or addLine(20,20,1,1,(...)). It will draw a point maybe, but it will take more time.
    More time than blitting a large pixmap? Doubtful

    It is not elegant.
    Then create yourself a GraphicsPointItem that will use QPainter::drawPoint() to draw the dot.

    So how to SetPixel on image, show that on scene, and then once again change sth on image and show too? Any ideas?
    I would start by paying attention when reading documentation of the API. If you want to shoot yourself in the foot, go ahead. But I bet my implementation using QGraphicsEllipseItem would be more efficient than yours using an image. And it would look nicer too.
    Last edited by wysota; 28th November 2010 at 02:21.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    Nov 2010
    Posts
    14
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QGraphicsView and QGraphicsScene

    Yes, i think so. Thanx a million.


    by the way: some time ago i got a specific problem with setting background on QPushButton.
    Qt Code:
    1. ui->pushButton->setPalette(QPalette(QColor(166,240,25)));
    To copy to clipboard, switch view to plain text mode 

    The result is as on the picture above. But on Gnome and Windows it does just not work. Buttons are without colors. Why?


    Added after 27 minutes:


    Shit.. That could be caused by missing color=new QColorDialog(); before. But i will see. Hopefully it will fix the problem.
    Else i will write about it
    Last edited by Molier; 28th November 2010 at 02:18.
    L'État, c'est la grande fiction à travers laquelle tout le monde s’efforce de vivre aux dépens de tout le monde. -Frédéric Bastiat

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

    Default Re: QGraphicsView and QGraphicsScene

    Quote Originally Posted by Molier View Post
    Qt Code:
    1. ui->pushButton->setPalette(QPalette(QColor(166,240,25)));
    To copy to clipboard, switch view to plain text mode 

    The result is as on the picture above. But on Gnome and Windows it does just not work. Buttons are without colors. Why?
    Windows doesn't know how to change the background of buttons. The only way to do it is with stylesheets.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QGraphicsScene & QGraphicsView help please
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 13th February 2017, 13:30
  2. Replies: 0
    Last Post: 5th March 2009, 07:54
  3. QGraphicsView and QGraphicsScene
    By alisami in forum Qt Programming
    Replies: 8
    Last Post: 4th December 2008, 11:10
  4. QGraphicsScene and QGraphicsView
    By sabeesh in forum Qt Programming
    Replies: 7
    Last Post: 1st August 2007, 07:59
  5. QGraphicsScene and QGraphicsView
    By rossd in forum Qt Programming
    Replies: 2
    Last Post: 25th April 2007, 15:43

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.