Hello,
I am trying to learn QML and Qt i am trying to run one example but getting the below Error
ASSERT: "!"No style available without QApplication!"" in file kernel\qapplication.cpp, line 1123
My example code are below
piechart.h
#include <QtQuick/QQuickPaintedItem>
#include <QColor>
class piechart : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName
) Q_PROPERTY(QColor color READ color WRITE setColor
) public:
piechart(QQuickItem *parent = 0);
};
#include <QtQuick/QQuickPaintedItem>
#include <QColor>
class piechart : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QColor color READ color WRITE setColor)
public:
piechart(QQuickItem *parent = 0);
void paint(QPainter *painter);
};
To copy to clipboard, switch view to plain text mode
piechart.cpp
#include "piechart.h"
piechart::piechart(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
}
{
painter->setPen(pen);
painter
->setRenderHints
(QPainter::Antialiasing,
true);
painter->drawPie(boundingRect().adjusted(1, 1, -1, -1), 90 * 16, 290 * 16);
}
#include "piechart.h"
piechart::piechart(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
}
void piechart::paint(QPainter *painter)
{
QPen pen(m_color, 2);
painter->setPen(pen);
painter->setRenderHints(QPainter::Antialiasing, true);
painter->drawPie(boundingRect().adjusted(1, 1, -1, -1), 90 * 16, 290 * 16);
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQuick/QQuickView>
#include "piechart.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.
load(QUrl(QStringLiteral
("qrc:/main.qml")));
return app.exec();
}
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQuick/QQuickView>
#include "piechart.h"
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec();
}
To copy to clipboard, switch view to plain text mode
main.qml
import QtQuick 2.5
import QtQuick.Window 2.2
import QtCharts 2.0
Item {
anchors.fill: parent
property variant othersSlice: 0
ChartView {
id: chart
title: "Top-5 car brand shares in Finland"
anchors.fill: parent
legend.alignment: Qt.AlignBottom
antialiasing: true
PieSeries {
id: pieSeries
PieSlice { label: "Volkswagen"; value: 13.5 }
PieSlice { label: "Toyota"; value: 10.9 }
PieSlice { label: "Ford"; value: 8.6 }
PieSlice { label: "Skoda"; value: 8.2 }
PieSlice { label: "Volvo"; value: 6.8 }
}
}
Component.onCompleted: {
// You can also manipulate slices dynamically
othersSlice = pieSeries.append("Others", 52.0);
pieSeries.find("Volkswagen").exploded = true;
}
}
import QtQuick 2.5
import QtQuick.Window 2.2
import QtCharts 2.0
Item {
anchors.fill: parent
property variant othersSlice: 0
ChartView {
id: chart
title: "Top-5 car brand shares in Finland"
anchors.fill: parent
legend.alignment: Qt.AlignBottom
antialiasing: true
PieSeries {
id: pieSeries
PieSlice { label: "Volkswagen"; value: 13.5 }
PieSlice { label: "Toyota"; value: 10.9 }
PieSlice { label: "Ford"; value: 8.6 }
PieSlice { label: "Skoda"; value: 8.2 }
PieSlice { label: "Volvo"; value: 6.8 }
}
}
Component.onCompleted: {
// You can also manipulate slices dynamically
othersSlice = pieSeries.append("Others", 52.0);
pieSeries.find("Volkswagen").exploded = true;
}
}
To copy to clipboard, switch view to plain text mode
qmlApp.pro
TEMPLATE = app
QT += core gui
QT += qml quick
CONFIG += c++11
SOURCES += main.cpp \
piechart.cpp
RESOURCES += qml.qrc
# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
piechart.h
Can anyone let me know why i am getting the Assert Error.
Bookmarks