29

I want to add QChart to the form. But I can't find it in the Widget Box. So I created it in the code. How can I insert it in QWidget or QFrame or something else?

I want to set area of that widget in QtDesigner.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Ufx
  • 2,595
  • 12
  • 44
  • 83

2 Answers2

57

Option 1: Promoted

I suppose you mean inserting a QChartView, because QChartView inherits from QGraphicsView, this would be a good option, for this we do the following:

  1. first add QT += charts in the .pro
  2. place the QGraphicsView to the design.
  3. Right click on the QGraphicsView and select Promote to...
  4. When doing the above, a menu appears, in the menu it should be set in QChartView in Promoted Class Name, and QtCharts in Header file, then press the add button and finally press promote.

Screenshots of some steps:

[3.]

enter image description here

[4.1]

enter image description here

[4.2]

enter image description here

The same could be done using QWidget as a base instead of QGraphicsView.

Why is one or another widget chosen as a base?

It is chosen because Qt Designer through moc establishes certain properties by default, and if the widget does not have that method then it will not compile. as all widgets inherit from QWidget this would be the basis for any new widget to promote it in Qt Designer.

In the following link you will find an example.


Option 2: QtChart plugin

Another option would be to compile the QtChart plugin for QtDesigner, for it you must download the 5 files from the following link:

enter image description here

Then you execute the following:

qmake
make
sudo make install

At the end you can access QtCharts::QChartView in Qt Designer

enter image description here

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • 6
    Don't forget to call `chartView.setChart(chart)` in your code. – lenooh May 14 '18 at 20:08
  • 2
    Great answer. If using `CMakeList.txt` instead of `qmake` (.pro file), you need to add `Charts` as a Qt5 COMPONENT in the `find_package` (for example: `find_package(Qt5 COMPONENTS Widgets Charts REQUIRED`) and Qt5::Charts in your `target_link_libraries` (for example: `target_link_libraries(${PROJECT_NAME} PRIVATE Qt5::Widgets Qt5::Charts`) – Simog Apr 09 '20 at 15:35
  • where to find .pro? – HussainBiedouh Feb 19 '21 at 09:16
  • I'm on macos, I tried the option 2 (qt is installe via brew) and the plugin gets installed in `/usr/local/share/qt/plugins/designer/libqtchartsdesigner.dylib` I tried to copy the shared lib insto the .app : `cp /usr/local/share/qt/plugins/designer/libqtchartsdesigner.dylib /Applications/Qt\ Creator.app/Contents/PlugIns/designer/` but I cannot see in the designer part of the Creator. Any clues? – bibi Nov 04 '22 at 09:03
0

Let me add something here for the answer below (thanks for that!)
I had some additional steps for using

QTcreator 4.11.0

on

Ubuntu 20.04.3 LTS

First, you need to install not only package

qml-module-qtcharts

But also package

libqt5charts5-dev

Next, for "Option 1: Promoted" below you 1st have to use Button "Add" after entering

Promoted class name: QChartView

and

Header file: QtCharts

Then, you can select it from the "Promoted Classes"

Finally, QTCreator adds to your ui_mainwindow.h (e.g.)

#include <qchartview.h>

and uses

QChartView *graphicsView;

But it does not consider, that type QChartView is defined in "namespace QtCharts" in <qchartview.h>. Therefore, type QChartView is not found.

I fixed (hacked) this by adding a "Central.h" to #include for users as a 1st #include file, with content

#ifndef CENTRAL_H
#define CENTRAL_H

#include <QtCharts>
using namespace QtCharts;

#endif // CENTRAL_H
Frank Bergemann
  • 325
  • 2
  • 14