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

QGraphicsAnchorLayout测试pyside6和C++

C++版本

#include <QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsWidget>
#include <QGraphicsAnchorLayout>
#include <QPushButton>#include <QtWidgets>class MyWidget : public QGraphicsWidget
{
public:MyWidget(const QColor &color, const QColor &textColor, const QString &caption,QGraphicsItem *parent = nullptr): QGraphicsWidget(parent), caption(caption), color(color), textColor(textColor){    }void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * = nullptr) override{QFont font;font.setPixelSize(0.75 * qMin(boundingRect().width(), boundingRect().height()));painter->fillRect(boundingRect(), color);painter->save();painter->setFont(font);painter->setPen(textColor);painter->drawText(boundingRect(), Qt::AlignCenter, caption);painter->restore();}
private:QString caption;QColor color;QColor textColor;
};int main(int argc, char *argv[])
{QApplication app(argc, argv);QGraphicsScene scene(0,0,600,400);MyWidget *a = new MyWidget(Qt::blue, Qt::white, "a");a->setPreferredSize(100, 100);MyWidget *b = new MyWidget(Qt::green, Qt::black, "b");b->setPreferredSize(100, 100);MyWidget *c = new MyWidget(Qt::red, Qt::black, "c");c->setPreferredSize(100, 100);QGraphicsAnchorLayout *layout = new QGraphicsAnchorLayout;layout->addCornerAnchors(a, Qt::TopLeftCorner, layout, Qt::TopLeftCorner);layout->addAnchor(b, Qt::AnchorLeft, a, Qt::AnchorRight);layout->addAnchor(b, Qt::AnchorTop, a, Qt::AnchorBottom);layout->addAnchor(b, Qt::AnchorBottom, c, Qt::AnchorTop);layout->addAnchors(b, c, Qt::Horizontal);layout->addCornerAnchors(c, Qt::BottomRightCorner, layout, Qt::BottomRightCorner);auto w = new QGraphicsWidget(nullptr, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);w->setPos(20, 50);w->setMinimumSize(100, 100);w->setPreferredSize(320, 240);w->setLayout(layout);w->setWindowTitle(QApplication::translate("simpleanchorlayout", "QGraphicsAnchorLayout in use"));scene.addItem(w);QGraphicsView view;view.setScene(&scene);view.setWindowTitle(QApplication::translate("simpleanchorlayout", "Simple Anchor Layout"));view.show();return app.exec();
}

测试结果

python版本代码

import sys
from PySide6.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene, QGraphicsWidget,QGraphicsLinearLayout, QGraphicsGridLayout, QGraphicsLayoutItem,QGraphicsAnchorLayout, QGraphicsProxyWidget, QLabel, QLineEdit, QPushButton
)
from PySide6.QtCore import Qt, QSizeFclass MyWidget(QGraphicsWidget):def __init__(self,text = "default"):super().__init__()self.label = QLabel("Hello,{}".format(text))self.label.setAlignment(Qt.AlignCenter)self.label_proxy = QGraphicsProxyWidget()self.label_proxy.setWidget(self.label)layout = QGraphicsLinearLayout()layout.addItem(self.label_proxy)self.setLayout(layout)class AnchorLayoutDemo(QGraphicsView):def __init__(self):super().__init__()self.resize(400, 300)scene = QGraphicsScene()scene.setSceneRect(0, 0, 400, 300)self.setScene(scene)self.layout = QGraphicsAnchorLayout()self.a = MyWidget("aaa")self.a.setPreferredSize(QSizeF(100, 50))self.b = MyWidget("bbb")self.b.setPreferredSize(QSizeF(100, 50))self.c = MyWidget("ccc")self.c.setPreferredSize(QSizeF(100, 50))self.d = MyWidget("ddd")self.d.setPreferredSize(QSizeF(100, 50))self.layout.addCornerAnchors(self.a,Qt.TopLeftCorner, self.layout, Qt.TopLeftCorner)self.layout.addCornerAnchors(self.b,Qt.BottomLeftCorner, self.layout, Qt.BottomLeftCorner)self.layout.addAnchor(self.c, Qt.AnchorLeft, self.a, Qt.AnchorRight)self.layout.addAnchor(self.d, Qt.AnchorRight, self.layout, Qt.AnchorRight)self.layout.addAnchor(self.d, Qt.AnchorBottom, self.layout, Qt.AnchorBottom)self.scene().setBackgroundBrush(Qt.blue)w = QGraphicsWidget()# w.setAutoFillBackground(True)w.setPos(0, 0)w.setMinimumSize(QSizeF(300, 200))w.setPreferredSize(QSizeF(300, 200))w.setLayout(self.layout)self.scene().addItem(w)self.show()self.setWindowTitle("Anchor Layout Demo")      if __name__ == "__main__":app = QApplication.instance() or QApplication(sys.argv)window = AnchorLayoutDemo()window.show()app.exec()

