QML | 实现一个转圈等待控件

项目中需要一个转圈等待,就自己实现了一个Rotatingwait,-----------------------后来发现QML有自带的一个等待指示器BusyIndicator(/ㄒoㄒ/)

//RotatingWait.qml
import QtQuick 2.7
import QtGraphicalEffects 1.0

Item {
    Rectangle {
        id: rect
        width: parent.width
        height: parent.height
        color: Qt.rgba(0, 0, 0, 0)
        radius: width / 2
        border.width: width / 6 //设置边框,看起来是一个圆环
        visible: false
    }
    //旋转的渐变色,效果是圆环外部看起来转动的颜色变化
    ConicalGradient {
       width: rect.width
       height: rect.height
       gradient: Gradient {
            GradientStop { position: 0.0; color: "#687698" }
            GradientStop { position: 1.0; color: "#37415F" }
       }
       source: rect //渐变色作用范围
       //边框中的一个圆形, 可以看成旋转的头部
       Rectangle {
           anchors.top: parent.top
           anchors.horizontalCenter: parent.horizontalCenter
           width: rect.border.width
           height: width
           radius: width / 2
           color: "#37415F"
       }

       RotationAnimation on rotation {
           from: 0
           to: 360
           duration: 1000 //旋转速度,默认250
           loops: Animation.Infinite //一直旋转
        }
    }
}

main.qml中使用 

//main.qml
import QtQuick 2.6
import QtQuick.Window 2.2
import QtGraphicalEffects 1.0
Window {
    id: rootWindow
    visible: true
    width: 1920
    height: 1080
    title: qsTr("Roating wait...")
    //设置页面背景渐变色
    LinearGradient {
        anchors.fill: parent
        start: Qt.point(0, 0)
        end: Qt.point(width, 0)
        gradient:Gradient {
            GradientStop {position: 0.0; color: "#2C3753"}
            GradientStop {position: 1.0; color: "#1B2234"}
        }
    }
    //文本提示框
    Text {
        anchors{
            left: parent.left
            leftMargin: parent.width * 0.45
            top: parent.top
            topMargin: parent.height * 0.6
        }
        text: qsTr("正在加载,请稍后...")
        color: "#FFFFFF"
        font{
           pixelSize: 24
           bold: true
        }
    }
    //转圈等待
    RotatingWait {
        anchors.centerIn: parent
        width: rootWindow.width * 0.05
        height: width
    }

}

效果图(没有gif截图软件,静图将就看看,实际效果请自行脑补^_^):

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的使用QML实现文件树控件的示例,它可以列出指定目录下的所有文件和目录,并且支持展开/折叠和双击打开文件的功能。注意,这个示例只是一个基础的实现,你可以根据自己的需求进行扩展和优化。 首先,我们需要定义一个TreeView作为文件树控件的基础,并设置一个自定义的TreeViewDelegate来显示每个节点。在TreeViewDelegate中,我们使用一个RowLayout来显示节点的图标和名称,并通过一个递归调用来显示子节点。具体的代码如下: ``` import QtQuick.Controls 2.4 TreeView { id: fileTreeView anchors.fill: parent model: fileModel delegate: TreeViewDelegate { id: fileTreeDelegate text: model.fileName icon: model.icon RowLayout { spacing: 5 Image { source: icon width: 16 height: 16 } Label { text: text font.bold: model.isDir } } branchDelegate: TreeViewBranchDelegate {} } Component.onCompleted: { // 初始化根目录 var root = { fileName: "Root", filePath: "", isDir: true, children: [] } fileModel.append(root) // 列出根目录下的所有文件和目录 listFiles(root) } function listFiles(parent) { var dir = Qt.resolvedUrl(parent.filePath).toLocalFile() if (dir !== "" && dir !== "." && dir !== "..") { var fileInfos = QDir(dir).entryInfoList(QDir.AllEntries | QDir.NoDotAndDotDot, QDir.Name) for (var i = 0; i < fileInfos.length; i++) { var fileInfo = fileInfos[i] var filePath = fileInfo.absoluteFilePath() var fileName = fileInfo.fileName() var isDir = fileInfo.isDir() var icon = isDir ? "qrc:/folder.png" : "qrc:/file.png" var child = { fileName: fileName, filePath: filePath, isDir: isDir, children: [] } parent.children.push(child) if (isDir) { listFiles(child) } } } } function openFile(node) { if (!node.isDir) { Qt.openUrlExternally(node.filePath) } } // 处理双击事件 MouseArea { anchors.fill: parent onDoubleClicked: { var node = fileTreeView.currentItem.model openFile(node) } } } ``` 在上述代码中,我们使用一个自定义的`fileModel`来作为TreeView的数据模型,它是一个包含多个节点的JavaScript对象。在程序初始化时,我们先添加一个根节点,并列出根目录下的所有文件和目录。在TreeViewDelegate中,我们通过递归调用的方式来显示子节点,并在双击事件中打开文件。 最后,我们需要准备一些文件图标,比如`folder.png`和`file.png`,并在QRC文件中进行注册。你可以根据自己的需求来选择合适的图标。 希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值