qml tableview 表格

 作者,QQ1284524409。

预览,

qml tableview 表格_第1张图片

deploy.pri

HEADERS += \
    $$PWD/deploy.h

SOURCES += \
    $$PWD/deploy.cpp

 deploy.h

#ifndef DEPLOY_H
#define DEPLOY_H

#include 
#include 

class Deploy : public QObject{

  Q_OBJECT

public:
    explicit Deploy(QObject *parent = nullptr);
    ~Deploy();

public slots:
    void slot_radioButton_onClicked();

public:
    QQmlApplicationEngine *gp_qmlEngine;
    QObject               *gp_rootObject;

public:
    void gm_setVisable(bool b);
};

#endif // DEPLOY_H

deploy.cpp

#include "deploy.h"
#include 
#include 
#include 
#include 

Deploy::Deploy(QObject *parent) :
    QObject(parent)
{
    gp_qmlEngine = new QQmlApplicationEngine(this);
    QQmlComponent lv_component(gp_qmlEngine, QUrl(QStringLiteral("qrc:/qmls/login/body/deploy/deploy.qml")));
    gp_rootObject = lv_component.create();
    gp_rootObject->setParent(this);
    QObject::connect(gp_rootObject, SIGNAL(signal_radioButton_onClicked()),
                     this, SLOT(slot_radioButton_onClicked()));
    QVariantMap m;
//    for(int i = 0; i<=5; i++)
//    {
        m.insert("a",QVariant(false));
        m.insert("b","1");
        QMetaObject::invokeMethod(gp_rootObject, "slot_listModelAppend", Q_ARG(QVariant, m));
        m.insert("a",QVariant(true));
        m.insert("b","2");
        QMetaObject::invokeMethod(gp_rootObject, "slot_listModelAppend", Q_ARG(QVariant, m));
//    }
        gp_rootObject->setProperty("gv_boolSwitchDelegateStatus",true);
}

Deploy::~Deploy()
{
}

void Deploy::slot_radioButton_onClicked()
{
    int gv_intTotalRow = gp_rootObject->property("gv_intTotalRow").toInt();
    int gv_intCurrentRow = gp_rootObject->property("gv_intCurrentRow").toInt();
//    QList gv_listBName;
//    gv_listBName.append(gp_rootObject->property("gv_listBName").toStringList());
    qDebug()<setProperty("visible", b);
}

deploy.qml