测试结果

import sys
from PySide6.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene, QGraphicsWidget,QGraphicsAnchorLayout, QGraphicsProxyWidget, QLabel, QLineEdit, QPushButton
)
from PySide6.QtCore import Qt, QSizeFclass AnchorLayoutDemo(QGraphicsView):def __init__(self):super().__init__()self.resize(400, 300)scene = QGraphicsScene()scene.setSceneRect(0, 0, 400, 300)self.setScene(scene)# 创建容器(QGraphicsWidget)self.container = QGraphicsWidget()self.container.setGeometry(0, 0, 400, 300)  # 必须设置大小scene.addItem(self.container)# 创建 AnchorLayoutself.layout = QGraphicsAnchorLayout()self.container.setLayout(self.layout)# 创建控件self.label = QLabel("Username:")self.label_proxy = QGraphicsProxyWidget()self.label_proxy.setWidget(self.label)self.label_proxy.setPreferredSize(QSizeF(80, 30))  # 设置固定大小self.input = QLineEdit()self.input_proxy = QGraphicsProxyWidget()self.input_proxy.setWidget(self.input)self.input_proxy.setPreferredSize(QSizeF(200, 30))  # 设置固定大小self.button = QPushButton("Login")self.button_proxy = QGraphicsProxyWidget()self.button_proxy.setWidget(self.button)self.button_proxy.setPreferredSize(QSizeF(100, 30))  # 设置固定大小# 添加控件到布局(通过 addAnchor 定义约束)# 水平方向约束self.layout.addAnchor(self.label_proxy, Qt.AnchorLeft, self.layout, Qt.AnchorLeft)  self.layout.addAnchor(self.input_proxy, Qt.AnchorLeft, self.label_proxy, Qt.AnchorRight) self.layout.addAnchor(self.input_proxy, Qt.AnchorRight, self.layout, Qt.AnchorRight)  self.layout.addAnchor(self.button_proxy, Qt.AnchorHorizontalCenter, self.layout, Qt.AnchorHorizontalCenter) # 垂直方向约束self.layout.addAnchor(self.label_proxy, Qt.AnchorTop, self.layout, Qt.AnchorTop)  self.layout.addAnchor(self.input_proxy, Qt.AnchorTop, self.label_proxy, Qt.AnchorTop)  self.layout.addAnchor(self.button_proxy, Qt.AnchorTop, self.input_proxy, Qt.AnchorBottom)  self.layout.addAnchor(self.button_proxy, Qt.AnchorLeft, self.input_proxy, Qt.AnchorLeft)  if __name__ == "__main__":app = QApplication.instance() or QApplication(sys.argv)window = AnchorLayoutDemo()window.show()app.exec()

python版本代码2

