#include"scanline.h" #include"qpainter.h" #include<QDebug> #include "mainwindow.h" #include "ui_mainwindow.h" ScanLine::ScanLine() { } void ScanLine::DDALine(QPainter* painter,int x0,int y0,int x1,int y1) { int x; float dy,dx,k,y; dx=x1-x0; dy=y1-y0; y=y0; k=dy/dx; for(x=x0;x<=x1;x++) { painter->drawPoint(x,int(y+0.5)); y+=k; } painter->drawLine(x0,y0,x1,y1); }以上函数无法被 MainWindow::~MainWindow() { delete ui; } void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter painter(this); // // 反走样 painter.setRenderHint(QPainter::Antialiasing, true); // // 设置画笔颜色 painter.setPen(QColor(0, 160, 230)); // // 绘制直线 //// painter.drawLine(QPointF(ScanLine())); if(menunum==1)//微分绘线 { line=new ScanLine(); line->DDALine(&painter, 30, 10, 10, 30); update(); }调用的解决办法
时间: 2024-01-28 10:03:42 浏览: 183
rDebug_qDebug:为 Qt 4.8 ... 5.x 重新实现 qDebug 的方便、更强大-开源
首先,需要修改ScanLine类的DDALine函数的返回值,让它返回一个QPointF类型的值,表示绘制直线的起点和终点坐标。修改后的代码如下:
```cpp
#include "scanline.h"
#include <QPointF>
ScanLine::ScanLine() {}
QPointF ScanLine::DDALine(int x0, int y0, int x1, int y1) {
int x;
float dy, dx, k, y;
dx = x1 - x0;
dy = y1 - y0;
y = y0;
k = dy / dx;
for (x = x0; x <= x1; x++) {
y += k;
}
return QPointF(x0, y0) + QPointF(dx, dy);
}
```
然后在MainWindow的paintEvent函数中,调用DDALine函数获取绘制直线的起点和终点坐标,再使用QPainter的drawLine函数进行绘制。修改后的代码如下:
```cpp
void MainWindow::paintEvent(QPaintEvent *event) {
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QColor(0, 160, 230));
if (menunum == 1) {
line = new ScanLine();
QPointF end = line->DDALine(30, 10, 10, 30);
painter.drawLine(QPointF(30, 10), end);
update();
}
}
```
最后,在MainWindow的析构函数中,需要释放line对象的内存,修改后的代码如下:
```cpp
MainWindow::~MainWindow() {
delete ui;
if (line != nullptr) {
delete line;
}
}
```
阅读全文