MrHulu / QmlQCustomPlot

Based on QCustomPlot, a simple package that supports the use of Qml.
5 stars 0 forks source link

qt6.6.2 上跑,报错找不到属性 #1

Closed StoneBang closed 3 months ago

StoneBang commented 3 months ago

image

MrHulu commented 3 months ago

@StoneBang 我没有在Qt6上尝试过,我感觉是x和y与基础属性冲突了,Qt6的检查可能比较严重,你可以使用以下补丁尝试?

 main.qml | 42 +++++++++++++++++++++---------------------
 1 file changed, 21 insertions(+), 21 deletions(-)

diff --git a/main.qml b/main.qml
index 9675b26..0f46d5f 100644
--- a/main.qml
+++ b/main.qml
@@ -21,21 +21,21 @@ ApplicationWindow {
                 id: timePlot
                 anchors.fill: parent
                 plotTimeRangeInMilliseconds: 10         
-                x.visible: true
-                y.visible: true
-                x1.visible: false
-                y1.visible: false
-                y.ticker.tickCount: 6
-                y.ticker.ticks: false
-                y.ticker.subTicks: false
-                y.ticker.baseColor: "transparent"
-                y.grid.lineColor: "mediumaquamarine"
-                x.ticker.baseColor: "midnightblue"
-                x.ticker.baseWidth: 2
-                x.grid.lineColor: "transparent"
+                xAxis.visible: true
+                yAxis.visible: true
+                x1Axis.visible: false
+                y1Axis.visible: false
+                yAxis.ticker.tickCount: 6
+                yAxis.ticker.ticks: false
+                yAxis.ticker.subTicks: false
+                yAxis.ticker.baseColor: "transparent"
+                yAxis.grid.lineColor: "mediumaquamarine"
+                xAxis.ticker.baseColor: "midnightblue"
+                xAxis.ticker.baseWidth: 2
+                xAxis.grid.lineColor: "transparent"
                 backgroundColor: "mistyrose"
                 Component.onCompleted: {
-                    y.setRange(0, 100)
+                    yAxis.setRange(0, 100)
                     addGraph("1")
                     graphs["1"].graphColor = "slategrey"
                 }
@@ -58,13 +58,13 @@ ApplicationWindow {
             BasePlot {
                 anchors.fill: parent
                 backgroundColor: "gainsboro"
-                x.ticker.ticks: false
-                x.ticker.subTicks: false
-                y.ticker.ticks: false
-                y.ticker.subTicks: false
+                xAxis.ticker.ticks: false
+                xAxis.ticker.subTicks: false
+                yAxis.ticker.ticks: false
+                yAxis.ticker.subTicks: false
                 Component.onCompleted: {
-                    x.label = "x"
-                    y.label = "y'"
+                    xAxis.label = "xAxis"
+                    yAxis.label = "yAxis'"
                     addGraph("1")
                     graphs["1"].graphColor = "lightcoral"
                     graphs["1"].graphWidth = 2
@@ -75,7 +75,7 @@ ApplicationWindow {
                         yData.push(xData[i] * xData[i])
                     }
                     graphs["1"].setData(xData, yData)
-                    graphs["1"].name = "y = x^2"
+                    graphs["1"].name = "yAxis = xAxis^2"
                     addGraph("2")
                     graphs["2"].graphColor = "lightseagreen"
                     graphs["2"].graphWidth = 3
@@ -86,7 +86,7 @@ ApplicationWindow {
                         yData.push(xData[i] * xData[i] * 2  + 100)
                     }
                     graphs["2"].setData(xData, yData)
-                    graphs["2"].name = "y = 2x^2 + 100"
+                    graphs["2"].name = "yAxis = 2x^2 + 100"
                     rescaleAxes(true)
                 }
             }
 qtquickcontrols2.conf | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/qtquickcontrols2.conf b/qtquickcontrols2.conf
index 8980bf6..d907935 100644
--- a/qtquickcontrols2.conf
+++ b/qtquickcontrols2.conf
@@ -12,4 +12,4 @@ Theme=Light
 [Material]
 Theme=Light
 Accent=BlueGrey
-Primary=BlueGray
\ No newline at end of file
+Primary=BlueGrey
\ No newline at end of file
 src/TimePlot.cpp | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/TimePlot.cpp b/src/TimePlot.cpp
index f84104d..dd21f73 100644
--- a/src/TimePlot.cpp
+++ b/src/TimePlot.cpp
@@ -12,9 +12,9 @@ TimePlot::TimePlot(QQuickItem *parent)
     : BasePlot(parent)
     , m_timer(new QTimer(this))
 {
-    x()->setTickerType(Axis::Time);
+    xAxis()->setTickerType(Axis::Time);
     connect(m_timer, &QTimer::timeout, this, &TimePlot::onTimeOut);
-    m_timer->start(1);
+    m_timer->start(5);
     startTimer(25);
     m_plotTimeRangeInMilliseconds = 60;
 }
@@ -33,10 +33,10 @@ void TimePlot::set_plotTimeRangeInMilliseconds(int value) noexcept

 Q_INVOKABLE void TimePlot::setTimeFormat(const QString &format) noexcept
 {
-    if(x()) {
+    if(xAxis()) {
         QSharedPointer<QCPAxisTickerTime> timeTicker(new QCPAxisTickerTime);
         timeTicker->setTimeFormat(format);
-        x()->setTicker(timeTicker);
+        xAxis()->setTicker(timeTicker);
     }
 }

@@ -74,8 +74,8 @@ void TimePlot::onTimeOut() noexcept
     //     });
     //     m_lastAddedTime = m_currentTimeKey;
     // }
-    if(x())
-        x()->setRange(m_currentTimeKey - m_plotTimeRangeInMilliseconds, m_currentTimeKey);
+    if(xAxis())
+        xAxis()->setRange(m_currentTimeKey - m_plotTimeRangeInMilliseconds, m_currentTimeKey);
     // QCP::MarginSide s = &static_cast<QCP::MarginSide*>((customPlot()->axisRect()->autoMargins()));
     // qDebug() << s;
 }
 src/baseplot.cpp | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/baseplot.cpp b/src/baseplot.cpp
index 0b98122..4ff8873 100644
--- a/src/baseplot.cpp
+++ b/src/baseplot.cpp
@@ -20,24 +20,24 @@ BasePlot::BasePlot(QQuickItem *parent)
     connect(this, &QQuickPaintedItem::heightChanged, this, &BasePlot::onChartViewSizeChanged);   
     connect(m_customPlot, &QCustomPlot::afterReplot, this, &BasePlot::onChartViewReplot, Qt::UniqueConnection);
     try {
-        m_x = new Axis(m_customPlot->xAxis, m_customPlot, this);
-        m_x1 = new Axis(m_customPlot->xAxis2, m_customPlot, this);
-        m_y = new Axis(m_customPlot->yAxis, m_customPlot, this);
-        m_y1 = new Axis(m_customPlot->yAxis2, m_customPlot, this);
-        connect(m_x, &Axis::destroyed, this, [this]{ m_x = nullptr; Q_EMIT xChanged(nullptr); });
-        connect(m_x1, &Axis::destroyed, this, [this]{ m_x1 = nullptr; Q_EMIT x1Changed(nullptr);});
-        connect(m_y, &Axis::destroyed, this, [this]{ m_y = nullptr; Q_EMIT yChanged(nullptr); });
-        connect(m_y1, &Axis::destroyed, this, [this]{ m_y1 = nullptr; Q_EMIT y1Changed(nullptr);});
+        m_xAxis = new Axis(m_customPlot->xAxis, m_customPlot, this);
+        m_x1Axis = new Axis(m_customPlot->xAxis2, m_customPlot, this);
+        m_yAxis = new Axis(m_customPlot->yAxis, m_customPlot, this);
+        m_y1Axis = new Axis(m_customPlot->yAxis2, m_customPlot, this);
+        connect(m_xAxis, &Axis::destroyed, this, [this]{ m_xAxis = nullptr; Q_EMIT xAxisChanged(nullptr); });
+        connect(m_x1Axis, &Axis::destroyed, this, [this]{ m_x1Axis = nullptr; Q_EMIT x1AxisChanged(nullptr);});
+        connect(m_yAxis, &Axis::destroyed, this, [this]{ m_yAxis = nullptr; Q_EMIT yAxisChanged(nullptr); });
+        connect(m_y1Axis, &Axis::destroyed, this, [this]{ m_y1Axis = nullptr; Q_EMIT y1AxisChanged(nullptr);});
         connect(m_customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), m_customPlot->xAxis2, SLOT(setRange(QCPRange)));
         connect(m_customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), m_customPlot->yAxis2, SLOT(setRange(QCPRange)));

     }
     catch(const std::exception &e) {
         qCritical() << e.what();
-        m_x = nullptr;
-        m_x1 = nullptr;
-        m_y = nullptr;
-        m_y1 = nullptr;
+        m_xAxis = nullptr;
+        m_x1Axis = nullptr;
+        m_yAxis = nullptr;
+        m_y1Axis = nullptr;
     }
     update();
 }
 src/baseplot.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/baseplot.h b/src/baseplot.h
