PDA

View Full Version : QPolarChart



buster
14th January 2020, 16:48
I am attempting to use QPolarChart, so I use:


#include <QtCharts/QPolarChart>

However, I get an error: QtCharts/QPolarChart: no such file or directorty
I tried to install the library, thus:
sudo apt-get install -y libqt5charts5
but I still get the same error. I am using Kubuntu 18.04, Kdevelop 5.2.1 and Cmake. I am not using qmake, so its version number (4.8.7) is probably not relevant? Anything else I need to install?

d_stranz
14th January 2020, 20:24
I am guessing that when you install libqt5charts5 you are getting only the runtime libraries.

You probably also need to install the development files: libqt5charts5-dev

buster
15th January 2020, 16:56
Installing libqt5charts5-dev helped, in the sense that I can now decleare


QPolarChart *chart;

But


QPolarChart *chart = new QPolarChart();

results in what is probably a linking error:
'... undefined reference to `QtCharts::QPolarChart::QPolarChart(QGraphicsItem* , QFlags<Qt::WindowType>)'
Do I need to modify my CMakeLists.txt file?

d_stranz
15th January 2020, 17:31
Do I need to modify my CMakeLists.txt file?

Yes. You probably need to add the name of the charts library to the target_link_libraries() directive. I don't know what that name is on linux - libqt5charts5 maybe?

buster
16th January 2020, 05:36
Modifying the line

target_link_libraries(lydkontroll Qt5::Widgets)

to

find_package(Qt5Charts)
target_link_libraries(lydkontroll Qt5::Widgets Qt5::Charts)

got rid of the linking error message. I also had to inlude

using namespace QtCharts;

in the .h file. Then it compiles, links and runs OK. Although it works, I am not sure how "using namespace QtCharts" is related to QtCharts. Is it needed because QtCharts is a separate module?

buster
16th January 2020, 17:10
I might have celebrated a bit prematurely, as the following code (adapted from the example in the QPolarChart Class doc.);


m_angularAxis = new QValueAxis();
m_angularAxis->setTickCount(9);
m_angularAxis->setLabelFormat("%.1f");
m_angularAxis->setShadesVisible(true);
m_angularAxis->setShadesBrush(QBrush(QColor(249, 249, 255)));
m_chart->addAxis(m_angularAxis, QPolarChart::PolarOrientationAngular);

craches the application. Looks like a segmentation error, although the error message just states that the application has crashed. The strange thing is that if I change the last line to


m_chart->addAxis(m_radialAxis, QPolarChart::PolarOrientationRadial);

there is no crash. So it seems to be related to PolarOrientationAngular. Running it in the debugger causes the debugger to crash, so I must be doing something really daft. I suspect it could be related to my CMakeListe.txt file,



find_package(Qt5Charts)
target_link_libraries(myApp Qt5::Widgets Qt5::Charts)

but I cannot see what it might me. Am I missing something?

d_stranz
16th January 2020, 21:05
If it runs in the debugger, then use the debugger to find where the crash occurs by looking at the call stack at the point of the crash. We aren't sitting there looking over your shoulder, so how are we to know what else you are doing that could cause a problem?

If your CMake script builds an executable, then the problem is not in the CMake script. It's in your code (or your run time environment). Be sure that the Qt installation that find_package() is finding (and building against) is the same one that your run time is calling on to load shared libraries. Use the "ldd" command to find which libraries have been linked to and compare those with your path.


using namespace QtCharts

All of the classes in the QtCharts library are in the QtCharts namespace. If you don't want to have to qualify every class with the namespace, you add the "using" directive. Otherwise, every time you declare a QPolarChart instance, you have to declare it as QtCharts::QPolarChart and so forth.

buster
18th January 2020, 15:41
The problem is solved. I had just committed a stupid mistake. Thanks for the help.

d_stranz
18th January 2020, 16:04
I had just committed a stupid mistake.

Wow, I've never done that. :p