qml package 的使用

什么时候使用这个。就是多个view使用同一个deleagte的时候。

The Package class is used in conjunction with VisualDataModel to enable delegates with a shared context to be provided to multiple views.

Any item within a Package may be assigned a name via the Package.name attached property.

The example below creates a Package containing two named items; list and grid. The third item in the package (the Rectangle) is parented to whichever delegate it should appear in. This allows an item to move between views.

Package {
Text { id: listDelegate; width: parent.width; height: 25; text: 'Empty'; Package.name: 'list' }
Text { id: gridDelegate; width: parent.width / 2; height: 50; text: 'Empty'; Package.name: 'grid' }

Rectangle {
    id: wrapper
    width: parent.width; height: 25
    color: 'lightsteelblue'

    Text { text: display; anchors.centerIn: parent }
    state: root.upTo > index ? 'inGrid' : 'inList'
    states: [
        State {
            name: 'inList'
            ParentChange { target: wrapper; parent: listDelegate }
        },
        State {
            name: 'inGrid'
            ParentChange {
                target: wrapper; parent: gridDelegate
                x: 0; y: 0; width: gridDelegate.width; height: gridDelegate.height
            }
        }
    ]

    transitions: [
        Transition {
            ParentAnimation {
                NumberAnimation { properties: 'x,y,width,height'; duration: 300 }
            }
        }
    ]
}

}
These named items are used as the delegates by the two views who reference the special VisualDataModel::parts property to select a model which provides the chosen delegate.

DelegateModel {
id: visualModel
delegate: Delegate {}
model: myModel
}

ListView {
id: lv
height: parent.height/2
width: parent.width

model: visualModel.parts.list

}
GridView {
y: parent.height/2
height: parent.height/2
width: parent.width
cellWidth: width / 2
cellHeight: 50
model: visualModel.parts.grid
}

转载于:https://www.cnblogs.com/xianqingzh/p/4497899.html

QML使用 QSettings 可以方便地实现应用程序的配置管理。QSettings 是一个用于访问应用程序设置的类,它可以将设置存储在操作系统的注册表中、INI 文件中或者其他位置。 在 CMake 工程中可以使用以下步骤添加 QSettings 功能: 1. 在 CMakeLists.txt 中添加以下内容: ```cmake find_package(Qt5 COMPONENTS Core REQUIRED) # 将 qml.qrc 添加到工程中 qt5_add_resources(QML_RESOURCES qml.qrc) # 将 QML_RESOURCES 添加到可执行文件中 add_executable(MyApp main.cpp ${QML_RESOURCES}) # 将 QML 文件复制到二进制目录中 file(COPY qml DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) ``` 2. 创建一个名为 Settings 的 QML 组件,用于管理应用程序设置。示例代码如下: ```qml import QtQuick 2.0 import QtQuick.LocalStorage 2.0 Item { property alias username: usernameInput.text property alias password: passwordInput.text TextInput { id: usernameInput placeholderText: qsTr("Username") text: Settings.value("username", "") onTextChanged: Settings.setValue("username", text) } TextInput { id: passwordInput placeholderText: qsTr("Password") text: Settings.value("password", "") onTextChanged: Settings.setValue("password", text) echoMode: TextInput.Password } // 设置存储位置为应用程序数据目录下的 settings.ini 文件 Component.onCompleted: Settings.storageLocation = Qt.application.dataDir + "/settings.ini" // 创建一个 QSettings 实例 property var Settings: Qt.createQmlObject("import Qt.labs.settings 1.0; Settings {}", parent) } ``` 在上述代码中,我们通过 TextInput 控件实现了用户名和密码的输入,并将它们的值保存在 QSettings 中。在 Component.onCompleted 信号中,我们将存储位置设置为应用程序数据目录下的 settings.ini 文件,这样可以确保用户的设置在不同的系统上都能够被正确地保存和读取。 3. 在 main.qml 中添加 Settings 组件并使用它来读取和写入应用程序设置。示例代码如下: ```qml import QtQuick 2.0 // 在 main.qml 中添加 Settings 组件 Settings { id: appSettings } Rectangle { width: 200 height: 200 Text { text: qsTr("Welcome, %1").arg(appSettings.username) anchors.centerIn: parent } } ``` 在上述代码中,我们通过 appSettings.username 来读取用户名的值,并在 Text 控件中显示它。这样,当用户在 Settings 组件中输入用户名后,它将自动更新到 Text 控件中。 这就是使用 QSettings 和 CMake 在 QML 中管理应用程序设置的基本步骤。当然,这只是一个简单的示例,实际应用中还需要根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值