[英]C++ Qt in ubuntu Segmentation fault when trying to new object Qchart
I'm trying to use Qchart in ubuntu.我正在尝试在 ubuntu 中使用 Qchart。 I have a segmentation fault(sigsegv) error when running the code below.
运行以下代码时出现分段错误(sigsegv)错误。 This error exists when creating the Qchart object.
创建 Qchart object 时存在此错误。
header file: header 文件:
#include <QtCharts/QChart>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
using namespace QtCharts;
class Test
{
public:
Test();
~Test();
void Run();
private:
QLineSeries *series;
QChart *chart;
QChartView *chartView;
};
cpp file: cpp文件:
#include "test.h"
Test::Test()
{
series = new QLineSeries();
chart = new QChart();
chartView = new QChartView(chart);
}
void Test::Run(){
series->append(0, 6);
series->append(2, 4);
series->append(3, 8);
series->append(7, 4);
series->append(10, 5);
chart->legend();
chart->addSeries(series);
chart->createDefaultAxes();
chart->setTitle("Simple line chart example");
chartView->setRenderHint(QPainter::Antialiasing);
chartView->show();
}
Test::~Test(){
delete series;
delete chart;
delete chartView;
}
What's the problem?有什么问题? And how to fix this error?
以及如何解决这个错误?
I don't know if you still need help about this but in case someone does, I will share my solution.我不知道你是否还需要这方面的帮助,但如果有人需要,我会分享我的解决方案。
First of all, you need to ensure QApplication
is declared and working correctly (do not confuse with QGuiApplication
).首先,您需要确保
QApplication
已声明并正常工作(不要与QGuiApplication
混淆)。
Secondly, if you necessarily need to modularize the chart (like you do by defining the Test
class and define the chart attributes in it), you need to initialize the QChart
(or the function or constructor that includes the initialization of QChart
) in main window widget's constructor.其次,如果您需要模块化图表(就像您通过定义
Test
QChart
并在其中定义图表属性所做的那样),您需要在主QChart
小部件的构造函数。 In Windows, this approach would solve your problem:在 Windows 中,这种方法可以解决您的问题:
in mainwindow.h:在 mainwindow.h 中:
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
Test testObject;
//other stuff
}
in mainwindow.cpp在主窗口.cpp
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow)
{
testObject = Test();
//other stuff
}
Note that MainWindow is default QMainWindow widget class in Windows.请注意,MainWindow 是 Windows 中的默认 QMainWindow 小部件 class。
I am not sure but the source of the problem might be Q_DISABLE_COPY(QChart)
line in qchart.h file.我不确定,但问题的根源可能是 qchart.h 文件中的
Q_DISABLE_COPY(QChart)
行。 It needs to throw a compile-time error but instead causes a segmentation fault on runtime.它需要抛出编译时错误,而是在运行时导致分段错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.