qtqt-designerqchartview

How to install QChartView in Qt Designer?


My problem is identical with that from this topic: How to insert QChartView in form with Qt Designer? however I still cannot solve it. I tried to download the plugin, but after typing qmake in the terminal, I get the following error:

Unknown test function: qtHaveModule

On the other hand, I can't use the first solution either. Where can I find the .pro file in Qt Designer (not qt creator) ?

Thank you in advance.


Solution

  • That problems usually comes because you are configuring your project with a older version of qmake, frequenctly for Qt4.

    QtChart is available only in modern versions of Qt. If you are using QtCreator those are the steps to update your build system:

    Go to:

    Tools -> Options...
    

    After that, go to:

    Kits -> Qt Versions
    

    Normally you should see the different versions of Qt that are installed. Check that you can see a modern version of Qt, QtCharts 2.0 was released with Qt 5.6.0, so that's your minimum version.

    Now, updates your kits. If you are compiling a desktop app, check that your default Kit or the one you are using is using the proper Qt Version. You will find a combo-box where you can select the proper one.

    After that, update and run again qmake.

    Regarding your second problem, you don't edit your pro file in QtDesigner, you do it in QtCreator. Basically, open your project and add the Qt dependency:

    QT += charts
    

    Now, open your mainwindow.ui file or the file in which you want to insert the chart-view and follow the steps of the post you linked.

    Example:

    This is a classic .pro configuration:

    QT += charts 
    SOURCES += \ main.cpp
    

    In your main file, you should always add QT_CHARTS_USE_NAMESPACE on top. For instance, to display a line chart, you can do something like this:

    #include <QtWidgets/QApplication>
    #include <QtWidgets/QMainWindow>
    #include <QtCharts/QChartView>
    #include <QtCharts/QLineSeries>
    
    QT_CHARTS_USE_NAMESPACE
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        // Create your time series
        QLineSeries *series = new QLineSeries();
        series->append(0, 6);
        series->append(2, 4);
        series->append(3, 8);
        series->append(7, 4);
        series->append(10, 5);
        *series << QPointF(11, 1) << QPointF(13, 3) << QPointF(17, 6) << QPointF(18, 3) <<   QPointF(20, 2);
    
        // Configure your chart
        QChart *chart = new QChart();
        chart->legend()->hide();
        chart->addSeries(series);
        chart->createDefaultAxes();
        chart->setTitle("Simple line chart example");
    
        // Create your chart view
        QChartView *chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
    
        // Display into a main window
        QMainWindow window;
        window.setCentralWidget(chartView);
        window.resize(400, 300);
        window.show();
    
        return a.exec();
    }
    

    Source: Qt Example.

    You just need to replace the final part for your special case:

    ...
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        ...
        // chartView is the name of the widget your promoted
        ...
    
        // Create your chart view
        ui->chartView->setRenderHint(QPainter::Antialiasing);
        ui->chartView->setChart(chart);
    }