QCustomPlot之布局系统

The Layout System布局系统
布局系统主要负责定位和缩放在QCustomPlot上的布局元素比如坐标轴矩形,图例和Plot标题。


Classes and mechanisms类和机制
布局系统基于抽象基类QCPLayoutElement.所有参与布局系统的对象都派生自此类,直接的或间接的。


布局元素的矩形
一个布局元素是一个矩形对象通过两个矩形被描述:内部矩形(QCPLayoutElement::rect)和外部矩形(QCPLayoutElement::setOuterRect)。内部矩形被自动计算通过(QCPLayoutElement::setMargins)来自外矩形的内部margin.内部的矩形是主要内容当margin区域被留下空白或者服务于显示外部图形。例如QCPAxisRect位于四个主要轴的内部矩形。因此graph在他里面并且坐标轴标签和刻度标签在margin区域。
如果多个布局元素被相互安排在旁边或者下面,它可能期望以它们内部矩形的特定的边对齐。因为它们有不同的自动margin,这通常并非如此。这个类QCPMarginGroup和QCPLayoutElement::setMarginGroup修补了这个通过允许同步多个margin。


margins
每一个布局元素都提供一个自动决定它margin的机制。它被实现用QCPLayoutElement::calculateAutoMargin带有一个QCP::MarginSide的函数并且返回一个整型值,代表指定边的真实的margin.自动计算的margin将被用在QCPLayoutElement::setMargins指定的边。默认它被设置为QCP::smAll意味着自动计算的margin被用于四个边。在这种情况下,最小的margin用QCPLayoutElement::setMinimumMargins设置。为了阻止自动margin机制设置更小的margin比期望的情况。如果自动计算的margin不被设置到指定的边,指定边的margin可以直接使用QCPlayoutElement::setMargins设置。


布局
如前所说,一个QCPLayoutElement可能有任意数量的子布局元素和从原则上说只有一个管理/安排这些子元素。这个类是QCPLayout的子类。QCPLayoutElement本身没有可视化。它定义了add,remove和管理子布局元素。QCPLayout不是一个可用的布局。它是具体布局的抽象基类像QCPLayoutGrid在网格线中安排它的子元素并且QCPLayoutInset允许将子元素自由地放置在它的内部矩形。
由于QCPLayout本身就是一个布局元素,通过这种方式可以创建复杂的继承,提供灵活的安排。
下面是一个简单的略图默认的QCPLayoutGrid可以通过QCustomPlot::plotLayout来访问。它展示了在网格布局内部如何放置两个布局元素相邻的(0,0)和(0,1)


顶层Plot布局
每一个QCustomPlot有一个顶层布局器类型是QCPLayoutGrid。它可以通过QCustomPlot::plotLayout来访问并且包含了所有的布局元素.默认这个顶级布局器包含了一个cell可以容纳主要的坐标轴矩形。


Examples
添加一个plot标题是一个典型并且见得的雷子对于了解布局系统的基本工作。
QCPPlotTitle*title = new QCPPlotTitle(customPlot);
title->setText("Plot Title Example");
title->setFont(QFont("sans",12,QFont::Bold));
customPlot->plotLayout()->insertRow(0);

customPlot->plotLayout()->addElement(0,0,title);


安排多个坐标轴矩形实际上是布局系统的重要目的。

[cpp]  view plain  copy
 print ?
  1. customPlot->plotLayout()->clear(); // let's start from scratch and remove the default axis rect  
  2. // add the first axis rect in second row (row index 1):  
  3. QCPAxisRect *bottomAxisRect = new QCPAxisRect(customPlot);  
  4. customPlot->plotLayout()->addElement(1, 0, bottomAxisRect);  
  5. // create a sub layout that we'll place in first row:  
  6. QCPLayoutGrid *subLayout = new QCPLayoutGrid;  
  7. customPlot->plotLayout()->addElement(0, 0, subLayout);  
  8. // add two axis rects in the sub layout next to each other:  
  9. QCPAxisRect *leftAxisRect = new QCPAxisRect(customPlot);  
  10. QCPAxisRect *rightAxisRect = new QCPAxisRect(customPlot);  
  11. subLayout->addElement(0, 0, leftAxisRect);  
  12. subLayout->addElement(0, 1, rightAxisRect);  
  13. subLayout->setColumnStretchFactor(0, 3); // left axis rect shall have 60% of width  
  14. subLayout->setColumnStretchFactor(1, 2); // right one only 40% (3:2 = 60:40)  
  15. // since we've created the axis rects and axes from scratch, we need to place them on  
  16. // according layers, if we don't want the grid to be drawn above the axes etc.  
  17. // place the axis on "axes" layer and grids on the "grid" layer, which is below "axes":  
  18. QList<QCPAxis*> allAxes;  
  19. allAxes << bottomAxisRect->axes() << leftAxisRect->axes() << rightAxisRect->axes();  
  20. foreach (QCPAxis *axis, allAxes)  
  21. {  
  22.   axis->setLayer("axes");  
  23.   axis->grid()->setLayer("grid");  
  24. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值