QtCharts:给QChartView添加背景图片

因为平时很少用QCharts,所以对一些样式自定义的设置不熟悉。本来想添加一个背景图片,结果发现只有setBackgroundBrush这种接口。百度一通之后,只找到个隐藏QChart背景的博客(https://blog.csdn.net/qq_40182127/article/details/82113267)。受到启发,自己一番折腾,总算把背景给设置出来了,原来也挺简单的。

我是参照示例的BarChart来修改的,

首先,给QChart类去掉背景渲染:

chart->setBackgroundVisible(false);

然后,给QChartView加上背景图的样式表就能显示图片了。

setStyleSheet(R"(QGraphicsView{ background-image:url(:/backimg.png);})");

下面是设置前后对比图(完整代码及工程文件链接在本文末尾):

(其实默认样式一般也足够了,不过有时候Ui会有特殊需求) 

这里有两个小插曲,一是我把图片url用绝对路径测试了下之后再改回来,居然设置无效(重新构建也不行),把生成的文件删了之后重新生成才正常了;第二个就是,我给QChartView设置样式表,如果填 “QChartView{}” 的话不生效,填他的父类 “QGraphicsView{}” 才有效了。

下面把代码和工程github链接贴出来:

github:https://github.com/gongjianbo/MyTestCode/tree/master/Qt/QCharts

(est20190806_QCharts_Qt5_9_8_MSVC文件夹)

code:

(参照示例的BarChart来修改)

//main.cpp
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtCharts/QChartView>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLegend>
#include <QtCharts/QBarCategoryAxis>

QT_CHARTS_USE_NAMESPACE

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

//![1]
    QBarSet *set0 = new QBarSet("Jane");
    QBarSet *set1 = new QBarSet("John");
    QBarSet *set2 = new QBarSet("Axel");
    QBarSet *set3 = new QBarSet("Mary");
    QBarSet *set4 = new QBarSet("Samantha");

    *set0 << 1 << 2 << 3 << 4 << 5 << 6;
    *set1 << 5 << 0 << 0 << 4 << 0 << 7;
    *set2 << 3 << 5 << 8 << 13 << 8 << 5;
    *set3 << 5 << 6 << 7 << 3 << 4 << 5;
    *set4 << 9 << 7 << 5 << 3 << 1 << 2;
//![1]

//![2]
    QBarSeries *series = new QBarSeries();
    series->append(set0);
    series->append(set1);
    series->append(set2);
    series->append(set3);
    series->append(set4);

//![2]

//![3]
    QChart *chart = new QChart();
    chart->addSeries(series);
    chart->setTheme(QChart::ChartThemeQt);
    chart->setTitle("Simple barchart example");
    chart->setAnimationOptions(QChart::SeriesAnimations);
    chart->setBackgroundVisible(false);
    //chart->setBackgroundBrush(QBrush("darkCyan"));
//![3]

//![4]
    QStringList categories;
    categories << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun";
    QBarCategoryAxis *axis = new QBarCategoryAxis();
    axis->append(categories);
    chart->createDefaultAxes();
    chart->setAxisX(axis, series);
//![4]

//![5]
    chart->legend()->setVisible(true);
    chart->legend()->setAlignment(Qt::AlignBottom);
//![5]

//![6]
    QChartView *chartView = new QChartView(chart);
    chartView->setRenderHint(QPainter::Antialiasing);
    //chartView->setContentsMargins(0,10,0,10);
    //chartView->setBackgroundBrush(QBrush(Qt::yellow));
    //chartView->setStyleSheet("");
    //chartView->setBackgroundBrush(Qt::transparent);

//![6]

//![7]
    QMainWindow window;
    window.setCentralWidget(chartView);
    window.setContentsMargins(10,10,10,10);
    window.setStyleSheet(R"(QMainWindow{ background-color:green;}
                         QMainWindow QWidget{ background-color:cyan;}
                         QGraphicsView{ background-image:url(:/backimg.png);})");
    window.resize(420, 300);
    window.show();
//![7]

    return a.exec();
}

 

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页