QT:QT窗口(一)

devtools/2024/9/22 19:05:03/

文章目录

  • 菜单栏
    • 创建菜单栏
    • 在菜单栏中添加菜单
    • 创建菜单项
    • 添加分割线
  • 工具栏
    • 创建工具栏
    • 设置停靠位置
      • 创建工具栏的同时指定停靠位置
      • 使用QToolBar类提供的setAllowedAreas函数来设置停靠位置
    • 设置浮动属性
    • 设置移动属性
  • 状态栏
    • 状态栏的创建
    • 在状态栏中显示实时消息
    • 在状态栏中显示永久消息

菜单栏

创建菜单栏

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);QMenuBar* menubar = new QMenuBar(this);this->setMenuBar(menubar);
}MainWindow::~MainWindow()
{delete ui;
}

在菜单栏中添加菜单

创建菜单:

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);QMenuBar* menubar = new QMenuBar(this);this->setMenuBar(menubar);// 创建菜单QMenu* menu1 = new QMenu("菜单");QMenu* menu2 = new QMenu("编辑");QMenu* menu3 = new QMenu("构建");// 再把菜单设置进去menubar->addMenu(menu1);menubar->addMenu(menu2);menubar->addMenu(menu3);
}MainWindow::~MainWindow()
{delete ui;
}

在这里插入图片描述

创建菜单项

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);QMenuBar* menubar = new QMenuBar(this);this->setMenuBar(menubar);// 创建菜单QMenu* menu1 = new QMenu("菜单");QMenu* menu2 = new QMenu("编辑");QMenu* menu3 = new QMenu("构建");// 再把菜单设置进去menubar->addMenu(menu1);menubar->addMenu(menu2);menubar->addMenu(menu3);// 创建菜单项QAction* act1 = new QAction("open");QAction* act2 = new QAction("close");QAction* act3 = new QAction("create");// 把菜单项设置进去menu1->addAction(act1);menu1->addAction(act2);menu1->addAction(act3);
}MainWindow::~MainWindow()
{delete ui;
}

在这里插入图片描述

添加分割线

可以使用addSeparator来添加分割线:

在这里插入图片描述

工具栏

创建工具栏

调用addToolBar函数来创建工具栏:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);QToolBar* toolbar1 = new QToolBar(this);QToolBar* toolbar2 = new QToolBar(this);this->addToolBar(toolbar1);this->addToolBar(toolbar2);}MainWindow::~MainWindow()
{delete ui;}

设置停靠位置

工具栏停靠位置的设置有两种方式,一种是在创建的时候选择停靠位置,一种是使用setAllowedAreas函数来设置

创建工具栏的同时指定停靠位置

在创建工具栏的同时,就可以设置工具栏的停靠位置,对于这个来说默认的位置是在窗口的最上面,也可以自己来设置:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 创建工具栏QToolBar* toolbar1 = new QToolBar(this);QToolBar* toolbar2 = new QToolBar(this);// 设置一下位置this->addToolBar(Qt::LeftToolBarArea, toolbar1);this->addToolBar(Qt::RightToolBarArea, toolbar2);}MainWindow::~MainWindow()
{delete ui;}

在这里插入图片描述

使用QToolBar类提供的setAllowedAreas函数来设置停靠位置

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 创建工具栏QToolBar* toolbar1 = new QToolBar(this);QToolBar* toolbar2 = new QToolBar(this);// 设置一下位置this->addToolBar(toolbar1);this->addToolBar(toolbar2);// 只允许在左侧停靠toolbar1->setAllowedAreas(Qt::LeftToolBarArea);// 只允许在右侧停靠toolbar2->setAllowedAreas(Qt::RightToolBarArea);}MainWindow::~MainWindow()
{delete ui;}

设置浮动属性

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 创建工具栏QToolBar* toolbar1 = new QToolBar(this);QToolBar* toolbar2 = new QToolBar(this);// 设置一下位置this->addToolBar(toolbar1);this->addToolBar(toolbar2);// 只允许在左侧停靠toolbar1->setAllowedAreas(Qt::LeftToolBarArea);// 只允许在右侧停靠toolbar2->setAllowedAreas(Qt::RightToolBarArea);// 允许和不允许工具栏浮动toolbar1->setFloatable(true);toolbar2->setFloatable(false);}MainWindow::~MainWindow()
{delete ui;}

设置移动属性

移动属性如果设置为false,表示的是停靠位置的操作就不会生效,可以理解为是一个总开关

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QToolBar>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 创建工具栏QToolBar* toolbar1 = new QToolBar(this);QToolBar* toolbar2 = new QToolBar(this);// 设置一下位置this->addToolBar(toolbar1);this->addToolBar(toolbar2);// 只允许在左侧停靠toolbar1->setAllowedAreas(Qt::LeftToolBarArea);// 只允许在右侧停靠toolbar2->setAllowedAreas(Qt::RightToolBarArea);// 允许和不允许移动toolbar1->setMovable(true);toolbar2->setMovable(false);}MainWindow::~MainWindow()
{delete ui;}

