qml中使用图表功能ChartView时提示 ASSERT: No style available without QApplication

目录

  • 一、qml 饼图代码
  • 二、报错信息:
  • 三、解决方案
  • 四、刨根问底

一、qml 饼图代码

// ...
ChartView {
	title: "统计"
	anchors.fill: parent
	legend.alignment: Qt.AlignBottom
	antialiasing: true  // 抗锯齿
	
	PieSeries {
	    PieSlice { label: "水果"; value: 13 }
	    PieSlice { label: "蔬菜"; value: 10 }
	    PieSlice { label: "水"; value: 8 }
    }
}
// ...

二、报错信息:

kernel\qapplication.cpp: 1038 - ASSERT: “!“No style available without QApplication!”” in file kernel\qapplication.cpp, line 1038

三、解决方案

将main.cpp文件中app声明那里,将类型改为QApplication

在我的代码中是将QGuiApplication app(argc, argv);改为了QApplication app(argc, argv);

四、刨根问底

Note: Projects created with Qt Creator's Qt Quick Application wizard are based on the Qt Quick 2 template that uses QGuiApplication by default. All such QGuiApplication instances in the project must be replaced with QApplication as the module depends on Qt's Graphics View Framework for rendering.

无意间在qt官网发现了上面这一段话,看到

All such QGuiApplication instances in the project must be replaced with QApplication

于是果断去寻找并尝试解决方案中提及的方法,再次运行,确实成功了, 干的漂亮。

qml中使用图表功能ChartView时提示 ASSERT: No style available without QApplication_第1张图片

你可能感兴趣的:(经验详谈,Qml,qt,qt,qml,解决方案)