Hello Everyone!
I'm trying to integrate a plot with qml using qmlRegisterType. When I create a plot without any parent widget,on a trigger from the qml, I'm able to call the function 'replot' without any problem.If I create the plot with a parent, 'replot' has no effect. Here's what I have so far.
mainwindow.h #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include"qcustomplot.h" #include<iostream> class MainWindow :public QWidget// public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = 0); ~MainWindow(); void setupQuadraticDemo( QCustomPlot* customPlot ); Q_INVOKABLE void plotGraph(int); Q_INVOKABLE void initGraph(int); QCustomPlot * customPlot; private: }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) { setGeometry(200, 250, 750,750); initGraph(7); } MainWindow::~MainWindow() { delete customPlot; } void MainWindow::plotGraph(int number) { std::cout<<"The integer passed is"<<number<<std::endl; QVector<double> x( 101 ), y( 101 ); for (int i = 0; i < 101; ++i) { x[i] = i ; y[i] = i*.2; } customPlot->graph()->clearData(); customPlot->graph()->setData(x,y); customPlot->replot(); customPlot->update(); } void MainWindow::initGraph(int number) { customPlot = new QCustomPlot(); customPlot->setGeometry(10,10,300,300); setupQuadraticDemo(customPlot); customPlot->replot(); customPlot->update(); customPlot->show(); } void MainWindow:: setupQuadraticDemo( QCustomPlot* customPlot ) { QVector<double> x( 101 ), y( 101 ); for (int i = 0; i < 101; ++i) { x[i] = i / 50.0 - 1; y[i] = x[i] * x[i]; } customPlot->addGraph(); customPlot->graph( 0 )->setPen( QPen( Qt::red ) ); customPlot->graph( 0 )->setSelectedPen( QPen( Qt::blue, 2 ) ); customPlot->graph( 0 )->setData( x, y ); customPlot->xAxis->setLabel( "x" ); customPlot->yAxis->setLabel( "y" ); customPlot->xAxis->setRange( -1, 1 ); customPlot->yAxis->setRange( -1, 1 ); }
main.cpp
#include <QApplication> #include "qmlapplicationviewer.h" #include <QtDeclarative/qdeclarative.h> #include "mainwindow.h" Q_DECL_EXPORT int main(int argc, char *argv[]) { QScopedPointer<QApplication> app(createApplication(argc, argv)); MainWindow window2; window2.show(); qmlRegisterType<MainWindow>("GraphComponenets", 1, 0, "MyGraph"); QmlApplicationViewer viewer(&window2); viewer.setGeometry(400,400,300,300); viewer.addImportPath(QLatin1String("modules")); viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto); viewer.setMainQmlFile(QLatin1String("qml/QmlCustomPLot/main.qml")); viewer.showExpanded(); return app->exec(); }
main.qml
import QtQuick 1.1
import GraphComponenets 1.0
Rectangle {
width: 360
height: 360
Text {
text: qsTr("Hello World")
anchors.centerIn: parent
}
Item
{
MyGraph
{
id:myGraph
function jSendNumber(number)
{
//myGraph.plotGraph((number))
// myGraph.initGraph(5)
}
function jChangePlot(number)
{
myGraph.plotGraph((number))
}
}
}
MouseArea {
anchors.fill: parent
onClicked: {
// Qt.quit();
myGraph.jSendNumber(10)
}
}
Rectangle
{
id:test
width:50;
height:100;
color:"red"
z:15
MouseArea
{
anchors.fill: parent
onClicked:
{
myGraph.jChangePlot(5);
}
}
}
}
in mainwindow.cpp-line 37, if i change it to
customPlot = new QCustomPlot(this);
then calling 'plotGraph' has no effect. Can someone please explain why this is so?
Thanks for any help!!:)