Basic Graphics Layouts Example 官方示例 笔记

2023-10-17 17:32

本文主要是介绍Basic Graphics Layouts Example 官方示例 笔记,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

参考例子:https://doc.qt.io/qt-5/qtwidgets-graphicsview-basicgraphicslayouts-example.html

QGraphicsItem
用于绘制图形项的类。
如果想绘制自定义的图形项,需要继承该类,并实现它里面的两个虚函数:boundingRect()、paint()。
boundingRect() 用于返回 item 绘制的区域的数据,在paint() 函数里实现该 item 的具体绘制。

QGraphicsLayoutItem Class
该类是一个抽象类,通过继承该类,可以让你自定义的 item 能够被布局管理。
QGraphicsLayoutItem 定义了一组虚函数,描述了QGraphicsLayout排列的任何对象的大小、大小策略和大小提示。

QGraphicsLinearLayout Class
继承于 QGraphicsLayoutItem。
该类在Graphics View中提供水平或垂直的布局来管理Widget。默认是水平。

QGraphicsGridLayout Class
继承于 QGraphicsLayoutItem。
该类在Graphcis View中提供网格布局来管理Widget。

源码:

// window.h
#ifndef WINDOW_H
#define WINDOW_H#include <QGraphicsWidget>//! [0]
class Window : public QGraphicsWidget
{Q_OBJECT
public:Window(QGraphicsWidget *parent = nullptr);};
//! [0]#endif  //WINDOW_H
// window.cpp
#include "window.h"
#include "layoutitem.h"#include <QGraphicsLinearLayout>
#include <QGraphicsGridLayout>Window::Window(QGraphicsWidget *parent) : QGraphicsWidget(parent, Qt::Window)
{
//! [0]QGraphicsLinearLayout *windowLayout = new QGraphicsLinearLayout(Qt::Vertical);QGraphicsLinearLayout *linear = new QGraphicsLinearLayout(windowLayout);LayoutItem *item = new LayoutItem;linear->addItem(item);linear->setStretchFactor(item, 1);
//! [0]//! [1]item = new LayoutItem;linear->addItem(item);linear->setStretchFactor(item, 3);windowLayout->addItem(linear);
//! [1]//! [2]QGraphicsGridLayout *grid = new QGraphicsGridLayout(windowLayout);item = new LayoutItem;grid->addItem(item, 0, 0, 4, 1);item = new LayoutItem;item->setMaximumHeight(item->minimumHeight());grid->addItem(item, 0, 1, 2, 1, Qt::AlignVCenter);item = new LayoutItem;item->setMaximumHeight(item->minimumHeight());grid->addItem(item, 2, 1, 2, 1, Qt::AlignVCenter);item = new LayoutItem;grid->addItem(item, 0, 2);item = new LayoutItem;grid->addItem(item, 1, 2);item = new LayoutItem;grid->addItem(item, 2, 2);item = new LayoutItem;grid->addItem(item, 3, 2);windowLayout->addItem(grid);
//! [2]//! [3]setLayout(windowLayout);setWindowTitle(tr("Basic Graphics Layouts Example"));
//! [3]
}
// layoutitem.h
#ifndef LAYOUTITEM_H
#define LAYOUTITEM_H#include <QGraphicsLayoutItem>
#include <QGraphicsItem>
#include <QPixmap>//! [0]
class LayoutItem : public QGraphicsLayoutItem, public QGraphicsItem
{
public:LayoutItem(QGraphicsItem *parent = nullptr);// Inherited from QGraphicsLayoutItemvoid setGeometry(const QRectF &geom) override;QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override;// Inherited from QGraphicsItemQRectF boundingRect() const override;void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget = nullptr) override;private:QPixmap m_pix;
};
//! [0]#endif // LAYOUTITEM_H
// layoutitem.cpp
#include "layoutitem.h"#include <QGradient>
#include <QPainter>//! [0]
LayoutItem::LayoutItem(QGraphicsItem *parent): QGraphicsLayoutItem(), QGraphicsItem(parent),m_pix(QPixmap(QLatin1String("../layoutdemo1-copy/block.png")))
{setGraphicsItem(this);
}
//! [0]//! [1]
void LayoutItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{Q_UNUSED(widget);Q_UNUSED(option);QRectF frame(QPointF(0, 0), geometry().size());const QSizeF pmSize = m_pix.size();QGradientStops stops;
//! [1]//! [2]// paint a background rect (with gradient)QLinearGradient gradient(frame.topLeft(), frame.topLeft() + QPointF(200,200));stops << QGradientStop(0.0, QColor(60, 60,  60));stops << QGradientStop(frame.height() / 2 / frame.height(), QColor(102, 176, 54));//stops << QGradientStop(((frame.height() + h)/2 )/frame.height(), QColor(157, 195,  55));stops << QGradientStop(1.0, QColor(215, 215, 215));gradient.setStops(stops);painter->setBrush(QBrush(gradient));painter->drawRoundedRect(frame, 10.0, 10.0);// paint a rect around the pixmap (with gradient)QPointF pixpos = frame.center() - (QPointF(pmSize.width(), pmSize.height()) / 2);QRectF innerFrame(pixpos, pmSize);innerFrame.adjust(-4, -4, 4, 4);gradient.setStart(innerFrame.topLeft());gradient.setFinalStop(innerFrame.bottomRight());stops.clear();stops << QGradientStop(0.0, QColor(215, 255, 200));stops << QGradientStop(0.5, QColor(102, 176, 54));stops << QGradientStop(1.0, QColor(0, 0,  0));gradient.setStops(stops);painter->setBrush(QBrush(gradient));painter->drawRoundedRect(innerFrame, 10.0, 10.0);painter->drawPixmap(pixpos, m_pix);
}
//! [2]//! [3]
QRectF LayoutItem::boundingRect() const
{return QRectF(QPointF(0, 0), geometry().size());
}
//! [3]//! [4]
void LayoutItem::setGeometry(const QRectF &geom)
{prepareGeometryChange();QGraphicsLayoutItem::setGeometry(geom);setPos(geom.topLeft());
}
//! [4]//! [5]
QSizeF LayoutItem::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
{switch (which) {case Qt::MinimumSize:case Qt::PreferredSize:// Do not allow a size smaller than the pixmap with two frames around it.return m_pix.size() + QSize(12, 12);case Qt::MaximumSize:return QSizeF(1000,1000);default:break;}return constraint;
}
//! [5]

这篇关于Basic Graphics Layouts Example 官方示例 笔记的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/226908

相关文章

MySQL中between and的基本用法、范围查询示例详解

《MySQL中betweenand的基本用法、范围查询示例详解》BETWEENAND操作符在MySQL中用于选择在两个值之间的数据,包括边界值,它支持数值和日期类型,示例展示了如何使用BETWEEN... 目录一、between and语法二、使用示例2.1、betwphpeen and数值查询2.2、be

python中的flask_sqlalchemy的使用及示例详解

《python中的flask_sqlalchemy的使用及示例详解》文章主要介绍了在使用SQLAlchemy创建模型实例时,通过元类动态创建实例的方式,并说明了如何在实例化时执行__init__方法,... 目录@orm.reconstructorSQLAlchemy的回滚关联其他模型数据库基本操作将数据添

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

JAVA项目swing转javafx语法规则以及示例代码

《JAVA项目swing转javafx语法规则以及示例代码》:本文主要介绍JAVA项目swing转javafx语法规则以及示例代码的相关资料,文中详细讲解了主类继承、窗口创建、布局管理、控件替换、... 目录最常用的“一行换一行”速查表(直接全局替换)实际转换示例(JFramejs → JavaFX)迁移建

MyBatis中的两种参数传递类型详解(示例代码)

《MyBatis中的两种参数传递类型详解(示例代码)》文章介绍了MyBatis中传递多个参数的两种方式,使用Map和使用@Param注解或封装POJO,Map方式适用于动态、不固定的参数,但可读性和安... 目录✅ android方式一:使用Map<String, Object>✅ 方式二:使用@Param

SpringBoot实现图形验证码的示例代码

《SpringBoot实现图形验证码的示例代码》验证码的实现方式有很多,可以由前端实现,也可以由后端进行实现,也有很多的插件和工具包可以使用,在这里,我们使用Hutool提供的小工具实现,本文介绍Sp... 目录项目创建前端代码实现约定前后端交互接口需求分析接口定义Hutool工具实现服务器端代码引入依赖获

C#中DateTime的格式符的实现示例

《C#中DateTime的格式符的实现示例》本文介绍了C#中DateTime格式符的使用方法,分为预定义格式和自定义格式两类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录DateTime的格式符1.核心概念2.预定义格式(快捷方案,直接复用)3.自定义格式(灵活可控

MyBatisPlus乐观锁和悲观锁的实现示例

《MyBatisPlus乐观锁和悲观锁的实现示例》本文主要介绍了MyBatisPlus乐观锁和悲观锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录1.场景2.乐观锁和悲观锁3.乐观锁实现4.悲观锁1.场景一件商品,成本价是80元,售价是10

Java中@Accessors使用的实现示例

《Java中@Accessors使用的实现示例》本文主要介绍了Java中@Accessors使用的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录一、@Accessors(chain = true)二、@Accessors(fluent =

pandas批量拆分与合并Excel文件的实现示例

《pandas批量拆分与合并Excel文件的实现示例》本文介绍了Pandas中基于整数位置的iloc和基于标签的loc方法进行数据索引和切片的操作,并将大Excel文件拆分合并,具有一定的参考价值,感... 目录一、Pandas 进行索引和切编程片的iloc、loc方法二、Pandas批量拆分与合并Exce