Results 1 to 7 of 7

Thread: Why I cannot get the size of scene changed according to the QGraphpicsView?

  1. #1
    Join Date
    May 2011
    Location
    North Carolina
    Posts
    23
    Thanks
    10
    Qt products
    Qt4

    Default Why I cannot get the size of scene changed according to the QGraphpicsView?

    HI,

    I want to fit an object of QGraphicsScene into the QGraphicsView and get the scene's size changed according to the object of QGraphicsView. However, I failed.

    First, I defined a class derived from QGraphicsView as follows:
    Qt Code:
    1. #ifndef IQGRAPHICSVIEW_H
    2. #define IQGRAPHICSVIEW_H
    3.  
    4. #include <iostream>
    5. #include <QGraphicsView>
    6. using namespace std;
    7.  
    8. class iQGraphicsView : public QGraphicsView
    9. {
    10. public:
    11. iQGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene) { }
    12.  
    13. protected:
    14. void resizeEvent(QResizeEvent *event)
    15. {
    16. QGraphicsView::resizeEvent(event);
    17. fitInView(sceneRect(), Qt::IgnoreAspectRatio);
    18. }
    19. };
    To copy to clipboard, switch view to plain text mode 

    Then:
    Qt Code:
    1. NLDCanvas = new QGraphicsScene(0,0,460,300);
    2.  
    3. iQGraphicsView* UpperLeft = new iQGraphicsView(NLDCanvas);
    4.  
    5. UpperLeft->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
    To copy to clipboard, switch view to plain text mode 

    I draw the nodes and lines on the background of QGraphicsScene: that is : writing the OpenGL code in the QGraphicsScene::drawForeground
    At first the showing is like this:


    However, after changing the size of the viewport, the image inside doesn't distort and change size according to the viewport but lose some contents as below:


    I have tested that the change of viewport doesn't change the value of sceneRect() belonging to the QGraphicsView.

    I want the scene always fit inside the frame and change&distort according to the frame. What I should do?

    Thanks a lot
    Last edited by superwave; 6th June 2011 at 23:21.

  2. #2
    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: Why I cannot get the size of scene changed according to the QGraphpicsView?

    Your attachments don't work. Please use the forum's attachment system instead. The code you posted seems fine. If it doesn't work as you expect it then probably your implementation of drawForeground() is incorrect.
    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.


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

    superwave (27th June 2011)

  4. #3
    Join Date
    May 2011
    Location
    North Carolina
    Posts
    23
    Thanks
    10
    Qt products
    Qt4

    Default Re: Why I cannot get the size of scene changed according to the QGraphpicsView?

    Thank you very much, I have changed my post.

    I do not think my code is right because I have tested that the change of viewport doesn't change the value of sceneRect() belonging to the QGraphicsView.

  5. #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: Why I cannot get the size of scene changed according to the QGraphpicsView?

    Show us how you populate the scene with items and what your drawForeground implementation looks like.
    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.


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

    superwave (27th June 2011)

  7. #5
    Join Date
    May 2011
    Location
    North Carolina
    Posts
    23
    Thanks
    10
    Qt products
    Qt4

    Default Re: Why I cannot get the size of scene changed according to the QGraphpicsView?

    class grCanvas : public QGraphicsScene

    Qt Code:
    1. void grCanvas::drawBackground(QPainter *painter, const QRectF &rect)
    2. {
    3. if (BackgroundDrawing==FALSE)
    4. {
    5. return;
    6. }
    7. else
    8. {
    9. myCanvasDrawer->RunOpenGL();
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void IRGNodeLinkDrawer::RunOpenGL()
    2. {
    3. IRGraph *graph = docManager->GetIRGraph();
    4. PrepareCanvas(canvas);
    5.  
    6. glEnable(GL_BLEND);
    7. glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
    8. this->DrawEdges(graph);
    9. this->DrawNodes(graph);
    10. glDisable(GL_BLEND);
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    the code of draw edges and nodes are here:
    Qt Code:
    1. void IRGNodeLinkDrawer::DrawEdges(IRGraph *graph)
    2. {
    3. int n = graph->GetEdgeNum();
    4. int *edges = new int [n * 2];
    5. graph->GetAllGraphEdges(edges);
    6. glLineWidth(0.5f);
    7. glBegin(GL_LINES);
    8. if (this->showAllOrSelected == 1) // Show selected nodes
    9. {
    10. for (int i=0; i<n; i++)
    11. {
    12. if (graph->IsNodeSelected(edges[i*2]) && graph->IsNodeSelected(edges[i*2+1]))
    13. {
    14. // glColor4f(0.8f, 0.8f, 0.8f, 0.3f);
    15. glColor4f(0.2f, 0.2f, 0.2f, 0.6f);
    16. glVertex2d(this->posX[edges[i*2]], this->posY[edges[i*2]]);
    17. glVertex2d(this->posX[edges[i*2+1]], this->posY[edges[i*2+1]]);
    18. }
    19. }
    20. }
    21. else
    22. {
    23. for (int i=0; i<n; i++)
    24. {
    25. if (graph->IsNodeSelected(edges[i*2]) && graph->IsNodeSelected(edges[i*2+1]))
    26. glColor4f(0.2f, 0.2f, 0.2f, 0.6f);
    27. else
    28. // glColor4f(0.8f, 0.8f, 0.8f, 0.1f);
    29. glColor4f(0.8f, 0.8f, 0.8f, 0.3f);
    30. glVertex2d(this->posX[edges[i*2]], this->posY[edges[i*2]]);
    31. glVertex2d(this->posX[edges[i*2+1]], this->posY[edges[i*2+1]]);
    32. }
    33. }
    34.  
    35. glEnd();
    36. delete [] edges;
    37. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void IRGNodeLinkDrawer::DrawNodes(IRGraph *graph)
    2. {
    3. int member, colorIdx;
    4. float alpha;
    5.  
    6. if (this->showAllOrSelected == 1) // Show selected nodes
    7. {
    8. glPointSize((float)nodeSize + distortionFactor);
    9. alpha = 1.0f;
    10. glBegin(GL_POINTS);
    11. for (int i=0; i<graph->GetNodeNum(); i++)
    12. {
    13. if (graph->IsNodeSelected(i))
    14. {
    15. member = graph->GetNodeMembership(i);
    16. colorIdx = member % IRGPixelDrawer::colorNum;
    17. glColor4f(qualitativeColorTable[colorIdx][0]/255.f, qualitativeColorTable[colorIdx][1]/255.f, qualitativeColorTable[colorIdx][2]/255.f, alpha);
    18. glVertex2d(this->posX[i], this->posY[i]);
    19. }
    20. }
    21. glEnd();
    22. }
    23. else
    24. {
    25. glColor4f(0, 1.0f, 0, 1);
    26. for (int i=0; i<graph->GetNodeNum(); i++)
    27. {
    28. //glPointSize((float)(nodeSize*graph->GetNodeCentrality(i)));
    29. glPointSize(graph->IsNodeSelected(i) ? (float)nodeSize + distortionFactor * 1.5 : (float)nodeSize);
    30. // glPointSize(graph->IsNodeSelected(i) ? (float)nodeSize + distortionFactor * 1.5 : (float)nodeSize + distortionFactor);
    31. // glPointSize((float)nodeSize + distortionFactor);
    32. glBegin(GL_POINTS);
    33. member = graph->GetNodeMembership(i);
    34. colorIdx = member % IRGPixelDrawer::colorNum;
    35. // alpha = graph->IsNodeSelected(i) ? 1.0f : 0.3f;
    36. alpha = graph->IsNodeSelected(i) ? 1.0f : 0.5f;
    37. glColor4f(qualitativeColorTable[colorIdx][0]/255.f, qualitativeColorTable[colorIdx][1]/255.f, qualitativeColorTable[colorIdx][2]/255.f, alpha);
    38. glVertex2d(this->posX[i], this->posY[i]);
    39. glEnd();
    40. }
    41. }
    42. }
    To copy to clipboard, switch view to plain text mode 

  8. #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: Why I cannot get the size of scene changed according to the QGraphpicsView?

    There is one problem with your code. You are totally ignoring the fact that you are using graphics view. Firstly, you're not using any items anywhere so you might as well use a plain QGLWidget instead of QGraphicsView with a GL widget viewport. Then you're totally ignoring the fact, you should be drawing in the scene coordinates (as I imagine that your prepareCanvas() does something that ruins the scene management completely) so you don't get anything the architecture offers.

    There is a simple question you need to answer - why are you using graphics view architecture instead of QGLWidget? I mean what are you expecting the architecture to offer you?
    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. #7
    Join Date
    May 2011
    Location
    North Carolina
    Posts
    23
    Thanks
    10
    Qt products
    Qt4

    Default Re: Why I cannot get the size of scene changed according to the QGraphpicsView?

    Thank you so much. Your advice on my mistake is dead on the target.

Similar Threads

  1. QScrollArea size of scroll bar is not getting changed
    By jthacker in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 7th September 2010, 09:54
  2. font can not be changed
    By ashishsaryar in forum Qt Programming
    Replies: 4
    Last Post: 28th May 2008, 15:32
  3. Setting the scene size in QGraphicsView
    By Valheru in forum Newbie
    Replies: 5
    Last Post: 11th November 2007, 15:03
  4. Creating a scene from piece of another scene
    By maverick_pol in forum Qt Programming
    Replies: 3
    Last Post: 23rd August 2007, 17:51

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.