若依,自动生成的代码粘贴进项目后,出现找不到符号的错误

将对应module中的target文件夹删除,然后重新编译

  • 8
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面我将详细地为您解释如何使用Qt5.14.2来制作一个函数图像绘制器。 首先,我们需要确保您已经安装了Qt5.14.2并且可以正常运行。接下来,我们需要添加QCustomPlot库,这是一个非常好用的开源图表绘制库,可以方便地绘制各种类型的图表。您可以从它的官网(https://www.qcustomplot.com/)上下载最新版本的库,然后将其添加到您的项目中。 1. 添加QCustomPlot库 将下载好的QCustomPlot库中的qcustomplot.h和qcustomplot.cpp文件拷贝到您的项目文件夹中。然后在Qt Creator中打开您的项目,在项目文件夹中右键,选择“添加新文件”,在弹出的窗口中选择“C++类”,将类名设置为“QCustomPlotWidget”,然后点击“下一步”。 在下一个界面中,将“基类”设置为“QWidget”,然后点击“下一步”并确认创建。随后会弹出一个“添加文件”对话框,您需要在该对话框中选择刚才拷贝到项目文件夹中的qcustomplot.h和qcustomplot.cpp文件。 此时,您需要在您的项目中添加以下代码来引用QCustomPlot库: ```cpp #include "qcustomplot.h" ``` 2. 创建界面 打开Qt Creator,在“设计”模式下创建一个新的窗口,然后将窗口的布局设置为“网格布局”。接下来,我们需要向窗口中添加一些控件,包括一个函数输入框、一个确认键、一个清除键、一个保存键以及几个常见的函数符号的按钮。您可以在Qt Creator的控件窗口中到这些控件并将它们拖拽到您的窗口中。 对于函数输入框、确认键、清除键和保存键,您需要分别设置它们的objectName属性为“functionInput”、“confirmButton”、“clearButton”和“saveButton”。 对于常见函数符号的按钮,您需要为每个按钮设置一个槽函数。例如,对于sin按钮,您需要将它的objectName属性设置为“sinButton”,然后在“槽”中添加一个名为“on_sinButton_clicked”的槽函数。 3. 编写代码 接下来,我们需要编写代码来实现这些功能。在此之前,您需要在您的项目中添加以下头文件: ```cpp #include <QFileDialog> #include <QMessageBox> ``` 这些头文件分别用于打开文件选择对话框和显示消息框。 首先,我们需要在QCustomPlotWidget类中添加以下代码: ```cpp class QCustomPlotWidget : public QWidget { Q_OBJECT public: explicit QCustomPlotWidget(QWidget *parent = nullptr); ~QCustomPlotWidget(); private: QCustomPlot *m_customPlot; }; ``` 这个类继承自QWidget,拥有一个QCustomPlot指针m_customPlot。 ```cpp #include "qcustomplotwidget.h" #include "ui_qcustomplotwidget.h" QCustomPlotWidget::QCustomPlotWidget(QWidget *parent) : QWidget(parent), ui(new Ui::QCustomPlotWidget) { ui->setupUi(this); m_customPlot = new QCustomPlot(this); m_customPlot->setMinimumSize(400, 300); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(m_customPlot); setLayout(layout); } QCustomPlotWidget::~QCustomPlotWidget() { delete ui; } ``` 在构造函数中,我们实例化了一个QCustomPlot对象,并将其添加到布局中。需要注意的是,我们必须要设置一个布局,否则QCustomPlot将无法正常显示。 接下来,我们需要在主窗口的头文件中添加以下代码: ```cpp #include "qcustomplotwidget.h" ``` 这个头文件用于引用QCustomPlotWidget类。 ```cpp #include <QWidget> QT_BEGIN_NAMESPACE namespace Ui { class MainWindow; } QT_END_NAMESPACE class MainWindow : public QWidget { Q_OBJECT public: MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_confirmButton_clicked(); void on_clearButton_clicked(); void on_saveButton_clicked(); void on_sinButton_clicked(); void on_cosButton_clicked(); void on_tanButton_clicked(); void on_logButton_clicked(); void on_lnButton_clicked(); private: Ui::MainWindow *ui; QCustomPlotWidget *m_customPlotWidget; }; ``` 这个类继承自QWidget,拥有一个QCustomPlotWidget指针m_customPlotWidget。 ```cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include "qcustomplot.h" #include "qcustomplotwidget.h" MainWindow::MainWindow(QWidget *parent) : QWidget(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); m_customPlotWidget = new QCustomPlotWidget(this); ui->verticalLayout->addWidget(m_customPlotWidget); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_confirmButton_clicked() { QString function = ui->functionInput->text(); if (function.isEmpty()) { QMessageBox::warning(this, tr("Warning"), tr("Please input a function!")); return; } m_customPlotWidget->m_customPlot->clearGraphs(); m_customPlotWidget->m_customPlot->addGraph(); m_customPlotWidget->m_customPlot->xAxis->setLabel("x"); m_customPlotWidget->m_customPlot->yAxis->setLabel("y"); QVector<double> x, y; double min = -10.0, max = 10.0, step = 0.1; for (double i = min; i <= max; i += step) { x.append(i); y.append(QVariant(function.replace("x", QString::number(i))).toDouble()); } m_customPlotWidget->m_customPlot->graph(0)->setData(x, y); m_customPlotWidget->m_customPlot->rescaleAxes(); m_customPlotWidget->m_customPlot->replot(); } void MainWindow::on_clearButton_clicked() { m_customPlotWidget->m_customPlot->clearGraphs(); m_customPlotWidget->m_customPlot->replot(); } void MainWindow::on_saveButton_clicked() { QString fileName = QFileDialog::getSaveFileName(this, tr("Save Image"), "", tr("JPEG Files (*.jpg)")); if (!fileName.isEmpty()) { m_customPlotWidget->m_customPlot->saveJpg(fileName); } } void MainWindow::on_sinButton_clicked() { ui->functionInput->insert("sin()"); } void MainWindow::on_cosButton_clicked() { ui->functionInput->insert("cos()"); } void MainWindow::on_tanButton_clicked() { ui->functionInput->insert("tan()"); } void MainWindow::on_logButton_clicked() { ui->functionInput->insert("log()"); } void MainWindow::on_lnButton_clicked() { ui->functionInput->insert("ln()"); } ``` 在构造函数中,我们实例化了一个QCustomPlotWidget对象,并将其添加到主窗口的布局中。 在on_confirmButton_clicked槽函数中,我们首先获取用户输入的函数表达式,然后判断是否为空。如果为空,则弹出一个警告框;否则,我们清空QCustomPlot并添加一个新的图表。接下来,我们生成一组x和y坐标的向量,并将函数表达式中的x替换为实际的x值,然后将这组向量作为数据设置到QCustomPlot中,并重新绘制图表。 在on_clearButton_clicked槽函数中,我们简单地清空QCustomPlot并重新绘制图表。 在on_saveButton_clicked槽函数中,我们打开一个文件选择对话框,让用户选择保存的文件名和路径,并将QCustomPlot的图表保存为JPEG格式的图片。 在on_sinButton_clicked、on_cosButton_clicked、on_tanButton_clicked、on_logButton_clicked和on_lnButton_clicked槽函数中,我们简单地在函数输入框中插入相应的函数符号。 4. 编译运行 最后,我们需要编译并运行程序。如果一切顺利,您将看到一个简单的函数图像绘制器,并且可以在其中输入函数表达式并绘制相应的图像。您还可以点击清除、保存和常见函数符号的按钮,以实现更多的功能。 希望这篇教程能够对您有所帮助!如果您有任何问题或疑问,请随时在评论区中留言,我会尽快回复您。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值