自定义控件 2 旋转框时出错



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

"在发动机销毁过程中仍有''2'"项正在创建中。"

该数字根据窗口中的旋转框数量而变化,对于每个自定义的指标,它都会上升(每个自定义的旋转框两次:一个用于向上指标,一个用于向下指标)。我尝试注释掉自定义代码的每个部分,以及使用此处提供的示例代码,因此我确信这就是错误的来源。

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

主窗口代码:

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
    }
}

我刚刚遇到了同样的问题,经过一些实验似乎发现它与将文字值分配给SpinBoxvalue属性有关。

本报告中也讨论了类似的事情,这些是用于其中提到的SpinBox时的其他麻烦:

  • SpinBox内部元素之间的引用(contentItembackground[up,down].indicator
  • 对内部元素中parent的引用
  • id分配给内部元素

在摆脱所有这些并将value: 0更改为value: from之后,它会自动停止用原始帖子中的消息警告我。

仅作为参考,这是在没有警告的情况下对我有用的片段:

SpinBox
{
    id: control
    value: from
    from: 0
    to: 600
    editable: true
    enabled: true
    leftPadding: topPadding
    rightPadding: height
    contentItem: Item
    {
        anchors.left: control.left
        anchors.top: control.top
        anchors.bottom: control.bottom
        implicitWidth: input.implicitWidth
        implicitHeight: input.implicitHeight
        width: control.width - control.height
        height: control.height
        TextInput
        {
            id: input
            anchors.fill: parent
            text: control.textFromValue(control.value, control.locale)
            color: "black"
            horizontalAlignment: Qt.AlignHCenter
            verticalAlignment: Qt.AlignVCenter
            readOnly: !control.editable
            validator: control.validator
            inputMethodHints: Qt.ImhFormattedNumbersOnly
        }
    }
    up.indicator: Rectangle
    {
        anchors.top: control.top
        anchors.right: control.right
        anchors.topMargin: 1
        anchors.rightMargin: 1
        height: control.height / 2
        width: control.height
        color: "gold"
    }
    down.indicator: Rectangle
    {
        anchors.bottom: control.bottom
        anchors.right: control.right
        anchors.bottomMargin: 1
        anchors.rightMargin: 1
        height: control.height / 2
        width: control.height
        color: "orange"
    }
    background: Rectangle
    {
        implicitWidth: 100
        border.color: "red"
        border.width: 1
        radius: 2
    }
}

最新更新