Solved I can't use QtChart! please help me to correct my code or write simple program with QtChart
-
I'm trying for a week and still I'm not able to run this simple program...
Whats the problem with my codes?
It gives me this error.pro
TEMPLATE = app QT += qml quick QT += widgets CONFIG += c++11 SOURCES += main.cpp RESOURCES += qml.qrc # Additional import path used to resolve QML modules in Qt Creator's code model QML_IMPORT_PATH = # Additional import path used to resolve QML modules just for Qt Quick Designer QML_DESIGNER_IMPORT_PATH = # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
.cpp
#include <QApplication> #include <QQmlApplicationEngine> int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); if (engine.rootObjects().isEmpty()) return -1; return app.exec(); }
.qlm
import QtQuick 2.6 import QtQuick.Window 2.2 import QtCharts 2.2 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 } } }
-
Hi! I'm not sure / can't test it right now; maybe you need to add
QT += charts
to the project file. -
@PooyaSis This is hidden in the "Qt Charts QML Types" documentation:
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.
It seems to require QT += widgets in the project file.
-
@Eeli-K
I have addedQT += charts
. But it's not working. -
@PooyaSis no need for charts, your current project file should be enough because it has the qml related modules and widgets. But you need to use QApplication instead of QGuiApplication in main.cpp as the documentation says.
-
@PooyaSis (You told in private message it still didn't work, I answer here because it may help others with the same kind of problem.) Do you have Qt Charts installed? In the Maintenance Tool it must be selected and installed, it's not installed by default. Otherwise please tell if it's still the same error message or something else.
-
@Eeli-K yes, I've installed Qt Charts.
now I can run the program without any error, but it's not showing anything!! -
@PooyaSis Try https://doc.qt.io/qt-5/qtcharts-qmlchart-example.html (available in Qt Creator examples section). Does it work? If it does, the problem may be in your own project. If not, the problem may be in your system or Qt installation. If the example doesn't work, do other QML Quick examples work?
-
@Eeli-K hi!
examples are working just fine!!!
but I don't know whats the matter with my Qt_program... -
use QApplication instead of QGuiApplication
-
@Eeli-K Thanks, it works for me :)