QML构建MainWindow模板,包含菜单栏、工具栏、状态栏

  • Post author:
  • Post category:其他




界面

在这里插入图片描述



软件版本

Qt 5.15.2



说明

messageBox使用Qt.createQmlObject构造一个新的MessageDialog,相当于C++中new一个对象,而且用完得自己destroy;如果直接写MessageDialog,实例会一直存在,直到调用destroy或者程序结束才会被销毁,而在被销毁之前再次open时是直接显示的,不是“弹出”,显然没有那味儿了,在使用Dialog这一点上QML着实是有点操蛋了,这不能像C++那样啥使用我就啥时候直接搞个局部对象那么方便,有点自己造轮子的意思了,呵呵。



代码

有报错请忽略,不影响运行,或者自行优化。

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15
import QtQuick.Dialogs 1.3
import QtQuick.Controls 1.4 as Ctrl14

ApplicationWindow {
    id: root
    width: 960
    height: 720
    visible: true
    title: qsTr("QML MainWindow Temp")

    property var aboutDlg: null
    function showAbout()
    {
        if(aboutDlg == null)
        {
            aboutDlg = Qt.createQmlObject(
                        'import QtQuick 2.15;
                        import QtQuick.Dialogs 1.3;
                        MessageDialog
                        {
                            icon: StandardIcon.Information;
                            text: "这是一个MainWindow示例";
                            visible: true;
                        }',
                        root,
                        "aboutDlg");
            aboutDlg.accepted.connect(onAboutDlgClosed);
        }
    }
    function onAboutDlgClosed()
    {
        aboutDlg.destroy();
        aboutDlg = null;
    }
    //菜单栏
    menuBar: MenuBar
    {
        Menu
        {
            title: qsTr("&File")
            Action { text: qsTr("&New...") }
            Action { text: qsTr("&Open...") }
            Action { text: qsTr("&Save") }
            Action { text: qsTr("Save &As...") }
            MenuSeparator { }   //分割线
            Action
            {
                text: qsTr("&Quit")
                onTriggered: close()
            }
        }
        Menu
        {
            title: qsTr("&Edit")
            Action { text: qsTr("Cu&t") }
            Action { text: qsTr("&Copy") }
            Action { text: qsTr("&Paste") }
        }
        Menu
        {
            title: qsTr("&Help")
            Action
            {
                text: qsTr("&About")
                onTriggered:
                {
                    root.showAbout()
                }
            }
        }
    }
    //工具栏
    header: ToolBar
    {
        Row
        {
            spacing: 10
            ToolButton
            {
                text: 'ToolButton1'
                onClicked: console.log('Button1 Clicked.')
            }
            ToolButton
            {
                text: 'ToolButton2'
                onClicked: console.log('Button2 Clicked.')
            }
            ToolButton
            {
                text: 'ToolButton3'
                onClicked: console.log('Button3 Clicked.')
            }
        }
        background:Rectangle
        {
            anchors.fill:parent
            color:'#008888'
        }
    }
    //状态栏
    footer: Ctrl14.StatusBar
    {
        Row
        {
            Text
            {
                id: statusBar
                text: qsTr("MainWindow StatusBar")
                font.family: 'Arial'
                font.pixelSize: 16
                font.italic: true
                color: '#ff0088'
            }
        }
    }
}



版权声明:本文为wwplh5520370原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。