QML 基础按钮:Button、RoundButton 与 DelayButton

这篇文章讲 QML 三种最基础的按钮用法:普通按钮、圆形图标按钮和带长按确认的延迟按钮。

  • Button --- 文本、图标、可选中状态与点击反馈
  • RoundButton --- 圆形外观,适合图标操作
  • DelayButton --- 长按触发,防止误操作

Button - 普通按钮

一个带文件图标的按钮,点击后下方显示反馈文字。

演示代码

qml 复制代码
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

FadeInAnimation {
    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 15

        // ... 省略标题组件 TitleSeparator ...

        Button {
            Layout.alignment: Qt.AlignHCenter
            Layout.preferredWidth: 160
            Layout.preferredHeight: 40
            text: "打开文件"
            font.pointSize: 13
            checkable: true
            icon.source: "qrc:/icons/file.png"
            onClicked: textResult.text = "点击了打开按钮"
        }

        Text {
            id: textResult
            Layout.alignment: Qt.AlignHCenter
        }

        Item { Layout.fillHeight: true }
    }
}

关键逻辑解析

  • text 设置按钮文字,icon.source 设置左侧图标,路径用 qrc:/ 引用资源文件。
  • checkable: true 让按钮变成可勾选状态,再次点击会取消选中。
  • onClicked 里直接给 textResult.text 赋值,是最常见的点击反馈写法。

适用场景:文件打开、保存、确认等常规操作。

RoundButton - 圆形按钮

一个圆形图标按钮,悬停时显示 ToolTip 提示。

演示代码

qml 复制代码
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

FadeInAnimation {
    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 15

        // ... 省略标题组件 TitleSeparator ...

        RoundButton {
            Layout.alignment: Qt.AlignHCenter
            Layout.preferredWidth: 64
            Layout.preferredHeight: 64
            icon.source: "qrc:/icons/file.png"
            icon.width: 36
            icon.height: 36
            ToolTip.visible: hovered
            ToolTip.text: "新建文件"
            onClicked: textResult.text = "点击了新建按钮"
        }

        Text {
            id: textResult
            Layout.alignment: Qt.AlignHCenter
        }

        Item { Layout.fillHeight: true }
    }
}

关键逻辑解析

  • RoundButton 默认就是圆形,只要宽高相等,显示出来就是正圆。
  • icon.width / icon.height 控制图标大小,避免图标被拉伸。
  • ToolTip.visible: hovered 绑定到按钮的 hovered 属性,鼠标放上去就显示提示。

适用场景:工具栏、浮动操作按钮、图标密集区域。

DelayButton - 延迟按钮

一个需要长按 2 秒才能触发的按钮,进度实时显示在文字里。

演示代码

qml 复制代码
import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

FadeInAnimation {
    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 15

        // ... 省略标题组件 TitleSeparator ...

        DelayButton {
            Layout.alignment: Qt.AlignHCenter
            Layout.preferredWidth: 200
            Layout.preferredHeight: 40
            text: "确认格式化" + " (" + Math.round(progress * 100) + "%)"
            font.pointSize: 13
            delay: 2000
            onActivated: textResult.text = "Format Partition ..."
        }

        Text {
            id: textResult
            Layout.alignment: Qt.AlignHCenter
        }

        Item { Layout.fillHeight: true }
    }
}

关键逻辑解析

  • delay: 2000 设置按住 2 秒后触发,默认是 3 秒。
  • progress 属性在 0 到 1 之间变化,用 Math.round(progress * 100) 显示百分比。
  • onActivated 是真正触发的地方,只有按住达到 delay 时间才会执行。

适用场景:格式化、删除、重置等不可逆操作,避免误触。

对比表格

按钮 外观 主要属性 典型场景
Button 矩形 texticon.sourcecheckable 常规操作
RoundButton 圆形 icon.widthicon.height 图标按钮
DelayButton 矩形带进度 delayprogressonActivated 危险操作确认

运行验证

  1. Qt Creator 打开 qml_button/CMakeLists.txt
  2. Ctrl+R 运行;
  3. 左侧导航切换到对应 demo,点击、悬停、长按按钮看效果。

扩展复用方向

  • ButtonbackgroundcontentItem,做成项目统一的风格按钮。
  • RoundButton 放到 ToolBar 里,组成图标工具栏。
  • DelayButtonprogress 做动画进度环,增强视觉反馈。

已验证环境

相关推荐
C++ 老炮儿的技术栈12 小时前
基于Qt实现轻量化本地音乐播放器
开发语言·c++·qt·c·播放器·音乐
我不是疯子是傻子16 小时前
Qt 实时曲线卡顿优化:从QPainter到OpenGL的3级加速实战
开发语言·qt
秋田君17 小时前
QT_XML文件操作
xml·数据库·qt
我不是疯子是傻子18 小时前
串口通信数据帧粘包拆包再进化:基于 Qt 的滑动窗口与 CRC 校验实战
开发语言·qt
程序员老陆18 小时前
Qt中实现窗口真全屏(覆盖Windows任务栏)
开发语言·windows·qt
艾莉丝努力练剑1 天前
【QT:解决问题】Qt5Core.dll:无法定位程序输入点
java·开发语言·qt·学习·面试
秋田君2 天前
QT_JSON文件操作
开发语言·qt·json
大白同学4212 天前
初识Qt——信号和槽
开发语言·qt
秋田君2 天前
QT_INI文件操作
开发语言·qt
≮傷£≯√2 天前
FFmpeg + qt 音频播放
qt·ffmpeg·音视频