void GraphicsView::populateScene()
{
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
void GraphicsView::drawGrid()
{
const int linesNumber = 48;
int counter=0;
qreal leftLine=0.0;
qreal y = 0;
qreal gridSize = m_fontFactor * 2;
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);
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);
}
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);
}
To copy to clipboard, switch view to plain text mode
Bookmarks