AlbertoN
27th May 2013, 18:57
Hi all,
I'm tring to develop a calendar like those of Outlook, or Google Calendar. To do that I'm using a QGraphicsView, QGraphicsScene and QGraphicsItem(s). At the moment I draw a grid that represents hours, so there is a solid line for each hour and a dotline for each half hour. I developed it with a small monitor, and when I render it with a bigger one my QGraphicsView is bigger than my scene. So there is white space on both left and right side of my graph. I'm going to attach a picture to show the defect. I tried to use this code to fix:
void GraphicsView::populateScene()
{
m_scene = new QGraphicsScene(this);
setScene(m_scene);
qDebug() << "scene rect: " << m_scene->sceneRect();//this prints: "scene rect: QRectF(0,0 0x0)"
fitInView(m_scene->sceneRect());
qDebug() << "scene rect: " << m_scene->sceneRect();//this prints: "scene rect: QRectF(0,0 0x0)"
drawGrid();
this->fitInView(m_scene->sceneRect());
qDebug() << "scene rect: " << m_scene->sceneRect();//and finally this works: "scene rect: QRectF(-0.5,-0.5 1001x1153) "
//Use ScrollHand Drag Mode to enable Panning
setDragMode(ScrollHandDrag);
}
so as far as I can understand only last fitInView call works, but after that call the view is empty, while if I remove that call I get what you can see in attached picture.
This is the code I used to obtain that grid:
void GraphicsView::drawGrid()
{
const int linesNumber = 48;
int counter=0;
qreal leftLine=0.0;
qreal y = 0;
qreal gridSize = m_fontFactor * 2;
QTime time;
QGraphicsTextItem *item;
QFont textFont("Times New Roman", m_fontFactor);
QPen redPen(QColor(255, 0, 0, 127),1.0,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin) ;
QPen halfHourPen(QColor(201,201,201, 255),1.0,Qt::DotLine,Qt::RoundCap,Qt::RoundJoin);
QPen hourPen(QColor(201, 201, 201, 255),1.0,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin) ;
m_eventRect = QRectF();
for(int i=counter;i<linesNumber;i++)
{
if(i%2==0)
{//draw hour line:
time.setHMS(counter++,0,0);
item = m_scene->addText(time.toString("hh:mm"),textFont);
item->setPos(10, y);//set up text's position
if(leftLine == 0.0)
leftLine = item->boundingRect().width();
m_scene->addLine(0,y,1000,y,hourPen);
m_lines[i] = QRectF(leftLine+11,y+1,1000-5,gridSize-2);
}else{//draw half hour line:
m_scene->addLine(leftLine+10,y,1000,y,halfHourPen);
m_lines[i] = QRectF(leftLine+11,y+1,1000-5,gridSize-2);
}
y += gridSize;
}
m_scene->addLine(leftLine+10,0,leftLine+10,y,redPen);
m_eventRect.setX(0.0);
m_eventRect.setY(0.0);
m_eventRect.setWidth(1000.0);
m_eventRect.setHeight(y);
slotUpdateHourHighlightLine();
setSceneRect(0, 0, 1000, y);
}
m_lines is a QVector<QRectF> used to store where coords' line, so I can use them later in my code (to attach an event with a fixed hour/half hour position)
How can I get size to avoid using "magic numbers" into my code? I mean how to get a width to always has a fullscreen graph? and how to draw always a fullscreen graph?
Thanks for any reply.
Alberto
I'm tring to develop a calendar like those of Outlook, or Google Calendar. To do that I'm using a QGraphicsView, QGraphicsScene and QGraphicsItem(s). At the moment I draw a grid that represents hours, so there is a solid line for each hour and a dotline for each half hour. I developed it with a small monitor, and when I render it with a bigger one my QGraphicsView is bigger than my scene. So there is white space on both left and right side of my graph. I'm going to attach a picture to show the defect. I tried to use this code to fix:
void GraphicsView::populateScene()
{
m_scene = new QGraphicsScene(this);
setScene(m_scene);
qDebug() << "scene rect: " << m_scene->sceneRect();//this prints: "scene rect: QRectF(0,0 0x0)"
fitInView(m_scene->sceneRect());
qDebug() << "scene rect: " << m_scene->sceneRect();//this prints: "scene rect: QRectF(0,0 0x0)"
drawGrid();
this->fitInView(m_scene->sceneRect());
qDebug() << "scene rect: " << m_scene->sceneRect();//and finally this works: "scene rect: QRectF(-0.5,-0.5 1001x1153) "
//Use ScrollHand Drag Mode to enable Panning
setDragMode(ScrollHandDrag);
}
so as far as I can understand only last fitInView call works, but after that call the view is empty, while if I remove that call I get what you can see in attached picture.
This is the code I used to obtain that grid:
void GraphicsView::drawGrid()
{
const int linesNumber = 48;
int counter=0;
qreal leftLine=0.0;
qreal y = 0;
qreal gridSize = m_fontFactor * 2;
QTime time;
QGraphicsTextItem *item;
QFont textFont("Times New Roman", m_fontFactor);
QPen redPen(QColor(255, 0, 0, 127),1.0,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin) ;
QPen halfHourPen(QColor(201,201,201, 255),1.0,Qt::DotLine,Qt::RoundCap,Qt::RoundJoin);
QPen hourPen(QColor(201, 201, 201, 255),1.0,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin) ;
m_eventRect = QRectF();
for(int i=counter;i<linesNumber;i++)
{
if(i%2==0)
{//draw hour line:
time.setHMS(counter++,0,0);
item = m_scene->addText(time.toString("hh:mm"),textFont);
item->setPos(10, y);//set up text's position
if(leftLine == 0.0)
leftLine = item->boundingRect().width();
m_scene->addLine(0,y,1000,y,hourPen);
m_lines[i] = QRectF(leftLine+11,y+1,1000-5,gridSize-2);
}else{//draw half hour line:
m_scene->addLine(leftLine+10,y,1000,y,halfHourPen);
m_lines[i] = QRectF(leftLine+11,y+1,1000-5,gridSize-2);
}
y += gridSize;
}
m_scene->addLine(leftLine+10,0,leftLine+10,y,redPen);
m_eventRect.setX(0.0);
m_eventRect.setY(0.0);
m_eventRect.setWidth(1000.0);
m_eventRect.setHeight(y);
slotUpdateHourHighlightLine();
setSceneRect(0, 0, 1000, y);
}
m_lines is a QVector<QRectF> used to store where coords' line, so I can use them later in my code (to attach an event with a fixed hour/half hour position)
How can I get size to avoid using "magic numbers" into my code? I mean how to get a width to always has a fullscreen graph? and how to draw always a fullscreen graph?
Thanks for any reply.
Alberto