[QT]QT视图标尺功能实现
实现了QT的视图的标尺功能
代码
int cn = scene->coordinateNumber();
int cofs = scene->coordinateOffset();
int sceneHeight = height();
QRectF rect;
//ver
for( int i = 0 ; i < cn ; i += 5 )
{
painter->setFont( maxFont );
QPointF pt( 0 , i * cofs );
pt = scene->viewportTransform().map( pt );//将坐标转换成scene的viewport坐标系中
//小于0或大于当前窗口大小都不需要显示
if( pt.y() < 0 )
continue;
if( pt.y() > sceneHeight )
break;
rect.setTop( pt.y() + 16.0f );
rect.setLeft( 0 );
rect.setWidth( 12.0f );
rect.setHeight( 12.0f );
painter->drawText( rect , Qt::AlignCenter , tr("%1").arg(i) );
}
int cn = scene->coordinateNumber();
int cofs = scene->coordinateOffset();
int sceneHeight = height();
QRectF rect;
//ver
for( int i = 0 ; i < cn ; i += 5 )
{
painter->setFont( maxFont );
QPointF pt( 0 , i * cofs );
pt = scene->viewportTransform().map( pt );//将坐标转换成scene的viewport坐标系中
//小于0或大于当前窗口大小都不需要显示
if( pt.y() < 0 )
continue;
if( pt.y() > sceneHeight )
break;
rect.setTop( pt.y() + 16.0f );
rect.setLeft( 0 );
rect.setWidth( 12.0f );
rect.setHeight( 12.0f );
painter->drawText( rect , Qt::AlignCenter , tr("%1").arg(i) );
}