#include "mainwindow.h"#include <QVBoxLayout>#include <QHBoxLayout>#include <QPainter>#include <QFileDialog>MainWindow::MainWindow(QWidget parent) : QMainWindow(parent){ // 设置窗口大小和标题 setFixedSize(800, 600); setWindowTitle(tr("Function Graph Drawer")); // 创建输入框和确认按钮 m_inputLineEdit = new QLineEdit; m_okButton = new QPushButton(tr("OK")); m_clearButton = new QPushButton(tr("Clear")); m_saveButton = new QPushButton(tr("Save")); // 创建绘制区域 m_drawWidget = new QWidget; m_drawWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); // 设置布局 QVBoxLayout mainLayout = new QVBoxLayout; QHBoxLayout* inputLayout = new QHBoxLayout; inputLayout->addWidget(m_inputLineEdit); inputLayout->addWidget(m_okButton); inputLayout->addWidget(m_clearButton); inputLayout->addWidget(m_saveButton); mainLayout->addLayout(inputLayout); mainLayout->addWidget(m_drawWidget); QWidget* centralWidget = new QWidget; centralWidget->setLayout(mainLayout); setCentralWidget(centralWidget); // 连接按钮的信号和槽函数 connect(m_okButton, &QPushButton::clicked, this, &MainWindow::onOkButtonClicked); connect(m_clearButton, &QPushButton::clicked, this, &MainWindow::onClearButtonClicked); connect(m_saveButton, &QPushButton::clicked, this, &MainWindow::onSaveButtonClicked);}MainWindow::~MainWindow(){}void MainWindow::onOkButtonClicked(){ // 获取输入的函数 std::string function = m_inputLineEdit->text().toStdString(); // 在绘制区域中绘制函数图像 QPainter painter(m_drawWidget); painter.setRenderHint(QPainter::Antialiasing, true); painter.setPen(QPen(Qt::blue, 1)); // 绘制坐标轴和函数图像的代码省略,需要根据输入的函数计算出相应的点坐标 // 绘制完成后调用QWidget的update方法刷新显示 m_drawWidget->update();}void MainWindow::onClearButtonClicked(){ // 清除绘制区域中的所有图像 m_drawWidget->update();}void MainWindow::onSaveButtonClicked(){ // 弹出文件保存对话框,选择保存路径和文件名 QString fileName = QFileDialog::getSaveFileName(this, tr("Save Image"), "", tr("JPEG (*.jpg)")); // 将绘制区域中的内容保存为图片 QImage image(m_drawWidget->size(), QImage::Format_RGB32); QPainter painter(&image); m_drawWidget->render(&painter); image.save(fileName);}将这段代码省略的根据输入的函数绘制坐标轴和函数图像的代码补全
时间: 2023-11-28 12:49:28 浏览: 73
moc.exe 解决moc过程中不支持处理enum包含#include头文件的语法。
5星 · 资源好评率100%
在onOkButtonClicked()函数中,需要根据输入的函数计算出相应的点坐标,并绘制坐标轴和函数图像。以下是可能的实现:
```cpp
void MainWindow::onOkButtonClicked(){
// 获取输入的函数
std::string function = m_inputLineEdit->text().toStdString();
// 在绘制区域中绘制函数图像
QPainter painter(m_drawWidget);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::blue, 1));
// 绘制坐标轴
painter.drawLine(0, m_drawWidget->height() / 2, m_drawWidget->width(), m_drawWidget->height() / 2);
painter.drawLine(m_drawWidget->width() / 2, 0, m_drawWidget->width() / 2, m_drawWidget->height());
// 绘制函数图像
double x_step = 0.1; // x轴单位长度代表的值
double y_step = -0.1; // y轴单位长度代表的值,注意是负数,因为y轴正方向向下
double x_offset = m_drawWidget->width() / 2; // x轴偏移量
double y_offset = m_drawWidget->height() / 2; // y轴偏移量
double x = -x_offset / x_step; // 从x轴左端点开始绘制
double y = 0;
bool valid = true; // 标记计算是否有效
while (x <= x_offset / x_step) {
// 计算当前点的y值
// 这里假设输入的函数只包含加减乘除和常数,变量为x
// 如果需要支持更复杂的函数,可以使用第三方库或自己实现解析器
std::string expr = function;
size_t pos = expr.find("x");
while (pos != std::string::npos) {
expr.replace(pos, 1, "(" + std::to_string(x) + ")");
pos = expr.find("x", pos + 1);
}
try {
y = eval(expr.c_str());
}
catch (const std::invalid_argument&) {
valid = false;
break;
}
// 绘制当前点和上一个点之间的线段
if (x > -x_offset / x_step) {
int x1 = static_cast<int>((x - x_step) * x_step + x_offset);
int y1 = static_cast<int>(y_step * y + y_offset);
int x2 = static_cast<int>(x * x_step + x_offset);
int y2 = static_cast<int>(y_step * y + y_offset);
painter.drawLine(x1, y1, x2, y2);
}
x += x_step;
}
// 绘制完成后调用QWidget的update方法刷新显示
if (valid) {
m_drawWidget->update();
}
else {
// 如果计算无效,弹出错误提示框
QMessageBox::warning(this, tr("Error"), tr("Invalid function!"));
}
}
```
其中,eval()函数是一个简单的表达式求值函数,可以使用第三方库或自己实现一个更复杂的解析器。以下是eval()函数的实现:
```cpp
double eval(const char* expr) {
std::stack<double> num_stack;
std::stack<char> op_stack;
const char* p = expr;
while (*p != '\0') {
if (*p == '(') {
op_stack.push(*p);
}
else if (*p >= '0' && *p <= '9') {
double num = 0;
while (*p >= '0' && *p <= '9') {
num = num * 10 + (*p - '0');
++p;
}
num_stack.push(num);
--p;
}
else if (*p == '+' || *p == '-') {
while (!op_stack.empty() && op_stack.top() != '(') {
double num2 = num_stack.top();
num_stack.pop();
double num1 = num_stack.top();
num_stack.pop();
if (op_stack.top() == '+') {
num_stack.push(num1 + num2);
}
else {
num_stack.push(num1 - num2);
}
op_stack.pop();
}
op_stack.push(*p);
}
else if (*p == '*' || *p == '/') {
while (!op_stack.empty() && (op_stack.top() == '*' || op_stack.top() == '/')) {
double num2 = num_stack.top();
num_stack.pop();
double num1 = num_stack.top();
num_stack.pop();
if (op_stack.top() == '*') {
num_stack.push(num1 * num2);
}
else {
num_stack.push(num1 / num2);
}
op_stack.pop();
}
op_stack.push(*p);
}
else if (*p == ')') {
while (!op_stack.empty() && op_stack.top() != '(') {
double num2 = num_stack.top();
num_stack.pop();
double num1 = num_stack.top();
num_stack.pop();
if (op_stack.top() == '+') {
num_stack.push(num1 + num2);
}
else if (op_stack.top() == '-') {
num_stack.push(num1 - num2);
}
else if (op_stack.top() == '*') {
num_stack.push(num1 * num2);
}
else {
num_stack.push(num1 / num2);
}
op_stack.pop();
}
op_stack.pop();
}
++p;
}
while (!op_stack.empty()) {
double num2 = num_stack.top();
num_stack.pop();
double num1 = num_stack.top();
num_stack.pop();
if (op_stack.top() == '+') {
num_stack.push(num1 + num2);
}
else if (op_stack.top() == '-') {
num_stack.push(num1 - num2);
}
else if (op_stack.top() == '*') {
num_stack.push(num1 * num2);
}
else {
num_stack.push(num1 / num2);
}
op_stack.pop();
}
return num_stack.top();
}
```
阅读全文