Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Line chart with sqlite database
Forum Updated to NodeBB v4.3 + New Features

Line chart with sqlite database

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 259 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krist0
    wrote on last edited by
    #1

    i am trying to put a line chart in the program but the program keeps crashing and wont let me go any further. here is the snippet of the function that's showing the error
    Any suggestions will be very very helpful```
    code_text

    {
        QSqlQuery qry1, qry2,qry3;
    
        qry2.prepare("select count(*) from fitness where username = '"+current+"'");
        qry2.exec();
        qry2.first();
        int noofrows = qry2.value(0).toInt();
        qDebug() << noofrows;
    
        int offset = noofrows - 5;
    
        QLineSeries *series = new QLineSeries();
        int i;
        qry3.prepare("select * from fitness where username = '"+current+"' limit 5 offset :offsetvalue ");
        qry3.bindValue(":offsetvalue",offset);
        qDebug() << current;
    
        int count = 0;
        if (qry3.exec()){
            while (qry3.next()){
                count = count + 1;
            }
        }
    
        qry1.prepare("select * from fitness username = '"+current+"' limit 5 offset :offsetvalue");
        qry1.bindValue(":offsetvalue",offset);
        qry1.exec();
    
        for (i=0; i<count; i++){
            qry1.next();
            QStringList dates = qry1.value(0).toString().split("-").mid(0,3);
            QDateTime momentInTime;
            momentInTime.setDate(QDate(dates[0].toInt(), dates[1].toInt() , dates[2].toInt()));
            series -> append(momentInTime.toMSecsSinceEpoch(), qry1.value(1).toDouble());
        }
    
        QChart *chart = new QChart();
        chart -> addSeries(series);
        chart -> legend() -> hide();
        chart -> setTitle("Weight Record");
        chart -> setTheme(QChart::ChartThemeHighContrast);
    
        QDateTimeAxis *axisX = new QDateTimeAxis;
        axisX->setFormat("dd MMM");
        axisX->setTitleText("Date");
        axisX->setTickCount(count);
        chart->addAxis(axisX, Qt::AlignBottom);
        series->attachAxis(axisX);
    
        QValueAxis *axisY = new QValueAxis;
        axisY->setLabelFormat("%i");
        axisY->setRange(66,75);
        axisY->setTickCount(10);
        axisY->setTitleText("Weight");
        chart->addAxis(axisY, Qt::AlignLeft);
        series->attachAxis(axisY);
    
        chart -> setAnimationOptions(QChart::GridAxisAnimations);
        chart -> setAnimationOptions(QChart::SeriesAnimations);
        chart -> setAnimationEasingCurve(QEasingCurve::OutCubic);
    
        QChartView *chartView = new QChartView(chart);
        chartView -> setRenderHint(QPainter::Antialiasing);
        chartView -> setParent(ui->horizontalFrame_chart);
    }```
    
    database:
    ![5d3e9be7-6604-4922-909a-b72cb63fab98-image.png](https://ddgobkiprc33d.cloudfront.net/9a0c6196-5586-4340-b2b5-ed129ff48cd1.png)
    Christian EhrlicherC 1 Reply Last reply
    0
    • K krist0

      i am trying to put a line chart in the program but the program keeps crashing and wont let me go any further. here is the snippet of the function that's showing the error
      Any suggestions will be very very helpful```
      code_text

      {
          QSqlQuery qry1, qry2,qry3;
      
          qry2.prepare("select count(*) from fitness where username = '"+current+"'");
          qry2.exec();
          qry2.first();
          int noofrows = qry2.value(0).toInt();
          qDebug() << noofrows;
      
          int offset = noofrows - 5;
      
          QLineSeries *series = new QLineSeries();
          int i;
          qry3.prepare("select * from fitness where username = '"+current+"' limit 5 offset :offsetvalue ");
          qry3.bindValue(":offsetvalue",offset);
          qDebug() << current;
      
          int count = 0;
          if (qry3.exec()){
              while (qry3.next()){
                  count = count + 1;
              }
          }
      
          qry1.prepare("select * from fitness username = '"+current+"' limit 5 offset :offsetvalue");
          qry1.bindValue(":offsetvalue",offset);
          qry1.exec();
      
          for (i=0; i<count; i++){
              qry1.next();
              QStringList dates = qry1.value(0).toString().split("-").mid(0,3);
              QDateTime momentInTime;
              momentInTime.setDate(QDate(dates[0].toInt(), dates[1].toInt() , dates[2].toInt()));
              series -> append(momentInTime.toMSecsSinceEpoch(), qry1.value(1).toDouble());
          }
      
          QChart *chart = new QChart();
          chart -> addSeries(series);
          chart -> legend() -> hide();
          chart -> setTitle("Weight Record");
          chart -> setTheme(QChart::ChartThemeHighContrast);
      
          QDateTimeAxis *axisX = new QDateTimeAxis;
          axisX->setFormat("dd MMM");
          axisX->setTitleText("Date");
          axisX->setTickCount(count);
          chart->addAxis(axisX, Qt::AlignBottom);
          series->attachAxis(axisX);
      
          QValueAxis *axisY = new QValueAxis;
          axisY->setLabelFormat("%i");
          axisY->setRange(66,75);
          axisY->setTickCount(10);
          axisY->setTitleText("Weight");
          chart->addAxis(axisY, Qt::AlignLeft);
          series->attachAxis(axisY);
      
          chart -> setAnimationOptions(QChart::GridAxisAnimations);
          chart -> setAnimationOptions(QChart::SeriesAnimations);
          chart -> setAnimationEasingCurve(QEasingCurve::OutCubic);
      
          QChartView *chartView = new QChartView(chart);
          chartView -> setRenderHint(QPainter::Antialiasing);
          chartView -> setParent(ui->horizontalFrame_chart);
      }```
      
      database:
      ![5d3e9be7-6604-4922-909a-b72cb63fab98-image.png](https://ddgobkiprc33d.cloudfront.net/9a0c6196-5586-4340-b2b5-ed129ff48cd1.png)
      Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      @krist0 said in Line chart with sqlite database:

      the program keeps crashing and wont let me go any further. h

      Then use your debugger and see where exactly it crashes.

      QStringList dates = qry1.value(0).toString().split("-").mid(0,3);
      momentInTime.setDate(QDate(dates[0].toInt(), dates[1].toInt() , dates[2].toInt()));

      You should check if you really get three strings here.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved