Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Labels do not appear in QChartView



  • I'm trying to generate a sales report that shows the amount of sales of the day on a particular day of the month in the chart.

    RelatorioDeVendas::RelatorioDeVendas(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::RelatorioDeVendas)
    {
        ui->setupUi(this);
    
        QChart *chartMensal = new QChart();
        QLineSeries *seriesMensal = new QLineSeries();
    
        Relatorios::vendasMensais(seriesMensal, chartMensal);
    
        chartMensal->addSeries(seriesMensal);
        chartMensal->setTitle("Vendas do mês");
        chartMensal->legend()->setVisible(false);
    
        ui->chartViewMensal->setRenderHint(QPainter::Antialiasing);
        ui->chartViewMensal->setChart(chartMensal);
    
    }
    
    void Relatorios::vendasMensais(QLineSeries *series, QChart *chart)
    {
    
       QSqlDatabase db = Database::database();
    
       QSqlQuery q(db);
       q.prepare("SELECT COUNT(id), feita_em FROM vendas WHERE YEAR(feita_em) = :ano AND MONTH(feita_em) = :mes GROUP BY DAY(feita_em)");
       q.bindValue(":ano", QDate::currentDate().year());
       q.bindValue(":mes", QDate::currentDate().month());
    
       if(!q.exec()){
           qCritical() << "Falha ao realizar a consulta: " << q.lastError().text();
           return;
       }
    
       QCategoryAxis *xAxis = new QCategoryAxis();
    
       while(q.next()){
    
           QDateTime data = QDateTime::fromString(q.value(1).toString(), "yyyy-MM-ddThh:mm:ss.z");
    
           xAxis->append(QString::number(data.date().day()), data.date().day());
           series->append(data.date().day(), q.value(0).toInt());
    
       }
    
       chart->createDefaultAxes();
       chart->addAxis(xAxis, Qt::AlignBottom);
    
    }
    

    But the days do not appear in the graph even using QCategoryAxis

    ![no labels in chart](0_1560887008630_8b854fc0-c71c-42e1-8fcf-5cfbc976be17-image.png image url)


Log in to reply