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

相关文章

Python中logging模块用法示例总结

《Python中logging模块用法示例总结》在Python中logging模块是一个强大的日志记录工具,它允许用户将程序运行期间产生的日志信息输出到控制台或者写入到文件中,:本文主要介绍Pyt... 目录前言一. 基本使用1. 五种日志等级2.  设置报告等级3. 自定义格式4. C语言风格的格式化方法

Spring 中的切面与事务结合使用完整示例

《Spring中的切面与事务结合使用完整示例》本文给大家介绍Spring中的切面与事务结合使用完整示例,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考... 目录 一、前置知识:Spring AOP 与 事务的关系 事务本质上就是一个“切面”二、核心组件三、完

sky-take-out项目中Redis的使用示例详解

《sky-take-out项目中Redis的使用示例详解》SpringCache是Spring的缓存抽象层,通过注解简化缓存管理,支持Redis等提供者,适用于方法结果缓存、更新和删除操作,但无法实现... 目录Spring Cache主要特性核心注解1.@Cacheable2.@CachePut3.@Ca

QT Creator配置Kit的实现示例

《QTCreator配置Kit的实现示例》本文主要介绍了使用Qt5.12.12与VS2022时,因MSVC编译器版本不匹配及WindowsSDK缺失导致配置错误的问题解决,感兴趣的可以了解一下... 目录0、背景:qt5.12.12+vs2022一、症状:二、原因:(可以跳过,直奔后面的解决方法)三、解决方

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

MySQL分库分表的实践示例

《MySQL分库分表的实践示例》MySQL分库分表适用于数据量大或并发压力高的场景,核心技术包括水平/垂直分片和分库,需应对分布式事务、跨库查询等挑战,通过中间件和解决方案实现,最佳实践为合理策略、备... 目录一、分库分表的触发条件1.1 数据量阈值1.2 并发压力二、分库分表的核心技术模块2.1 水平分

SpringBoot请求参数传递与接收示例详解

《SpringBoot请求参数传递与接收示例详解》本文给大家介绍SpringBoot请求参数传递与接收示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋... 目录I. 基础参数传递i.查询参数(Query Parameters)ii.路径参数(Path Va

RabbitMQ 延时队列插件安装与使用示例详解(基于 Delayed Message Plugin)

《RabbitMQ延时队列插件安装与使用示例详解(基于DelayedMessagePlugin)》本文详解RabbitMQ通过安装rabbitmq_delayed_message_exchan... 目录 一、什么是 RabbitMQ 延时队列? 二、安装前准备✅ RabbitMQ 环境要求 三、安装延时队

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的