柚子快報激活碼778899分享:qt繪制曲線
柚子快報激活碼778899分享:qt繪制曲線
#pragma once
#include
class CurveWidget : public QWidget { ?? ?Q_OBJECT
public: ?? ?CurveWidget(QWidget* parent = nullptr) : QWidget(parent), lastPoint(QPointF()) {}
protected: ?? ?void mousePressEvent(QMouseEvent* event) override { ?? ??? ?if (event->button() == Qt::LeftButton) { ?? ??? ??? ?lastPoint = event->pos(); ?? ??? ??? ?points.clear(); ?? ??? ??? ?points.append(lastPoint); ?? ??? ??? ?update(); ?? ??? ?} ?? ?}
?? ?void mouseMoveEvent(QMouseEvent* event) override { ?? ??? ?if (event->buttons() & Qt::LeftButton) { ?? ??? ??? ?QPointF newPoint = event->pos(); ?? ??? ??? ?if ((newPoint - lastPoint).manhattanLength() > 0) { // 避免繪制過于密集的點 ? ?? ??? ??? ??? ?points.append(newPoint); ?? ??? ??? ??? ?lastPoint = newPoint; ?? ??? ??? ??? ?update(); ?? ??? ??? ?} ?? ??? ?} ?? ?}
?? ?void paintEvent(QPaintEvent* event) override { ?? ??? ?QPainter painter(this); ?? ??? ?painter.setRenderHint(QPainter::Antialiasing);
?? ??? ?QPainterPath path; ?? ??? ?if (!points.isEmpty()) { ?? ??? ??? ?path.moveTo(points.first()); ?? ??? ??? ?for (int i = 1; i < points.size(); ++i) { ?? ??? ??? ??? ?// 這里我們仍然使用lineTo來連接點,但為了視覺效果,你可以在最終渲染時應用平滑算法 ? ?? ??? ??? ??? ?path.lineTo(points[i]);
?? ??? ??? ??? ?// 繪制點 ? ?? ??? ??? ??? ?painter.setPen(Qt::NoPen); ?? ??? ??? ??? ?painter.setBrush(Qt::red); ?? ??? ??? ??? ?painter.drawEllipse(points[i], 3, 3); // 繪制以點為中心,半徑為3的小圓 ? ?? ??? ??? ?}
?? ??? ??? ?// 如果需要繪制平滑曲線,這里應該使用cubicTo或quadTo,并計算控制點 ?
?? ??? ??? ?painter.setPen(QPen(Qt::blue, 2)); ?? ??? ??? ?painter.setBrush(Qt::transparent); ?? ??? ??? ?painter.drawPath(path); ?? ??? ?} ?? ?}
private: ?? ?QVector
// 使用方法同前 ? // CurveWidget *widget = new CurveWidget(); ? // widget->show();
柚子快報激活碼778899分享:qt繪制曲線
相關文章
本文內容根據網絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯(lián)系刪除。