简体   繁体   English

如何在 Qml 中实现下拉颜色选择器

[英]How to implement drop down color picker in Qml

I see that QML provides color dialogs out of which i can pick a color, on click of a button i can launch this color picker dialogs what if dont want the color picker dialog where on click of a button i want to show the same color picker dialog but as a drow down, something like this.我看到 QML 提供了颜色对话框,我可以从中选择颜色,单击按钮我可以启动此颜色选择器对话框如果不想要颜色选择器对话框,单击按钮时我想显示相同的颜色选择器对话框,但作为一个下拉,像这样的东西。

在此处输入图像描述

if i have to add the dialog in an overlay object?如果我必须在覆盖 object 中添加对话框? can you give me an example as to how to add it?你能给我一个如何添加它的例子吗?

i have tried this and it still opens as a seperate dialog我已经尝试过了,它仍然作为单独的对话框打开

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Dialogs 1.3
//import Qt.labs.platform 1.1
import QtQuick.Controls 2.15


Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")
    Rectangle{
        id: rect1
        height: 50
        width: 100
        color: "red"
        anchors.centerIn: parent

    }
    Button{
        id:b1
        width: 50
        height: 50
        anchors.left: rect1.right
        anchors.verticalCenter: parent.verticalCenter
        text: "V"
        onClicked: {
            console.log("launch color picker as an overlay")
            popup.open()
            colorPicker.visible = true
        }
        Popup{
            id: popup
            parent: Overlay.overlay
            x: Math.round((parent.width  - width) / 2) + 25
            y: Math.round((parent.height - height) / 2) + 50
            height: 200
            width: 150
            ColorDialog{
                id:colorPicker
            }
        }
    }

}

Overlay is for popups: 覆盖用于弹出窗口:

A window overlay for popups.用于弹出窗口的 window 覆盖。
Overlay provides a layer for popups, ensuring that popups are displayed above other content... Overlay 为弹出窗口提供了一个层,确保弹出窗口显示在其他内容之上...

As @JarMan rightly pointed out, if you use ColorDialog, it will always be shown as a separate dialog.正如@JarMan 正确指出的那样,如果您使用 ColorDialog,它将始终显示为单独的对话框。

If you want the color picker to be shown in the same window as a drop down, then you have to implement the color picker component and control its visibility based on user actions.如果您希望颜色选择器显示在同一个 window 中作为下拉菜单,那么您必须实现颜色选择器组件并根据用户操作控制其可见性。
You can implement your own version of color picker component or you could reuse any existing component .您可以实现自己版本的颜色选择器组件,也可以重用任何现有组件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM