1

我正在尝试使用 QtCharts 2.1 更改条形图的外观。目前,我将所需的颜色和样式应用于单个 QBarSet,如下所示:

import QtQuick 2.8
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.0
import QtCharts 2.1

import QtQuick.Window 2.2

ChartView {
    id: root
    width: 600
    height: 400
    antialiasing: true
    backgroundColor: "transparent"
    legend.visible: false

    property var histogramProvider

    Connections {
        target: histogramProvider
        onHistogramChanged: {
            console.log("histogram changed")
            histogramProvider.updateSeries(histogramSeries)
            console.log(histogramSeries)
            console.log(histogramSeries.at(0))
            histogramSeries.at(0).color = Qt.rgba(0.9, 0.2, 0.1, 0.5)
        }
        onAxesChanged: {
            xAxis.min = xMin
            xAxis.max = xMax
            yAxis.min = yMin
            yAxis.max = yMax
        }
    }

    ValueAxis {
            id: yAxis
            min: 0
            max: 100
            tickCount: 3
            gridVisible: false
            labelsColor: Style.fontColorLight
            labelsFont: Qt.font({pointSize: 8})
            titleText: qsTr("Count")
            labelFormat: "%.1f"
    }

    ValueAxis {
        id: xAxis
        tickCount: 3
        gridVisible: false
        labelsColor: "white"
        labelFormat: "%i"
        min: 0
        max: 100
        titleText: qsTr("Elongation")
    }

    StackedBarSeries {
        id: histogramSeries
        axisX: xAxis
        axisY: yAxis
    }
}

我可以在图表中看到带有默认颜色的 BarSet,但是我无法更改颜色。该代码产生以下控制台输出onHistogramChanged

qml: QtCharts::DeclarativeStackedBarSeries(0x1df550cd830)
qml: null
qrc:/qml/Histogram.qml:27: TypeError: Type error

我像这样从 C++ 更新 BarSeries:

void HistogramProvider::updateSeries(QStackedBarSeries *barSeries)
{
    [...]
    barSeries->clear();
    QBarSet* set = new QBarSet("Histogram", barSeries);
    set->setBorderColor(QColor("transparent"));
    set->setColor(QColor("#6f74dd"));
    for(int i = 0; i < bins.size(); i++) {
        set->append(bins.at(i));
    }
    qDebug() << "blubb";
    barSeries->append(set);
}

我在这里做错了什么?

4

0 回答 0