import sys
from PySide6.QtWidgets import (QApplication, QGraphicsView, QGraphicsScene, QGraphicsWidget,QGraphicsAnchorLayout, QGraphicsProxyWidget, QLabel, QLineEdit, QPushButton
)
from PySide6.QtCore import Qt, QSizeFclass AnchorLayoutDemo(QGraphicsView):def __init__(self):super().__init__()self.resize(400, 300)scene = QGraphicsScene()scene.setSceneRect(0, 0, 400, 300)self.setScene(scene)# 创建容器(QGraphicsWidget)self.container = QGraphicsWidget()self.container.setGeometry(0, 0, 400, 300)  # 必须设置大小scene.addItem(self.container)# 创建 AnchorLayoutself.layout = QGraphicsAnchorLayout()self.container.setLayout(self.layout)# 创建控件self.label = QLabel("Username:")self.label_proxy = QGraphicsProxyWidget()self.label_proxy.setWidget(self.label)self.label_proxy.setPreferredSize(QSizeF(80, 30))  # 设置固定大小self.input = QLineEdit()self.input_proxy = QGraphicsProxyWidget()self.input_proxy.setWidget(self.input)self.input_proxy.setPreferredSize(QSizeF(200, 30))  # 设置固定大小self.button = QPushButton("Login")self.button_proxy = QGraphicsProxyWidget()self.button_proxy.setWidget(self.button)self.button_proxy.setPreferredSize(QSizeF(100, 30))  # 设置固定大小# 添加控件到布局(通过 addAnchor 定义约束)# 水平方向约束self.layout.addAnchor(self.label_proxy, Qt.AnchorLeft, self.layout, Qt.AnchorLeft)  self.layout.addAnchor(self.input_proxy, Qt.AnchorLeft, self.label_proxy, Qt.AnchorRight) self.layout.addAnchor(self.input_proxy, Qt.AnchorRight, self.layout, Qt.AnchorRight)  self.layout.addAnchor(self.button_proxy, Qt.AnchorHorizontalCenter, self.layout, Qt.AnchorHorizontalCenter) # 垂直方向约束self.layout.addAnchor(self.label_proxy, Qt.AnchorTop, self.layout, Qt.AnchorTop)  self.layout.addAnchor(self.input_proxy, Qt.AnchorTop, self.label_proxy, Qt.AnchorTop)  self.layout.addAnchor(self.button_proxy, Qt.AnchorTop, self.input_proxy, Qt.AnchorBottom)  self.layout.addAnchor(self.button_proxy, Qt.AnchorLeft, self.input_proxy, Qt.AnchorLeft)  if __name__ == "__main__":app = QApplication.instance() or QApplication(sys.argv)window = AnchorLayoutDemo()window.show()app.exec()

测试结果

http://www.dtcms.com/a/327424.html

相关文章:

  • 微内核与插件化设计思想及其在前端项目中的应用
  • 怎么写好汉语言文学专业的论文?
  • TongSearch3.0.6.0安装和使用指引(by lqw)
  • Day 38: Dataset类和DataLoader类
  • 三点估算法(Three-Point Estimation)
  • OpenHarmony介绍
  • 知识篇 | Oracle Active Data Guard(ADG)同步机制再学习
  • TCP服务器网络编程设计流程详解
  • 车规级霍尔开关芯片SC25891 | 为汽车安全带扣筑起高可靠性安全防线
  • FileLink:为企业跨网文件传输筑牢安全与效率基石
  • Go 语言中的结构体、切片与映射:构建高效数据模型的基石
  • apache+虚拟主机
  • windows git安装步骤
  • 深入剖析 React 合成事件:透过 onClick 看本质
  • Flutter UI Kits by Olayemi Garuba:免费开源的高质量UI组件库
  • C++中template、 implicit 、explicit关键字详解
  • Kimi K2 架构深度解析:万亿MoE模型的效率革命与智能体突破
  • Linux随记(二十二)
  • Notta:高效智能的音频转文字工具
  • 视频抽取关键帧算法
  • MR一体机(VST)预算思路
  • Linux的pthread怎么实现的?(包括到汇编层的实现)
  • AWT 事件监听中的适配器模式:从原理到实战的完整指南
  • Photoshop软件打开WebP文件格的操作教程
  • leecode2439 最小化数组中的最大值
  • 大数据中的数据压缩原理
  • 【解决apisix问题】
  • 快速了解词向量模型
  • RIOT、RT-Thread 和 FreeRTOS 是三种主流的实时操作系统
  • SpringMVC的原理及执行流程?