Search code examples
c++qtqwt

No member named 'setMargin' in 'QwtPlotLayout' - Convert Qt4.7 to Qt5.8


I need to convert Qt legacy code from 4.7 to 5.8, I have a compilation error in Qt Creator 4.2.1 Clang 7.0(Apple) 64bit. I'm using latest Qwt 6.1.3

Looking in .cpp file

#include "frmMainChart_UI.h"
#include <QHeaderView>
#include <qwt_plot_layout.h>
#include <qwt_legend.h>
#include "mpiDateScale.h"
#include "mpiPercentScale.h"

void frmMainChart_UI::setupUI(QWidget *parent_)
{
    frmMainTableViewTree_UI::setupUI(QMap<int, QString>(), false, parent_);
    delete frmMainTableViewTree_UI::tableCopy;
    delete frmMainTableViewTree_UI::table;

    chart = new mpiChart(widget);
    chart->setAxisScaleDraw(QwtPlot::xBottom, new mpiDateScale());
    chart->setAxisScaleDraw(QwtPlot::yLeft, new mpiPercentScale());
    chart->plotLayout()->setCanvasMargin(20);
    chart->plotLayout()->setMargin(20);  // BROKE convert Qt4 to Qt5
    chartZoomer = new mpiPlotZoomer(chart->canvas()); // BROKE convert Qt4 to Qt5
    chartLegend = new QwtLegend(chart);
    chart->insertLegend(chartLegend, QwtPlot::RightLegend);

    QLinearGradient grad(0, 0, 1, 1);
    grad.setCoordinateMode(QGradient::StretchToDeviceMode);
    grad.setColorAt(0, Qt::white);
    grad.setColorAt(1, QColor(220, 220, 220));

2 Errors in .cpp

../src/ui/frmMainChart_UI.cpp:18:26: error: no member named 'setMargin' in 'QwtPlotLayout' chart->plotLayout()->setMargin(20); // BROKE convert Qt4 to Qt5

../src/ui/frmMainChart_UI.cpp:19:23: error: no matching constructor for initialization of 'mpiPlotZoomer' chartZoomer = new mpiPlotZoomer(chart->canvas()); // BROKE convert Qt4 to Qt5

           ^

5 warnings and 2 errors generated make: *** [frmMainChart_UI.o] Error 1 06:58:25: The process "/usr/bin/make" exited with code 2. Error while building/deploying project mypersonalindex (kit: Desktop Qt 5.8.0 clang 64bit) When executing step "Make" 06:58:25: Elapsed time: 00:01.

The Qwt 6.1.3 Docs has member function http://qwt.sourceforge.net/class_qwt_plot_layout.html

void    setCanvasMargin (int margin, int axis=-1)

But NO Member Function being used

setMargin

My C++ skill is pretty limited, do you see any minor tweaks that could convert this from Qt4 to Qt5. ... so what is the replacement?

Looking at mpiChart.h likely relates to canvas() error

#ifndef MPICHART_H
#define MPICHART_H

#include "qwt_plot.h"

class mpiChart : public QwtPlot
{
    Q_OBJECT

public:
    mpiChart(QWidget *parent_ = 0):
        QwtPlot(parent_)
    {}

public slots:
    void exportChart();
};

#endif // MPICHART_H

And Looking in mpiPlotZoomer.h relates to canvas() error

#ifndef MPIPLOTZOOMER_H
#define MPIPLOTZOOMER_H

#include <qwt_plot_zoomer.h>
#include <qwt_plot_canvas.h>  // JDL convert Qt4 to Qt5
#include <qwt_compat.h>  // JDL convert Qt4 to Qt5

class mpiPlotZoomer: public QwtPlotZoomer
{
public:
    mpiPlotZoomer(QwtPlotCanvas *canvas_):
        QwtPlotZoomer(canvas_, false)
    {
        setTrackerMode(AlwaysOn);
    }

    virtual QwtText trackerText(const QwtDoublePoint &pos_) const;
};

#endif // MPIPLOTZOOMER_H

Solution

  • As the setMargin function has been removed between Qwt version you were using for Qt 4.7 and Qwt 1.6.3 you are using with 5.8, you have no other choice than:

    • Replace setMargin calls by setCanvasMargin if that fits your needs
    • Or, remove setMargin calls

    Try both, and see which one looks the best when displaying the GUI.

    For the canvas() error, it's hard to tell without seeing mpiChart and mpiPlotZoomer declaration. However, I'll give it a try:

    canvas() used to return a QwtPlotCanvas* in the past. For recent versions of Qwt it returns a QWidget*. So if your mpiPlotZoomer constructor expects a QwtPlotCanvas* as parameter, you'll have to:

    • Replace mpiPlotZoomer parameters type from QwtPlotCanvas* by QWidget*. May work if the guy who wrote the mpiPlotZoomer class does not actually use and QwtPlotCanvas members/attributes (he may only use it for parenting purpose)
    • Or replace mpiPlotZoomer(chart->canvas()); by mpiPlotZoomer( qobject_cast<QwtPlotCanvas*>( chart->canvas() ) ); which will hoepfully work fine.