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

QWidget/QMainWindow与QLayout的布局

1.使用QWidget和QVBoxLayout 添加菜单栏和其他控件布局

demo如下:

#include <QWidget>#include <QApplication>
#include <QLayout>
#include <QToolBar>
#include <QWidgetAction>
#include <QPushButton>
#include <QLineEdit>
int main(int argc, char *argv[])
{QApplication a(argc, argv);QWidget w;w.setWindowTitle("testWidgetToolBar");QVBoxLayout *layout = new QVBoxLayout(&w);// 1. 创建 QToolBarQToolBar *toolBar = new QToolBar();// 2. 创建 QWidgetAction 并设置其 QWidget(如 QPushButton)QWidgetAction *widgetAction = new QWidgetAction(toolBar);QPushButton *button = new QPushButton("Button in QWidgetAction");widgetAction->setDefaultWidget(button);// 3. 将 QWidgetAction 添加到 QToolBartoolBar->addAction(widgetAction);// 4. 可选:添加普通 QAction(如分隔符或普通按钮)toolBar->addSeparator();toolBar->addAction("Normal Action");// 5. 将 QToolBar 添加到布局layout->addWidget(toolBar, 0, Qt::AlignTop);// 6. 可选:添加其他控件到布局QLineEdit *lineEdit = new QLineEdit();layout->addWidget(lineEdit,1, Qt::AlignTop);QLineEdit *lineEdit1 = new QLineEdit();layout->addWidget(lineEdit1);layout->setContentsMargins(10,20,40,60);w.show();return a.exec();
}

运行结果如下:

2.使用QWidget介绍QHBoxLayout、QVBoxLayout 的嵌套布局

布局可参考Qt官网

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

相关文章:

  • CSDN使用技巧
  • Pandas中数据分组进阶以及数据透视表
  • 链表-143.重排链表-力扣(LeetCode)
  • 微信小程序集成vant-weapp时,构建npm报错的解决办法
  • 基于springboot的中医养生管理系统
  • Pytorch基础学习--张量(生成,索引,变形)
  • 火语言 RPA 进阶功能:让自动化更实用​
  • 交易高光时刻-01
  • SOP到自动化:一种适合小型金融机构的轻量级开源工具整合方案
  • Vue3+Spring Boot技术栈,前端提交混合表单数据(普通字段+文件字段),上传文件,后端插入数据,将文件保存到数据库
  • Docker端口映射与数据卷完全指南
  • 几张PPT快速了解云原生与华为云CCE方案
  • Eureka和Nacos的原理分析
  • openEuler系统中r如何将docker安装在指定目录
  • CentOS 7常用国内源配置:阿里云、腾讯云、华为云、清华源
  • 从聚类到集成,两种实用算法框架分享
  • 医疗信息化自主可控转型的实践探索 —— 以常德二院为例
  • 为什么调用API总返回404,该如何调试
  • 35、自主移动机器人 (AMR) 调度模拟 (电子厂) - /物流与仓储组件/amr-scheduling-electronics
  • 机器学习-集成算法
  • HarmonyOS 时钟应用开发详解:从零构建实时时钟组件
  • MS17-010永恒之蓝复现
  • Prometheus+Grafana 监控体系搭建:从入门到告警配置
  • open3d-点云函数:变换:旋转,缩放、平移,齐次变换(R,T)等
  • 从“卡脖子”到“自主可控”!电科金仓+东华医为生态协同,打造医疗新范式
  • postman接口自动化测试
  • NavA3——双VLM架构下的先“推理解析”后“定位导航”:理解任意指令,导航至任意地点,查找任意目标
  • opencv基础学习与实战(3)图像形态学与边缘检测
  • langgraph快速搭建agent后端和react前端
  • TOC语法源码生成脚本:基础易纷呈,进阶心渲染(python)