Little help with a project
-
Hello guys. I'm Brazilian and I'm learning QT. I want to make a simple project, but I'm having trouble developing the code. The idea is to create a method that does a sin (sin () math function) that receives as a parameter another method that increments from 1 to -1, with steps of 0.1. The first method will output an array that I would use to fill a chart. Someone help me? please..
-
@FelpsGK said in Little help with a project:
The idea is to create a method that does a sin (sin () math function)
that increments from 1 to -1, with steps of 0.1
for(double i = 1.0; i>=-1.0; i-=0.1)
The first method will output an array
QVector<double> array; array.reserve(20); for(double i = 1.0; i>=-1.0; i-=0.1) array << std::sin(i);
-
@VRonin I think I said it wrong. As a method, I mentioned the function. Something like "public void ....". I usually program in java, so I'm used to calling everything by method. But I understood logic. The question would be, how to fill a graph with this array? Make the graph receive the data and auto-fill it from the received array.
-
As a method, I mentioned the function.
std::sin
is already a function. I don't see the advantage of having a function doing what a for loop doesFrom https://doc.qt.io/qt-5/qtcharts-linechart-example.html
#include <QtWidgets/QApplication> #include <QtWidgets/QMainWindow> #include <QtCharts/QChartView> #include <QtCharts/QLineSeries> QT_CHARTS_USE_NAMESPACE int main(int argc, char *argv[]) { QApplication a(argc, argv); const double doublePi = 2.0*std::acos(-1); QLineSeries *series = new QLineSeries(); for(double i = 0; i>=doublePi ; i+=0.1) series->append(i, std::sin(i);); QChart *chart = new QChart(); chart->legend()->hide(); chart->addSeries(series); chart->createDefaultAxes(); chart->setTitle("Sin chart example"); QChartView *chartView = new QChartView(chart); chartView->setRenderHint(QPainter::Antialiasing); QMainWindow window; window.setCentralWidget(chartView); window.resize(400, 300); window.show(); return a.exec(); }
-
Kinda comes across as "do my job for me".
You are a developer, it's your job to read the documentation on Charts if you want charts... at least try.https://doc.qt.io/qt-5.9/qtcharts-index.html
To link against the Qt Charts module, add this line to your qmake project file:
QT += chartsI cannot find a reference to but seem to remember also needing:
widgets
Maybe because I use ChartView's? Dunno.QT_CHARTS_USE_NAMESPACE
is a macro and does the same as this, I can't really remember, but again, the docs say and how I now use is as per docs:If you intend to use Qt Charts C++ classes in your application, use the following include and using directives:
#include <QtCharts>
using namespace QtCharts;You won't (or shouldn't imo) get far being that kind of lazy. Nobody appreciates carrying another developer, we have enough to do already.