资源简介
Qt5.6+MSVC,QGraphicsScene QGraphicsView 绘制刻度图
代码片段和文件信息
#include “fixedscene.h“
#include
FixedScene::FixedScene(QWidget *parent)
:QGraphicsScene(parent)
{
mVerticalScaleLength =12;
mHorizenScaleLength =8;
mLength = 54.0;
mSpeed = 25.0;
mLinePen =QPen(Qt::red);
mTextColor=Qt::red;
}
/*!
* @name setViewData(const floatconst float)
* @brief 设置刻度图中的显示信息
* @version 1.0
* @author Lynch
* @date 2016-06-28
* @param length:显示的长度,speed:显示的速度
*/
void FixedScene::setViewData(const float lengthconst float speed)
{
mLength = length;
mSpeed = speed;
}
/*!
* @name drawScaleChart()
* @brief 绘制刻度图表
* @version 1.0
* @author Lynch
* @date 2016-06-23
* @param param
* @attention <>
*/
void FixedScene::drawScaleChart()
{
QRectF pRect = this->sceneRect();
this->addRect(pRectmLinePen);
int pHorizenSpaceing =pRect.width()/12;
int pVerticalSpaceing =pRect.height()/11;
for (int i = 0; i < 5; i += 1 ) {
addLine(QLineF(0+pHorizenSpaceing*i pRect.y() 0+pHorizenSpaceing*i pRect.y()+mHorizenScaleLength*((i+1)%2+1))mLinePen);
if(0==i)
{
QGraphicsTextItem *text = new QGraphicsTextItem();
text->setPos(-8 pRect.y()+mHorizenScaleLength*2);
text->setFont(QFont(“Times“ 10 QFont::Bold));
text->setDefaultTextColor(mTextColor);
text->setPlainText(“0 mm“);
addItem(text);
QGraphicsTextItem *_text = new QGraphicsTextItem();
_text->setPos(pRect.x()pRect.y()+pRect.height()-10*mHorizenScaleLength);
_text->setFont(QFont(“Times“ 10 QFont::Bold));
_text->setDefaultTextColor(mTextColor);
QString str = QString(“ << “)+QString::number(mLength‘f‘1)+“ mm\n << “+QString::number(mSpeed‘f‘1)+“ mm/sec“;
_text->setPlainText(str);
addItem(_text);
}
addLine(QLineF(0+pHorizenSpaceing*i pRect.y()+pRect.height() 0+pHorizenSpaceing*i pRect.y()+pRect.height()-mHorizenScaleLength*((i+1)%2+1))mLinePen);
addLine(QLineF(0-pHorizenSpaceing*i pRect.y() 0-pHorizenSpaceing*i pRect.y()+mHorizenScaleLength*((i+1)%2+1))mLinePen);
addLine(QLineF(0-pHorizenSpaceing*i pRect.y()+pRect.height() 0-pHorizenSpaceing*i pRect.y()+pRect.height()-mHorizenScaleLength*((i+1)%2+1))mLinePen);
}
for (int i = 0; i < 5; i += 1) {
addLine(QLineF(pRect.x()0+pVerticalSpaceing*i pRect.x()+mVerticalScaleLength*((i+1)%2+1) 0+pVerticalSpaceing*i)mLinePen);
addLine(QLineF(pRect.x()0+pVerticalSpaceing*i pRect.x()+mVerticalScaleLength*((i+1)%2+1) 0+pVerticalSpaceing*i)mLinePen);
addLine(QLineF(pRect.x()0-pVerticalSpaceing*i pRect.x()+mVerticalScaleLength*((i+1)%2+1) 0-pVerticalSpaceing*i)mLinePen);
addLine(QLineF(pRect.x()0-pVerticalSpaceing*i pRect.x()+mVerticalScaleLength*((i+1)%2+1) 0-pVer
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2016-06-29 09:46 GraphicsSceneDrawScale\
文件 4532 2016-06-29 09:25 GraphicsSceneDrawScale\fixedscene.cpp
文件 510 2016-06-29 09:27 GraphicsSceneDrawScale\fixedscene.h
文件 437 2016-06-29 09:17 GraphicsSceneDrawScale\GraphicsSceneDrawScale.pro
文件 183 2016-06-29 09:17 GraphicsSceneDrawScale\main.cpp
文件 641 2016-06-29 09:43 GraphicsSceneDrawScale\mainwindow.cpp
文件 363 2016-06-29 09:20 GraphicsSceneDrawScale\mainwindow.h
文件 1585 2016-06-29 09:42 GraphicsSceneDrawScale\mainwindow.ui
评论
共有 条评论