Qt Quick - ApplicationWindow

Qt Quick - ApplicationWindow 使用总结

一、概述

ApplicationWindow 是一个可以方便地向窗口中添加 菜单栏、页眉和 页脚项 的窗口。很像经常使用的界面窗口。这个类似 Window的基础窗口。可以当顶层窗口使用

我们可以将ApplicationWindow声明为应用程序的根元素,并使用QQmlApplicationEngine来运行它。

通过这种方式,我们可以从QML控制窗口的属性、外观和布局。
在这里插入图片描述

基本框架如下:

  import QtQuick.Controls 2.12

  ApplicationWindow {
    
    
      visible: true

      menuBar: MenuBar {
    
    
          // ...
      }

      header: ToolBar {
    
    
          // ...
      }

      footer: TabBar {
    
    
          // ...
      }

      StackView {
    
    
          anchors.fill: parent
      }
  }

二、使用

使用也是很简单的,其实就利用上面的框架,我们可以选择性的显示 menuBar、header、footer、或者内容区域的。

如果不需要使用对应的控件部分的话,就可以不用写这部分内容的。

下面就是我利用这个框架来写的一个类似记事本的界面

在这里插入图片描述

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Layouts 1.3
import QtQuick.Controls 2.5

  ApplicationWindow {
    
    
      visible: true

      width: 600
      height: 400
      title: "主窗体"
      menuBar: MenuBar {
    
    
          // ...
          Menu{
    
    
              title: "文件"
              MenuItem{
    
    
                  text: "新建"
              }
              MenuItem{
    
    
                  text: "保存"
              }
              MenuItem{
    
    
                  text: "另存"
              }
              MenuItem{
    
    
                  text: "关闭"
              }
          }

          Menu{
    
    
              title: "编辑"
              MenuItem{
    
    
                  text: "撤销"
              }
              MenuItem{
    
    
                  text: "剪切"
              }
              MenuItem{
    
    
                  text: "复制"
              }
              MenuItem{
    
    
                  text: "粘贴"
              }
          }

          Menu{
    
    
              title: "格式"
              MenuItem{
    
    
                  text: "自动换行"
              }
              MenuItem{
    
    
                  text: "字体"
              }
          }
          
          Menu{
    
    
              title: "查看"
              MenuItem{
    
    
                  text: "缩放"
              }
              MenuItem{
    
    
                  text: "状态栏"
              }
          }
          
          Menu{
    
    
              title: "帮助"
              MenuItem{
    
    
                  text: "查看帮助"
              }
              MenuItem{
    
    
                  text: "发送反馈"
              }
              MenuItem{
    
    
                  text: "关于记事本"
              }
          }
      }

      header: ToolBar {
    
    
          RowLayout
          {
    
    
            ToolButton
            {
    
    
                text: "☢"
            }
            ToolButton
            {
    
    
                text: "☣"
            }
            ToolButton
            {
    
    
                text: "☠"
            }
            ToolButton
            {
    
    
                text: "☮"
            }
            ToolButton
            {
    
    
                text: "ஐ"
            }
            ToolButton
            {
    
    
                text: "✈"
            }
            ToolButton
            {
    
    
                text: "☏"
            }
            ToolButton
            {
    
    
                text: "☸"
            }
          }

          // ...
      }

      footer: TabBar {
    
    
          // ...

             TabButton{
    
    
                 text: "∷"
             }
             TabButton{
    
    
                 text: "※"
             }
             TabButton{
    
    
                 text: "░"
             }
             
             TabButton{
    
    
                 text: "▒"
             }
             TabButton{
    
    
                 text: "▓"
             }
             TabButton{
    
    
                 text: "♒"
             }
      }

      StackView {
    
    
          anchors.fill: parent
        
          ScrollView{
    
    
              anchors.fill: parent
              TextArea{
    
    
                  text: "内容区域
大方


非 非

非的fw
非ew非任务分为
 few

 ewf
 few 

非二维few非
ew
非
ewfwe非
we非
we非wef ewf ew

非ew
非ewf ewf
 few

非ew
非ew 访问"
              }
          }
      }
  }

三、Page使用

Page是一个容器控件,它可以方便地向 page 添加header 和 footer项。

这个使用和 ApplicationWindow 的使用是一样的,但是 Page 没有menuBar 项目而已,而且这个只是用于普通页面的,没有顶层窗口的功能的哈。

猜你喜欢

转载自blog.csdn.net/qq_43680827/article/details/130324691