简体   繁体   English

如何在Qt中创建或实现这样的图表?

[英]how can i create or achieve a chart like this in Qt?

在此处输入图片说明

I want to create a chart like this in qt. 我想在qt中创建这样的图表。 I already searched and can not find a way to do it. 我已经搜索过,找不到解决方法。

I also can not find a way to customize Barchart and look like this in Widget Based Applications 我也找不到自定义Barchart的方法,并且在基于Widget的应用程序中看起来像这样

Easy in QML! 轻松使用QML!

import QtQuick 2.0
import QtQuick.Layouts 1.1

Rectangle
{
    width: 600
    height: 300

    ListModel
    {
        id: dataModel
        ListElement { label: "C.A"; value: 37 }
        ListElement { label: "C.B"; value: 58 }
        ListElement { label: "C.C"; value: 16 }
        ListElement { label: "C.D"; value: 5 }
        ListElement { label: "C.E"; value: 95 }
        ListElement { label: "C.F"; value: 10 }
        ListElement { label: "C.G"; value: 27 }
        ListElement { label: "C.H"; value: 2 }
    }

    Rectangle
    {
        height: 4
        width: layout.width
        anchors.top: layout.bottom
        anchors.horizontalCenter: layout.horizontalCenter
        color: "#bbbdbe"
    }
    RowLayout
    {
        id: layout
        width: 400
        height: 200
        spacing: 0
        anchors.centerIn: parent

        Repeater
        {
            id: rpt
            property int barWidth: layout.width / count
            model: dataModel
            delegate:
                Rectangle
                {
                    width: rpt.barWidth
                    height: layout.height
                    color: "transparent"

                    Rectangle
                    {
                        anchors.bottom: parent.bottom
                        anchors.horizontalCenter: parent.horizontalCenter
                        width: 3
                        height: (parent.height * value) / 100
                        color: "#448bbe"

                        Rectangle
                        {
                            color: "#448bbe"
                            radius: width / 2
                            width: 8
                            height: 8
                            anchors.top: parent.top
                            anchors.horizontalCenter: parent.horizontalCenter
                        }
                    }
                    Text
                    {
                        y: parent.height + 3
                        anchors.horizontalCenter: parent.horizontalCenter
                        text: label
                    }
                }
        }
    }
}

Screenshot 屏幕截图

在此处输入图片说明

Since the OP didn't specify he wants a solution for a Qt widgets based application [he did after editing the question], the answer is: 由于OP没有指定他想要基于Qt小部件的应用程序的解决方案(他在编辑问题后就这样做了),答案是:

Create your own QWidget class. 创建自己的QWidget类。 Override the paintEvent and paint in it with a QPainter. 重写paintEvent并使用QPainter在其中绘画。 I think there's plenty of examples if you google it. 我认为,如果您使用Google搜索,则有很多示例。

Like this: http://doc.qt.io/qt-5/qtwidgets-painting-basicdrawing-example.html 像这样: http : //doc.qt.io/qt-5/qtwidgets-painting-basicdrawing-example.html

Or this: http://programmingexamples.wikidot.com/qt-qpainter-example 或这样: http : //programmingexamples.wikidot.com/qt-qpainter-example

Or this: Draw on QWidget 或者这样: 在QWidget上绘图

你可以去定制QQuickPaintedItem在这里你可以找到一个例子

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

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