首页 > 解决方案 > 自定义 Controls 2 SpinBox 时出错

问题描述

自定义 QtQuickControls2 SpinBox 时,我在关闭应用程序时在控制台中收到此错误:

“在引擎销毁时,仍有“2”项正在创建过程中。”

该数字根据窗口中的 Spinbox 数量而有所不同,每个自定义的指标都会上升(每个自定义 SpinBox 两次:一个用于向上指示器,一个用于向下指示器)。我尝试注释掉我的自定义代码的每一部分,以及使用此处提供的示例代码,所以我很肯定这就是错误的来源。

有谁知道如何摆脱这个错误?

主窗口代码:

import QtQuick 2.7
import QtQuick.Controls 2.3

ApplicationWindow{
    width: 1600
    height: 900
    visible: true
    SpinBox_custom{

    }
}

自定义 SpinBox_custom 代码:

import QtQuick 2.11
import QtQuick.Controls 2.4

SpinBox {
    id: control
    value: 50
    editable: true

    contentItem: TextInput {
        anchors.fill: parent
        anchors.rightMargin : up.indicator.width
        anchors.leftMargin : down.indicator.width
        z: 2
        text: control.textFromValue(control.value, control.locale)

        font.pointSize: Style.textPointSize-2
        color: '#7e8d9e'
        selectionColor: '#7e8d9e'
        selectedTextColor: "white"
        horizontalAlignment: Qt.AlignHCenter
        verticalAlignment: Qt.AlignVCenter

        readOnly: !control.editable
        validator: control.validator
        inputMethodHints: Qt.ImhFormattedNumbersOnly
    }

    up.indicator: Rectangle {
        x: control.mirrored ? 0 : parent.width - width
        height: parent.height
        implicitWidth: 20
        implicitHeight: 30
        color: control.up.pressed ? '#dee2e6' : '#bec6ce'
        border.color: enabled ? '#bec6ce' : '#dee2e6'

        Text {
            text: "+"
            font.pixelSize: control.font.pixelSize * 2
            color: '#428AC9'
            anchors.fill: parent
            fontSizeMode: Text.Fit
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
    }

    down.indicator: Rectangle {
        x: control.mirrored ? parent.width - width : 0
        height: parent.height
        implicitWidth: 20
        implicitHeight: 30
        color: control.down.pressed ? '#dee2e6' : '#bec6ce'
        border.color: enabled ? '#bec6ce' : '#dee2e6'

        Text {
            text: "-"
            font.pixelSize: control.font.pixelSize * 2
            color: '#428AC9'
            anchors.fill: parent
            fontSizeMode: Text.Fit
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
        }
    }

    background: Rectangle {
        implicitWidth: 90
    }
}

标签: qmlqtquickcontrols2

解决方案


推荐阅读