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如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Java Spring ApplicationEvent 代码示例解析

《JavaSpringApplicationEvent代码示例解析》本文解析了Spring事件机制,涵盖核心概念(发布-订阅/观察者模式)、代码实现(事件定义、发布、监听)及高级应用(异步处理、... 目录一、Spring 事件机制核心概念1. 事件驱动架构模型2. 核心组件二、代码示例解析1. 事件定义

python使用库爬取m3u8文件的示例

《python使用库爬取m3u8文件的示例》本文主要介绍了python使用库爬取m3u8文件的示例,可以使用requests、m3u8、ffmpeg等库,实现获取、解析、下载视频片段并合并等步骤,具有... 目录一、准备工作二、获取m3u8文件内容三、解析m3u8文件四、下载视频片段五、合并视频片段六、错误

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

spring中的ImportSelector接口示例详解

《spring中的ImportSelector接口示例详解》Spring的ImportSelector接口用于动态选择配置类,实现条件化和模块化配置,关键方法selectImports根据注解信息返回... 目录一、核心作用二、关键方法三、扩展功能四、使用示例五、工作原理六、应用场景七、自定义实现Impor

mysql中insert into的基本用法和一些示例

《mysql中insertinto的基本用法和一些示例》INSERTINTO用于向MySQL表插入新行,支持单行/多行及部分列插入,下面给大家介绍mysql中insertinto的基本用法和一些示例... 目录基本语法插入单行数据插入多行数据插入部分列的数据插入默认值注意事项在mysql中,INSERT I

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

C++ 函数 strftime 和时间格式示例详解

《C++函数strftime和时间格式示例详解》strftime是C/C++标准库中用于格式化日期和时间的函数,定义在ctime头文件中,它将tm结构体中的时间信息转换为指定格式的字符串,是处理... 目录C++ 函数 strftipythonme 详解一、函数原型二、功能描述三、格式字符串说明四、返回值五

LiteFlow轻量级工作流引擎使用示例详解

《LiteFlow轻量级工作流引擎使用示例详解》:本文主要介绍LiteFlow是一个灵活、简洁且轻量的工作流引擎,适合用于中小型项目和微服务架构中的流程编排,本文给大家介绍LiteFlow轻量级工... 目录1. LiteFlow 主要特点2. 工作流定义方式3. LiteFlow 流程示例4. LiteF

MyBatis ResultMap 的基本用法示例详解

《MyBatisResultMap的基本用法示例详解》在MyBatis中,resultMap用于定义数据库查询结果到Java对象属性的映射关系,本文给大家介绍MyBatisResultMap的基本... 目录MyBATis 中的 resultMap1. resultMap 的基本语法2. 简单的 resul