状态栏

状态栏是输出简要消息的区域,一般用于主窗口的底部,一个窗口中最多只能有一个状态栏

可以显示有实时消息,永久消息,进度消息等

状态栏的创建

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 创建状态栏QStatusBar* stbar = statusBar();// 把状态栏放到窗口中setStatusBar(stbar);
}MainWindow::~MainWindow()
{delete ui;
}

在状态栏中显示实时消息

可以使用showMessage来实现

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStatusBar>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 创建状态栏QStatusBar* stbar = statusBar();// 把状态栏放到窗口中setStatusBar(stbar);// 在状态栏中显示2s的消息stbar->showMessage("Hello_Qt", 2000);
}MainWindow::~MainWindow()
{delete ui;
}

在状态栏中显示永久消息

简单的设计方案是,把消息用标签来提示

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QStatusBar>
#include <QLabel>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent), ui(new Ui::MainWindow)
{ui->setupUi(this);// 创建状态栏QStatusBar* stbar = statusBar();// 把状态栏放到窗口中setStatusBar(stbar);// 创建标签QLabel* label = new QLabel("这是提示消息", this);stbar->addWidget(label);
}MainWindow::~MainWindow()
{delete ui;
}

在这里插入图片描述
也可以放到右侧

在这里插入图片描述


http://www.ppmy.cn/devtools/33392.html

相关文章

信号量的使用

文章目录 前言一、计数型信号量使用二、二进制信号量实现互斥 前言 队列就像一个传送带&#xff0c;可以传送不同数据。信号量不能传输数据&#xff0c;只能表示资源的数量&#xff0c;信号量只有一个计数值。在前面同步互斥中&#xff0c;我们有两个实验。 同步实验&#xff…

项目使用git开发流程

第一步 项目初期&#xff1a;领导负责的工作 01 创建仓库&#xff1a;在码云上面创建仓库地址&#xff0c;创建完成后点击初始化README&#xff1a;郝陶涛/vue-tea 02 领导在桌面上将代码克隆下来&#xff1a;将代码克隆下来之后&#xff0c;切换到代码内部&#xff0c;使用g…

首支由OpenAI Sora生成的音乐MV震撼诞生:《The Hardest Part》

大家好&#xff0c;我是木易&#xff0c;一个持续关注AI领域的互联网技术产品经理&#xff0c;国内Top2本科&#xff0c;美国Top10 CS研究生&#xff0c;MBA。我坚信AI是普通人变强的“外挂”&#xff0c;所以创建了“AI信息Gap”这个公众号&#xff0c;专注于分享AI全维度知识…

ChatGPT DALL-E绘图,制作各种表情包,实现穿衣风格的自由切换

DALL-E绘图功能探索&#xff1a; 1、保持人物形象一致&#xff0c;适配更多的表情、动作 2、改变穿衣风格 3、小女孩的不同年龄段展示 4、不同社交平台的个性头像创作 如果不会写代码&#xff0c;可以问GPT。使用地址&#xff1a;我的GPT4 视频&#xff0c;B站会发&#…

牛角源码|PHP域名授权系统网站授权授权管理工单系统精美UI支付系统团队合作代理返利发卡系统

产品介绍 功能最多的授权系统&#xff0c;用户系统&#xff0c;工单系统&#xff0c;精美UI&#xff0c;支付系统&#xff0c;授权管理&#xff0c;产品更新&#xff0c;更新详情&#xff0c;代理返利&#xff0c;发卡系统&#xff0c;授权系统&#xff0c;盗版入库&#xff0…

建造者模式

建造者模式简介 建造者模式又称为生成器模式,主要用于对复杂对象的构建和初始化,他可以将多个简单的对象按照一定的顺序一步步组装起来,最终形成一个复杂的成品,比如飞船、火车、计算机、积木等等。建造者模式的主要目的在于把繁琐的构建过程从不同的对象抽离出来,使其脱…

java泛型详解

简介 Java 泛型&#xff08;Generics&#xff09;是 Java 语言在 5.0 版本中引入的一个核心概念&#xff0c;用于在编译时提供更严格的类型检查&#xff0c;并支持编写可重用的代码。通过使用泛型&#xff0c;你可以在类、接口和方法中定义类型参数&#xff0c;这些类型参数在…

Cocos2d,一个能实现梦想的 Python 库

大家好&#xff01;我是爱摸鱼的小鸿&#xff0c;关注我&#xff0c;收看每期的编程干货。 一个简单的库&#xff0c;也许能够开启我们的智慧之门&#xff0c; 一个普通的方法&#xff0c;也许能在危急时刻挽救我们于水深火热&#xff0c; 一个新颖的思维方式&#xff0c;也许能…