'QwtPlotLayout' 中没有名为 'setMargin' 的成员 - 将 Qt4.7 转换为 Qt5.8

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

我需要将 Qt 遗留代码从 4.7 转换为 5.8,我在 Qt Creator 4.2.1 Clang 7.0(Apple) 64 位中遇到编译错误。我正在使用最新的 Qwt 6.1.3

正在 .cpp 文件中查找

#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));

.cpp 中有 2 个错误

../src/ui/frmMainChart_UI.cpp:18:26: 错误:'QwtPlotLayout' 中没有名为 'setMargin' 的成员 图表->plotLayout()->setMargin(20); // 中断将 Qt4 转换为 Qt5

../src/ui/frmMainChart_UI.cpp:19:23: 错误:'mpiPlotZoomer' 的初始化没有匹配的构造函数 chartZoomer = new mpiPlotZoomer(图表->canvas()); // 中断将 Qt4 转换为 Qt5

           ^

生成了 5 个警告和 2 个错误 make: *** [frmMainChart_UI.o] 错误 1 06:58:25:进程“/usr/bin/make”已退出,代码为 2。 building/deploying 项目 mypersonalindex 时出错(工具包:桌面 Qt 5.8.0 clang 64 位) 当执行步骤 "Make" 06:58:25:经过时间:00:01.

Qwt 6.1.3 Docs有成员函数http://qwt.sourceforge.net/class_qwt_plot_layout.html

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

但是没有成员函数被使用

setMargin

我的 C++ 技能非常有限,您是否看到任何可以将其从 Qt4 转换为 Qt5 的小调整。 ...那么替代品是什么?

查看 mpiChart.h 可能与 canvas() 错误有关

#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

并查看 mpiPlotZoomer.h 与 canvas() 错误有关

#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

由于在您用于 Qt 4.7 的 Qwt 版本和用于 5.8 的 Qwt 1.6.3 之间删除了 setMargin 函数,您别无选择,只能:

  • 如果满足您的需要,请将 setMargin 调用替换为 setCanvasMargin
  • 或者,移除 setMargin 个调用

两者都试一下,看看哪一个在显示 GUI 时看起来最好。

对于canvas()错误,不看mpiChartmpiPlotZoomer声明就很难分辨。不过,我会试一试:

canvas() 过去曾 return 和 QwtPlotCanvas*。对于最新版本的 Qwt,它 return 是 QWidget*。因此,如果您的 mpiPlotZoomer 构造函数需要 QwtPlotCanvas* 作为参数,则您必须:

  • QwtPlotCanvas* 中的 mpiPlotZoomer 参数类型替换为 QWidget*。如果写 mpiPlotZoomer class 的人实际上没有使用并且 QwtPlotCanvas members/attributes (他可能只将其用于育儿目的)
  • 可能会起作用
  • 或将 mpiPlotZoomer(chart->canvas()); 替换为 mpiPlotZoomer( qobject_cast<QwtPlotCanvas*>( chart->canvas() ) );,这将非常有效。