qml formLayout实现方式

一、背景

我们制作界面时,通常有表单界面需要制作,如下图:

但是Qt5 是没有 formLayout 的,直到Qt6才有,所以现在 qml 使用 TableView 来实现这个样式

二、实现

cpp 复制代码
enum ComponentType {
        TitleText,
        Text,
        Button,
        Image
    }
TableView {
        id: _ifoView
        clip: true
        property int fColumnWidth: 75
        property int sColumnWidth: 280

        model: TableModel {
            id: _ifoViewModel
            TableModelColumn { display: "FColumn" }
            TableModelColumn { display: "SColumn" }
            rows: []
        }

        delegate: Item {
            implicitWidth: display.dataType === DeviceInfo.TitleText? _ifoView.fColumnWidth : _ifoView.sColumnWidth
            implicitHeight: display.height
            Loader{
                id: _loader
                anchors.fill: parent
                onLoaded: {
                    _loader.item.dataDisplay = display;
                }
            }

            Component.onCompleted: {
                switch (display.dataType){
                case DeviceInfo.TitleText:
                    _loader.sourceComponent = _TitleTextComponent
                    break;
                case DeviceInfo.Text:
                    _loader.sourceComponent = _TextComponent
                    break;
                case DeviceInfo.Button:
                    _loader.sourceComponent = _ButtonComponent
                    break;
                case DeviceInfo.Image:
                    _loader.sourceComponent = _ImageComponent
                    break;
                }
            }
        }
    }
    Component{
        id: _TitleTextComponent
        Rectangle{
            anchors.fill: parent
            property var dataDisplay
            color:"transparent"
            Text {
                text: dataDisplay.title
                anchors.left: parent.left
                opacity: 0.7
                font.pixelSize: 13
            }
        }
    }
    Component{
        id: _TextComponent
        Rectangle{
            anchors.fill: parent
            color:"transparent"
            property var dataDisplay
            Text {
                text: dataDisplay.title
                anchors.left: parent.left
                font.pixelSize: 13
                width: _ifoView.sColumnWidth
                wrapMode: Text.WordWrap
            }
        }
    }
    Component{
        id: _ButtonComponent
        Rectangle{
            anchors.fill: parent
            color:"transparent"
            property var dataDisplay
            Text {
                id: _text
                text: dataDisplay.title
                anchors.left: parent.left
                anchors.top: parent.top
                font.pixelSize: 13
            }
            Button {
                text: qsTr("复制")
                anchors.left: _text.right
                anchors.leftMargin: 10
                anchors.verticalCenter: _text.verticalCenter
                alwaysHighlight: true
                font.pixelSize: 13
                onClicked: {
                }
            }
        }
    }
    Component{
        id: _ImageComponent
        Rectangle{
            anchors.fill: parent
            property var dataDisplay
            color:"transparent"
            Image {
                id: _image
                width: 24
                height: 16
                source: dataDisplay.source
                anchors.left: parent.left
                anchors.top: parent.top
            }
            Text {
                text: dataDisplay.title
                anchors.left: _image.right
                anchors.leftMargin: 5
                anchors.verticalCenter: _image.verticalCenter
                font.pixelSize: 13
            }
        }
    }

数据格式是

cpp 复制代码
function aovIfo()
    {
        var codeRow = {"FColumn": {dataType:DeviceInfo.TitleText, title:"设备", height:20},
            "SColumn": {dataType:DeviceInfo.Button, title:"设备", height:22}}
        _ifoViewModel.appendRow(codeRow);
        var powerLevelRow = {"FColumn": {dataType:DeviceInfo.TitleText, title:"版本", height:20},
            "SColumn": {dataType:DeviceInfo.Image, title:"1%", height:22, source:""}}
        _ifoViewModel.appendRow(powerLevelRow);
相关推荐
DreamLife☼3 天前
Qt 中的 Q_OBJECT 宏详解 —— 从源码到底层机制的全面剖析
qt·信号·qml·q_object··rtti·运行时类型信息
大橘21 天前
【qml-5】qml与c++交互(类型单例)
qt·qml
大橘22 天前
【qml-4】qml与c++交互(类型多例)
qt·qml
眠りたいです23 天前
Qt音频播放器项目实践:文件过滤、元数据提取与动态歌词显示实现
c++·qt·ui·音视频·媒体·qt5·mime
钱彬 (Qian Bin)1 个月前
AI质检数据准备利器:基于Qt/QML 5.14的图像批量裁剪工具开发实战
qt·自定义·图像·qml·qt quick·裁剪工具
啊森要自信1 个月前
【QT】Qt 信号与槽的使用详解&&连接方式&&Lambda表达式定义槽函数
android·开发语言·c++·qt·qt5
钱彬 (Qian Bin)1 个月前
《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——8. AI赋能(下):在Qt中部署YOLOv8模型
人工智能·qt·yolo·qml·qt quick·工业质检·螺丝瑕疵检测
Little-Hu1 个月前
QML 3D曲面图(Surface3D)技术
3d·qml
Little-Hu1 个月前
QML视图组件:ListView、GridView、TableView、PathView
数据库·microsoft·qml
钱彬 (Qian Bin)1 个月前
《使用Qt Quick从零构建AI螺丝瑕疵检测系统》——6. 传统算法实战:用OpenCV测量螺丝尺寸
教程·cmake·qml·qt quick·工业软件·工业瑕疵检测·qt6.9.1