Qt 5.10 - 如何将图表示例添加到布局

Qt 5.10 - How to add a chart example to a layout

这是我第一次使用 Qt,所以我希望它是一个基本问题和我找不到的基本答案。我需要添加一个 "custom" 图表小部件

(like in the following example: https://doc.qt.io/qt-5.10/qtcharts-zoomlinechart-example.html)

到一些 Qt 容器,如 "layout" 或 "form"。当您在工具箱中有小部件并使用设计器时,它非常简单,但由于它是自定义小部件,我无法做到这一点。

在 Qt Creator 中如何做到这一点?

如果您的图表 class 继承自 QWidget,您有两种方法可以做到这一点:

  1. 在设计模式下,将 QWidget 放在 Window 上,或放入布局中。然后,右键单击它并 select "Promote to ..."。在那里,在 "Promoted class name".

  2. 中写下你的 class 名字
  3. 在您的 window cpp 文件中,写入:

    YourChartClass* ycc = new YourChartClass(this);
    ycc.show();
    

    YourChartClass* ycc = new YourChartClass();
    this->layout()->addWidget(ycc)
    

如果您的图表继承自另一个 class,而那个 class 继承自 QWidget,代码行是相同的。