PDA

View Full Version : Why I cannot get the size of scene changed according to the QGraphpicsView?



superwave
6th June 2011, 21:38
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:

#ifndef IQGRAPHICSVIEW_H
#define IQGRAPHICSVIEW_H

#include <iostream>
#include <QGraphicsView>
using namespace std;

class iQGraphicsView : public QGraphicsView
{
public:
iQGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene) { }

protected:
void resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
fitInView(sceneRect(), Qt::IgnoreAspectRatio);
}
};

Then:

NLDCanvas = new QGraphicsScene(0,0,460,300);

iQGraphicsView* UpperLeft = new iQGraphicsView(NLDCanvas);

UpperLeft->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));

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:
http://www.qtcentre.org/attachment.php?attachmentid=6543&d=1307398761

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:
http://www.qtcentre.org/attachment.php?attachmentid=6542&d=1307398760

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

wysota
6th June 2011, 22:15
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.

superwave
7th June 2011, 00:23
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.

wysota
7th June 2011, 00:30
Show us how you populate the scene with items and what your drawForeground implementation looks like.

superwave
7th June 2011, 00:40
class grCanvas : public QGraphicsScene


void grCanvas::drawBackground(QPainter *painter, const QRectF &rect)
{
if (BackgroundDrawing==FALSE)
{
return;
}
else
{
myCanvasDrawer->RunOpenGL();
}
}



void IRGNodeLinkDrawer::RunOpenGL()
{
IRGraph *graph = docManager->GetIRGraph();
PrepareCanvas(canvas);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
this->DrawEdges(graph);
this->DrawNodes(graph);
glDisable(GL_BLEND);

}

the code of draw edges and nodes are here:

void IRGNodeLinkDrawer::DrawEdges(IRGraph *graph)
{
int n = graph->GetEdgeNum();
int *edges = new int [n * 2];
graph->GetAllGraphEdges(edges);
glLineWidth(0.5f);
glBegin(GL_LINES);
if (this->showAllOrSelected == 1) // Show selected nodes
{
for (int i=0; i<n; i++)
{
if (graph->IsNodeSelected(edges[i*2]) && graph->IsNodeSelected(edges[i*2+1]))
{
// glColor4f(0.8f, 0.8f, 0.8f, 0.3f);
glColor4f(0.2f, 0.2f, 0.2f, 0.6f);
glVertex2d(this->posX[edges[i*2]], this->posY[edges[i*2]]);
glVertex2d(this->posX[edges[i*2+1]], this->posY[edges[i*2+1]]);
}
}
}
else
{
for (int i=0; i<n; i++)
{
if (graph->IsNodeSelected(edges[i*2]) && graph->IsNodeSelected(edges[i*2+1]))
glColor4f(0.2f, 0.2f, 0.2f, 0.6f);
else
// glColor4f(0.8f, 0.8f, 0.8f, 0.1f);
glColor4f(0.8f, 0.8f, 0.8f, 0.3f);
glVertex2d(this->posX[edges[i*2]], this->posY[edges[i*2]]);
glVertex2d(this->posX[edges[i*2+1]], this->posY[edges[i*2+1]]);
}
}

glEnd();
delete [] edges;
}



void IRGNodeLinkDrawer::DrawNodes(IRGraph *graph)
{
int member, colorIdx;
float alpha;

if (this->showAllOrSelected == 1) // Show selected nodes
{
glPointSize((float)nodeSize + distortionFactor);
alpha = 1.0f;
glBegin(GL_POINTS);
for (int i=0; i<graph->GetNodeNum(); i++)
{
if (graph->IsNodeSelected(i))
{
member = graph->GetNodeMembership(i);
colorIdx = member % IRGPixelDrawer::colorNum;
glColor4f(qualitativeColorTable[colorIdx][0]/255.f, qualitativeColorTable[colorIdx][1]/255.f, qualitativeColorTable[colorIdx][2]/255.f, alpha);
glVertex2d(this->posX[i], this->posY[i]);
}
}
glEnd();
}
else
{
glColor4f(0, 1.0f, 0, 1);
for (int i=0; i<graph->GetNodeNum(); i++)
{
//glPointSize((float)(nodeSize*graph->GetNodeCentrality(i)));
glPointSize(graph->IsNodeSelected(i) ? (float)nodeSize + distortionFactor * 1.5 : (float)nodeSize);
// glPointSize(graph->IsNodeSelected(i) ? (float)nodeSize + distortionFactor * 1.5 : (float)nodeSize + distortionFactor);
// glPointSize((float)nodeSize + distortionFactor);
glBegin(GL_POINTS);
member = graph->GetNodeMembership(i);
colorIdx = member % IRGPixelDrawer::colorNum;
// alpha = graph->IsNodeSelected(i) ? 1.0f : 0.3f;
alpha = graph->IsNodeSelected(i) ? 1.0f : 0.5f;
glColor4f(qualitativeColorTable[colorIdx][0]/255.f, qualitativeColorTable[colorIdx][1]/255.f, qualitativeColorTable[colorIdx][2]/255.f, alpha);
glVertex2d(this->posX[i], this->posY[i]);
glEnd();
}
}
}

wysota
7th June 2011, 01:40
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?

superwave
7th June 2011, 18:31
Thank you so much. Your advice on my mistake is dead on the target.