QT开发中文网

E:\YZYEdu\WhiteBoard\main.cpp:17: error: 'const class QList' has no member named 'data'; did you mean 'at'? painter.drawPolyline(curve.data(), curve.size()); ^~~~ ...

抱歉,我之前的回答有误。在Qt中,QList没有提供直接获取数据指针的方法。要绘制QList类型的曲线,可以使用QPainterPath类来实现。以下是修正后的示例代码:

#include <QtWidgets>

class CustomWidget : public QWidget {
public:
    CustomWidget(QWidget *parent = nullptr) : QWidget(parent), drawing(false) {
        setMouseTracking(true);
    }

protected:
    void paintEvent(QPaintEvent *event) override {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing, true);
        painter.setPen(QPen(Qt::black, 2));

        for (const QList<QPointF>& curve : curves) {
            if (curve.size() >= 2) {
                QPainterPath path;
                path.addPolygon(curve.toPolygon());
                painter.drawPath(path);
            }
        }
    }

    void mousePressEvent(QMouseEvent *event) override {
        if (event->button() == Qt::LeftButton) {
            currentCurve.clear();
            currentCurve.append(event->pos());
            drawing = true;
            update();
        }
    }

    void mouseMoveEvent(QMouseEvent *event) override {
        if (drawing && event->buttons() & Qt::LeftButton) {
            currentCurve.append(event->pos());
            update();
        }
    }

    void mouseReleaseEvent(QMouseEvent *event) override {
        if (event->button() == Qt::LeftButton && drawing) {
            currentCurve.append(event->pos());

            // Apply the Bezier algorithm to smooth the curve
            QList<QPointF> smoothedCurve = bezierAlgorithm(currentCurve);

            // Add the smoothed curve to the list of curves
            curves.append(smoothedCurve);

            // Clear the current curve and finish drawing
            currentCurve.clear();
            drawing = false;

            update();
        }
    }

private:
    QList<QList<QPointF>> curves;
    QList<QPointF> currentCurve;
    bool drawing;

    QList<QPointF> bezierAlgorithm(const QList<QPointF>& points, int resolution = 50) {
        QList<QPointF> smoothedCurve;

        if (points.size() < 2) {
            return smoothedCurve;
        }

        for (int i = 0; i <= resolution; ++i) {
            qreal t = static_cast<qreal>(i) / resolution;

            QPointF point;
            for (int j = 0; j < points.size(); ++j) {
                qreal coefficient = binomialCoefficient(points.size() - 1, j);
                qreal term = coefficient * qPow(t, j) * qPow(1 - t, points.size() - 1 - j);
                point += term * points[j];
            }

            smoothedCurve.append(point);
        }

        return smoothedCurve;
    }

    int binomialCoefficient(int n, int k) const {
        if (k > n)
            return 0;

        int result = 1;
        for (int i = 0; i < k; ++i) {
            result *= (n - i);
            result /= (i + 1);
        }
        return result;
    }
};

int main(int argc, char **argv) {
    QApplication app(argc, argv);

    CustomWidget widget;
    widget.resize(800, 600);
    widget.show();

   return app.exec();
}

这样应该可以正常编译和运行了。如果还有其他问题,请随时提问!


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:http://qt.0voice.com/?id=11880

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?