/****************************************************************************
**
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.12 as Controls212
import QtQuick.Layouts 1.1

Window {
    property int gv_intCurrentRow: 0
    property int gv_intTotalRow: 0
//    property var gv_listBName: []
    property bool gv_boolSwitchDelegateStatus: false

    signal signal_onBodyClose()
    signal signal_buttonAdd_onClicked()
    signal signal_button1Del_onClicked()
    signal signal_radioButton_onClicked()

    function slot_listModelAppend(add){
        listModel.append(add)
    }

    function slot_listModelRemove(del){
        if(del === -1)
            return
        else
            listModel.remove(del)
    }

    function slot_listModelClear(){
        listModel.clear()
    }

    function slot_listModelChangeA(index, value){
        listModel.setProperty(index, "a", value)
    }

    function slot_RadioButtonSetUnchecked(index){
        listModel.setProperty(index,"a",false)
    }

    function slot_RadioButtonSetChecked(index){
        listModel.setProperty(index,"a",true)
    }

    id: root
    visible: true
    title:qsTr("部署系统")
    width:600; height:400
    maximumWidth: 600; maximumHeight: 400
    minimumWidth: 600; minimumHeight: 400
    x: Screen.width / 2 - width / 2
    y: Screen.height / 2 - height / 2
    color: "#808080"
    onVisibleChanged: {
        if(root.visible == true)
        {
            root.x = Screen.width / 2 - width / 2
            root.y = Screen.height / 2 - height / 2
        }
        else
            signal_onBodyClose()
    }

    Rectangle {
        id: rectangle
        x: 0
        y: 0
        width: 600
        height: 40
        color: "#00000000"
        border.color: "#00000000"
        anchors.topMargin: 10

        Controls212.Button {
            onClicked: {
                signal_radioButton_onClicked()
            }
            id: button
            x: 42
            width: 35
            height: 35
            text: qsTr("重启")
            anchors.verticalCenter: parent.verticalCenter
            anchors.leftMargin: 55

            contentItem: Text {
                color: button.down ? "green" : "white"
                text: "添加"
                anchors.fill: parent
                font: button.font
                opacity: enabled ? 1.0 : 0.3
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                elide: Text.ElideRight
            }

            background: Rectangle {
                implicitWidth: 100
                implicitHeight: 40
                opacity: enabled ? 1 : 0.3
                color: button.down ? "white" : "green"
                border.color: button.down ? "green" : "white"
                border.width: 1
                radius: 2
            }
        }

        Controls212.Button {
            onClicked: {
                slot_listModelRemove(gv_intCurrentRow)
            }
            id: button1
            y: -8
            width: 35
            height: 35
            text: qsTr("重启")
            anchors.verticalCenterOffset: 0
            contentItem: Text {
                color: button1.down ? "green" : "white"
                text: "删除"
                verticalAlignment: Text.AlignVCenter
                elide: Text.ElideRight
                opacity: enabled ? 1.0 : 0.3
                anchors.fill: parent
                font: button1.font
                horizontalAlignment: Text.AlignHCenter
            }
            background: Rectangle {
                color: button1.down ? "white" : "green"
                radius: 2
                implicitWidth: 100
                border.width: 1
                border.color: button1.down ? "green" : "white"
                opacity: enabled ? 1 : 0.3
                implicitHeight: 40
            }
            anchors.leftMargin: 10
            anchors.left: button.right
            anchors.verticalCenter: parent.verticalCenter
        }
    }

    TableView {
        onClicked: {
            gv_intCurrentRow = tableView.currentRow
        }
        onRowCountChanged: {
            gv_intCurrentRow = tableView.currentRow
        }
        id: tableView
        frameVisible: false
        anchors.top: rectangle.bottom
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.left: parent.left

        TableViewColumn {
            id: radioButtonCol
            role: "a"
            title: qsTr("当前启动项")
            horizontalAlignment : Text.AlignHCenter
            movable: false
            resizable: false
            width: 120
        }

        TableViewColumn {
            id: textCol
            role: "b"
            title: qsTr("系统名称")
            horizontalAlignment : Text.AlignHCenter

            movable: false
            resizable: false
            width: 340
        }

        TableViewColumn {
            id: switchCol
            role: "c"
            title: "传统|UEFI"
            horizontalAlignment : Text.AlignHCenter
            movable: false
            resizable: false
            width: 120
        }

        model:ListModel {
            id: listModel
//            ListElement {
//                a: true
//                b: "Jens"
//                c: false
//            }
        }

        itemDelegate: Item {
            Controls212.RadioButton {
                onClicked: {
                    if(styleData.value)
                        listModel.setProperty(styleData.row, "a", false)
                    else
                        listModel.setProperty(styleData.row, "a", true)
                    gv_intTotalRow = listModel.rowCount()
                    gv_intCurrentRow = styleData.row
                    tableView.selection.clear()
                    tableView.selection.select(gv_intCurrentRow)
//                    for (var i = 0; i < gv_intTotalRow; i++) {
//                        gv_listBName[i] = listModel.get(i).b
//                        gv_listCName[i] = listModel.get(i).c
//                    }
                    signal_radioButton_onClicked()
                }
                id: control
                //                text: qsTr("RadioButton")
                checked: styleData.value
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                visible: isACol( styleData.column )

                indicator: Rectangle {
                    implicitWidth: 20
                    implicitHeight: 20
                    x: control.leftPadding
                    y: parent.height / 2 - height / 2
                    radius: 13
                    border.color: control.down ? "#17a81a" : "#21be2b"

                    Rectangle {
                        width: parent.implicitWidth / 2
                        height: parent.implicitHeight / 2
                        x: control.leftPadding - 1
                        y: parent.height / 2 - height / 2
                        radius: parent.radius
                        color: styleData.value ? "#17a81a" : "#21be2b"
                        visible: styleData.value
                    }
                }

                //                contentItem: Text {
                //                    text: control.text
                //                    font: control.font
                //                    opacity: enabled ? 1.0 : 0.3
                //                    color: control.down ? "#17a81a" : "#21be2b"
                //                    verticalAlignment: Text.AlignVCenter
                //                    leftPadding: control.indicator.width + control.spacing
                //                }
            }

            Text {
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                text: styleData.value
                visible: isBCol( styleData.column )
            }

            Controls212.SwitchDelegate {
                onClicked: {
                    if(gv_boolSwitchDelegateStatus)
                        gv_boolSwitchDelegateStatus = false
                    else
                        gv_boolSwitchDelegateStatus = true
                }
                id: control_2
                width: 40
                height: 20
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
//                text: qsTr("SwitchDelegate")
                checked: gv_boolSwitchDelegateStatus
                visible: styleData.selected && isCCol( styleData.column )

//                contentItem: Text {
//                    rightPadding: control_2.indicator.width + control_2.spacing
//                    text: control_2.text
//                    font: control_2.font
//                    opacity: enabled ? 1.0 : 0.3
//                    color: control_2.down ? "#17a81a" : "#21be2b"
//                    elide: Text.ElideRight
//                    verticalAlignment: Text.AlignVCenter
//                }

                indicator: Rectangle {
                    implicitWidth: 40
                    implicitHeight: 20
                    x: control_2.width - width
                    y: parent.height / 2 - height / 2
                    radius: 13
                    color: gv_boolSwitchDelegateStatus ? "#17a81a" : "transparent"
                    border.color: gv_boolSwitchDelegateStatus ? "#17a81a" : "#cccccc"

                    Rectangle {
                        x: gv_boolSwitchDelegateStatus ? parent.width - width : 0
                        width: parent.implicitWidth / 2
                        height: parent.implicitHeight
                        radius: parent.radius
                        color: gv_boolSwitchDelegateStatus ? "#cccccc" : "#ffffff"
                        border.color: gv_boolSwitchDelegateStatus ? (gv_boolSwitchDelegateStatus ? "#17a81a" : "#21be2b") : "#999999"
                    }
                }

                background: Rectangle {
                    width: parent.implicitWidth
                    height: parent.implicitHeight
                    visible: gv_boolSwitchDelegateStatus
                    color: gv_boolSwitchDelegateStatus ? "transparent" : "#eeeeee"
                }
            }

            function isACol( columnIndex ){
                return tableView.getColumn(columnIndex) === radioButtonCol
            }

            function isBCol( columnIndex ){
                return tableView.getColumn(columnIndex) === textCol
            }

            function isCCol( columnIndex ){
                return tableView.getColumn(columnIndex) === switchCol
            }
        }
    }
}

 

你可能感兴趣的:(qt,qt,qml)