index cfd5092..f6ccb31 100644
--- a/src/baseplot.h
+++ b/src/baseplot.h
@@ -17,10 +17,10 @@ class BasePlot : public QQuickPaintedItem
 {
     Q_OBJECT
     QML_READ_WRITE_NOTIFY_PROPERTY(QColor, backgroundColor)
-    QML_READ_NOTIFY_PROPERTY(QmlQCustomPlot::Axis *, x)
-    QML_READ_NOTIFY_PROPERTY(QmlQCustomPlot::Axis *, x1)
-    QML_READ_NOTIFY_PROPERTY(QmlQCustomPlot::Axis *, y)
-    QML_READ_NOTIFY_PROPERTY(QmlQCustomPlot::Axis *, y1)
+    QML_READ_NOTIFY_PROPERTY(QmlQCustomPlot::Axis *, xAxis)
+    QML_READ_NOTIFY_PROPERTY(QmlQCustomPlot::Axis *, x1Axis)
+    QML_READ_NOTIFY_PROPERTY(QmlQCustomPlot::Axis *, yAxis)
+    QML_READ_NOTIFY_PROPERTY(QmlQCustomPlot::Axis *, y1Axis)
     Q_PROPERTY(QVariantMap graphs READ graphs NOTIFY graphsChanged)
 public:
     BasePlot(QQuickItem *parent = nullptr);
StoneBang commented 3 months ago

按照你的思路,修改完了是可以正常运行了 image 可是切换tab的时候,报错了.内容如下: image

MrHulu commented 3 months ago

@StoneBang 你的环境是什么?我可能需要下载一个Qt6测试一下,这个问题应该不存在的,十分奇怪

StoneBang commented 3 months ago

是我个人问题,修改的时候,yAxis没有被赋值.现在已经跑起来.只是现在效果交互并不是特别多.但是作为自定义qCsutomPlot并且引入到qml中使用,是个对新手很好的启蒙项目.感谢开源

MrHulu commented 3 months ago

是我个人问题,修改的时候,yAxis没有被赋值.现在已经跑起来.只是现在效果交互并不是特别多.但是作为自定义qCsutomPlot并且引入到qml中使用,是个对新手很好的启蒙项目.感谢开源

熟悉了可以直接改源码以满足你的交互效果,应该很简单。


我准备关了这个issues了,你是否还有其他问题?

StoneBang commented 3 months ago

目前没有了