PDA

View Full Version : Pass a parameter to new QWidget



Blitzor DDD
27th July 2016, 19:49
Hello!

I want to create a program, which plots graph. I am using QtCharts (thanks God its available in Qt 5.7). The idea is: there is a Widget (inher. from QWidget), I push a button and create new QWidget - window and there is plot, which is build already.
But I want to re-plot it, so I put the int-number in QLineEdit on my new QWidget and I want the graph to re-plot, but the problem is that I already created a QWidget and graph is already there..
How can I pass parameter to new QWidget in order to re-plot my graph there..?

This is code:


void Widget::plotAGraph()
{
QVBoxLayout *vertical = new QVBoxLayout();
QHBoxLayout *horizontal = new QHBoxLayout();
QPushButton *ok2 = new QPushButton("Close");
QLineEdit *plotNumber = new QLineEdit("Step Number");
QSlider *stepp=new QSlider(Qt::Horizontal);
horizontal->addWidget(ok2);
horizontal->addWidget(plotNumber);
horizontal->addWidget(stepp);

QScatterSeries *series0 = new QScatterSeries();
// series0->setName("Первое сечение");
series0->setMarkerShape(QScatterSeries::MarkerShapeCircle);
series0->setMarkerSize(15.0);

//here is parameter of my graph__and parameter step I want to change___
int a=1;
for(int i=step;i<step+50;i++){
series0->append(a, obj.A[i]);
a++;

}
//__________________________________________________ _____________


QChart *chart=new QChart;

chart->addSeries(series0);
chart->setTitle("Сечение в первом разрезе");
// chart->axisX()->setRange(0,90);
// chart->axisY()->setRange(0,100);
chart->createDefaultAxes();
chart->axisX()->setRange(0,90);
chart->axisY()->setRange(0,100);

QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);


QWidget *newWindow=new QWidget();
vertical-> addWidget(chartView);
vertical-> addLayout(horizontal);
newWindow-> setLayout(vertical);

newWindow->setGeometry(300,150,0,0);
newWindow->resize(600,500);


newWindow->show();


connect(ok2,SIGNAL(clicked(bool)),newWindow,SLOT(r eplot()));

}

void Widget::replot()
{
//Doesnt work
// int step = plotNumber.text().toInt();
// how can I pass a parameter step in Loop in slot plotGraph and then to re-plot it all ?
}


in file Widget.cpp I created object of class calculation and named it obj.
In the class there is an Array A

I create a plot:

for(int i=step;i<step+50;i++){
series0->append(a, obj.A[i]);
a++;
}

а - step along x
А[i] - value accord. to the step

Thanks in advance!

anda_skoa
28th July 2016, 09:54
I am afraid I didn't understand what you are actually asking for?

You have an instance of "Widget" and you want to pass data to that object?

Cheers,
_

Blitzor DDD
28th July 2016, 10:33
Ok, I will try to explain.
I am using this: http://doc.qt.io/qt-5/qtcharts-scatterchart-example.html

When I push the button on QWidget open new QWidget with plot like in the article.
Then I would like to change my plot on new QWidget but the problem is when I create QWidget my plot is an argument of QWidget's object. and since then I am unable to change data of my plot.

P.S.
Or at least can I create a few graphs, independent from each other on QWidget from the start ?

anda_skoa
28th July 2016, 11:48
Well, can't you just keep a pointer to the object you want to change?

Cheers,
_

Blitzor DDD
28th July 2016, 18:24
Well, can't you just keep a pointer to the object you want to change?

I am sorry, definitely it is lack of knowledge from my side, but I see it this way: when I create new QWidget I create it with plot and in order to change it I must create new QWidget, right ?
If there is likelihood to solve it using pointer (I didnt see it in the example of off. doc.) please write a code with a pointer and interaction on its base.
Thanks

anda_skoa
28th July 2016, 20:34
I am sorry, definitely it is lack of knowledge from my side, but I see it this way: when I create new QWidget I create it with plot and in order to change it I must create new QWidget, right ?

No, the plot class has methods to change its data.



If there is likelihood to solve it using pointer (I didnt see it in the example of off. doc.) please write a code with a pointer and interaction on its base.

Instead of using a local variable "chartView" you use a member variable in your "Widget" class.
Then any method of "Widget" can access "chartView" and call its methods to change data.

Cheers,
_

Blitzor DDD
29th July 2016, 10:15
Ok dann. Ich hab nicht alles verstanden, aber sowiese vielen Dank fur Ihre Hilfe :)

d_stranz
30th July 2016, 04:00
Ok dann. Ich hab nicht alles verstanden, aber sowiese vielen Dank fur Ihre Hilfe

So fragen Sie mehr...