QML使用QtCharts 报错
2018-01-04 16:08
260 查看
新建一个 Qt Quick Application 应用,测试QtCharts模块报错:
运行报错:
ASSERT: “!”No style available without QApplication!”” in file kernel\qapplication.cpp, line 1060
ASSERT: “!”No style available without QApplication!”” in file kernel\qapplication.cpp, line 1060
查看官方文档有这样一句话:
Note: Since Qt Creator 3.0 the project created with Qt Quick Application wizard based on Qt Quick 2 template uses QGuiApplication by default. As Qt Charts utilizes Qt Graphics View Framework for drawing, QApplication must be used. The project created with the wizard is usable with Qt Charts after the QGuiApplication is replaced with QApplication.
意思大概就是要在qtquick中使用QtCharts模块的话必须使用QApplication代替QGuiApplication。
pro文件中添加:
修改main.cpp为:
运行正常。
import QtQuick 2.6 import QtQuick.Window 2.2 import QtCharts 2.2 Window { visible: true width: 640 height: 480 title: qsTr("Hello World") ChartView { width: 400 height: 300 theme: ChartView.ChartThemeBrownSand antialiasing: true PieSeries { id: pieSeries PieSlice { label: "eaten"; value: 94.9 } PieSlice { label: "not yet eaten"; value: 5.1 } } } }
运行报错:
ASSERT: “!”No style available without QApplication!”” in file kernel\qapplication.cpp, line 1060
ASSERT: “!”No style available without QApplication!”” in file kernel\qapplication.cpp, line 1060
查看官方文档有这样一句话:
Note: Since Qt Creator 3.0 the project created with Qt Quick Application wizard based on Qt Quick 2 template uses QGuiApplication by default. As Qt Charts utilizes Qt Graphics View Framework for drawing, QApplication must be used. The project created with the wizard is usable with Qt Charts after the QGuiApplication is replaced with QApplication.
意思大概就是要在qtquick中使用QtCharts模块的话必须使用QApplication代替QGuiApplication。
pro文件中添加:
QT += widgets
修改main.cpp为:
#include <QApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
运行正常。
相关文章推荐
- 关于QtCharts中的映射器与模型的使用
- 使用QT搭建点云显示框架系列五·基于QT的QML图像选点、动态绘制十字丝功能 ,以及纹理映射
- QT c++程序使用qml的两种方法
- 第二篇 QT程序员使用QML
- QT:使用C++还是QML
- qt中使用QML
- Qt:QtQuick:使用Qt静态库无法找到qml
- Qt QML 入门 — 使用C++定义QML类型
- QT程序员使用QML
- Qt实例分析——C++自定义类型的属性在QML的使用
- QT c++程序使用qml的两种方法
- QML笔记整理——在Qt/C++应用中使用QML
- Qt中使用Quick2动态加载QML组件的两种方式
- 使用Qt开发跨平台桌面应用时,应该选择widgets还是qml?
- 使用QML 布局Qt for Android
- QML如何结合Qt使用GridView
- Qt: 使用QML来创建界面
- Qt:使用C++还是QML
- Qt:在QML中使用Animator,让动画无卡顿
- Qt&QML使用过程中遇到的问题(还是声明篇)