import QtQuick import QtQuick.Controls import QtQuick.Layouts RowLayout { id: reusecombobox anchors.fill: parent spacing: 10 property string strTitle: "" property string strValue: "" property var objInfos property var objChild ListModel { id: reusecombobox_listmodel } Rectangle { width: parent.width * 0.225 height: parent.height color: "#00ffffff" Text { id: reusecombobox_text_title anchors.fill: parent font.pixelSize: 25 color: "#ffffff" horizontalAlignment: Text.AlignRight verticalAlignment: Text.AlignVCenter } } Rectangle { x: parent.width * 0.225 width: parent.width * 0.675 height: parent.height * 0.725 color: "#ffffff" radius: 5 ComboBox { id: reusecombobox_text_value anchors.fill: parent focusPolicy: Qt.NoFocus anchors.margins: 2 font.pointSize: 15 focus: false background: Rectangle { color: "transparent" } font.pixelSize: 20 padding:5 font.styleName: "Regular" model: reusecombobox_listmodel; // 当前选中项改变时触发 onCurrentIndexChanged: { objChild = objInfos[textAt(currentIndex)]; strValue = textAt(currentIndex); } } } Connections { target: reusecombobox onStrTitleChanged: { reusecombobox_text_title.text = strTitle; } } Component.onCompleted: { reusecombobox_text_title.text = strTitle; } function setModel(value) { objInfos = {}; var types = []; for (var i = 0; i < value.length; ++i) { objInfos[value[i].display] = value[i]; types.push(value[i].display); } reusecombobox_text_value.model = types; } function setValue(value) { for (var i = 0; i < reusecombobox_text_value.model.length; ++i) { if (reusecombobox_text_value.model[i] === value) { reusecombobox_text_value.currentIndex = i; } } strValue = value; objChild = objInfos[reusecombobox_text_value.textAt(reusecombobox_text_value.currentIndex)]; } }