当前位置: 首页 > news >正文

QT传输函数控件设计10 包含小圆点的图形项

在NodeGraphicsItem小圆点类里面加入新的成员:


    double lastPos;
    double currentPos;
    double nextPos;
    double opacity;

    void setMargin(int x,int y,int w,int h);
    int xStart;
    int yStart;
    int widthRe;
    int heightRe;
    int currentX;
    int currentY;
void NodeGraphicsItem::setMargin(int x, int y, int w, int h)
{
	xStart = x;
	yStart = y;
	widthRe = w;
	heightRe = h;
}

其中,下面设置区域的,就是设置它的起始位置以及宽度。暂时我们先不管它怎么用。说一下其他变量,lastPos是上一个点的位置,这里是小数形式(范围0——1),currentPos是当前位置,以及nextPos是表示下个位置。然后是不透明度(纵轴数据高度)。

在该类中继续添加 NodeXmlItem *nodeXmlItemInGraphics;用来指向一个节点。注意我们用指针类型,直接指向数据本身(而不是其拷贝)

然后在移动函数中:

QVariant NodeGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
	if (change == ItemPositionChange && scene())
	{
		QPointF newPos = value.toPointF();
		
		QRectF rect(xStart + widthRe*lastPos,yStart, widthRe*(nextPos-lastPos),heightRe  );
		if (!rect.contains(newPos))
		{
			newPos.setX(qMin(rect.right(), qMax(newPos.x(), rect.left())));
			newPos.setY(qMin(rect.bottom(), qMax(newPos.y(), rect.top())));
			currentPos = (double)(newPos.x() - xStart) / (double)(widthRe);
			nodeXmlItemInGraphics->setNormalizedIntensity(currentPos);
			opacity = (double)(yStart + heightRe - newPos.y()) / (double)(heightRe);
			nodeXmlItemInGraphics->setOpacity(opacity);
			//DebugText::getDebugText()->addContents(QString::number(currentPos));
			//DebugText::getDebugText()->addContents(QString::number(opacity));
			return newPos;
		}
		currentPos = (double)(newPos.x() - xStart) / (double)(widthRe);
		nodeXmlItemInGraphics->setNormalizedIntensity(currentPos);
		opacity = (double)(yStart + heightRe - newPos.y()) / (double)(heightRe);
		nodeXmlItemInGraphics->setOpacity(opacity);
		//DebugText::getDebugText()->addContents(QString::number(currentPos));
		//DebugText::getDebugText()->addContents(QString::number(opacity));
	}
	return QGraphicsItem::itemChange(change, value);
}

这里的移动范围是根据我们设置的前后位置来设置的。这样,我们只需要在每次绘制点的时候把所有的坐标的前面的一个坐标位置,和后面的一个坐标位置设置进去,就能得到最后我们想要的值。

然后我们再继承一个类,继承自线段图形项

class LigatureForNodesGraItem : public QGraphicsLineItem

里面你可以自己随便设置线宽和粗细,因为没有什么自带的函数,这里就不把程序列入了。

之后在我们的矩形图形项中,我们需要能够添加并且使用这些小圆点。我们在矩形图形项中添加如下成员:

	TsfunXmlItem *tsfunXmlItemInView;

	NodeGraphicsItem *item;
	NodeGraphicsItemList NdGraItemlist;

	LigatureForNodesGraItem *ligatureitem;
	LigatureItemList ligatureitemlist;

	Margin tfgraItemMargin;
	void initializeLigature();
	void itemsUpdate();
	void ligatureUpdate();
	void setWidthHeight(int, int);
	void setPaintable(int, int);
	int width;
	int height;
	int paintWidth;
	int paintHeight;

首先我们有一个指针,指向xml文件读出以后的数据结构(指向其本身)。然后是NodeGraphicsItem的指针和列表。注意这个列表里面的元素也是指针类型的。然后定义了画线的类。以及画线的列表(注意这个列表你可以用typedef别名,也可以直接QList<T>创建,这里不再赘述)。

之后设置显示区域Margin。

然后设置更新图形项的函数,以及更新线段项的函数,设置宽高,设置可画线区域。关于区域后面再说。(其实如果你有其他想法完全可以按自己的想法设置区域)

初始化函数里我们这么定义:

TsfunGraphicsItem::TsfunGraphicsItem(QGraphicsRectItem * parent) : QGraphicsRectItem(parent),
tfgraItemMargin(0,0,0,0)
{
	setBrush(QBrush(QColor(100, 200, 100, 50)));

	setZValue(0);
	//指针指向原始数据
	tsfunXmlItemInView = tsfunxmlitem;

	//显示并添加item
	double lastPosn = 0, currentPosn = 0, nextPosn = 0;
	int i = 0;
	for (i = 0;i < tsfunXmlItemInView->nodeXmlItemList.count() - 1;i++) {
		item = new NodeGraphicsItem();
		item->index = i;
		item->nodeXmlItemInGraphics = &(tsfunXmlItemInView->nodeXmlItemList[i]);
		item->currentPos = tsfunXmlItemInView->nodeXmlItemList[i].returnNormalizedIntensity();
		item->opacity = tsfunXmlItemInView->nodeXmlItemList[i].returnOpacity();
		item->lastPos = lastPosn;
		item->nextPos = tsfunXmlItemInView->nodeXmlItemList[i + 1].returnNormalizedIntensity();
		lastPosn = item->currentPos;
		if (i == 0) {
			item->nextPos = 0;
		}
		NdGraItemlist.append(item);//一定要注意传入实体
	}
	item = new NodeGraphicsItem;
	item->nodeXmlItemInGraphics = &(tsfunXmlItemInView->nodeXmlItemList[i]);
	item->index = i;
	item->lastPos = 1;item->currentPos = 1;item->nextPos = 1;
	item->opacity = tsfunXmlItemInView->nodeXmlItemList[i].returnOpacity();
	NdGraItemlist.append(item);

}

这里的tsfunXmlItemInView = tsfunxmlitem;中的tsfunxmlitem是总程序一开始就创建的指针,指向读入的xml文件中的某一个tsfunxmlitem(之前讲过xml文件里有一个根节点group,group里面有两个子节点item)。

显示并添加NodeGraItem,根据里面的原始值来设置rgb和不透明度。这里我比较懒,就没有设置rgb,只读出了位置和不透明度opacity。初始点的前后都是0,让它不能移动。结尾点前后都是1,也让它不能移动。

同时我们让nodeXmlItemInGraphics指向了原始数据的一个NodeXmlItem,这样,当我们运动的时候,原始数据(非拷贝值)就被修改了,根据原始数据生成的新的NodeGraItem的坐标就是你移动后的坐标了。

这样初始化就完成了。

相关文章:

  • QT传输函数控件设计11 包含小圆点的图形项2
  • QT传输函数控件设计12 自定义信号和槽
  • QT传输函数控件设计13 大结局
  • QT三维图形1
  • QT三维图形2
  • QT三维图形3
  • QT三维图形4
  • icache的方面以及使用
  • cmp bne 以及sub指令的详解
  • 关于ARM Cortex a 系列的看门狗定时器
  • C语言之 认识可变参数
  • ARM cortex a 的SDRAM (DDR)
  • C语言 之递归函数
  • C语言 之建立静态链接库
  • ARM的PWM定时器1
  • 2017-08-04 前端日报
  • 4. 路由到控制器 - Laravel从零开始教程
  • Android优雅地处理按钮重复点击
  • Angular 2 DI - IoC DI - 1
  • Angular Elements 及其运作原理
  • angular2 简述
  • AWS实战 - 利用IAM对S3做访问控制
  • EOS是什么
  • ES6 ...操作符
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • JavaScript 基础知识 - 入门篇(一)
  • Java应用性能调优
  • Mithril.js 入门介绍
  • MobX
  • PHP的Ev教程三(Periodic watcher)
  • rabbitmq延迟消息示例
  • React Transition Group -- Transition 组件
  • Tornado学习笔记(1)
  • Yii源码解读-服务定位器(Service Locator)
  • 半理解系列--Promise的进化史
  • 海量大数据大屏分析展示一步到位:DataWorks数据服务+MaxCompute Lightning对接DataV最佳实践...
  • 猴子数据域名防封接口降低小说被封的风险
  • 记录:CentOS7.2配置LNMP环境记录
  • 面试遇到的一些题
  • 模仿 Go Sort 排序接口实现的自定义排序
  • 使用agvtool更改app version/build
  • 使用Swoole加速Laravel(正式环境中)
  • 正则表达式
  • 3月27日云栖精选夜读 | 从 “城市大脑”实践,瞭望未来城市源起 ...
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • # .NET Framework中使用命名管道进行进程间通信
  • (Matalb时序预测)WOA-BP鲸鱼算法优化BP神经网络的多维时序回归预测
  • (Redis使用系列) Springboot 使用redis实现接口Api限流 十
  • (SpringBoot)第七章:SpringBoot日志文件
  • (一) springboot详细介绍
  • (转)甲方乙方——赵民谈找工作
  • ***利用Ms05002溢出找“肉鸡
  • .bat批处理(十一):替换字符串中包含百分号%的子串
  • .NET 5种线程安全集合
  • .NET CORE 2.0发布后没有 VIEWS视图页面文件