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

Problem with QTableWidget as input for QChart



  • Hi, I would like to display points entered into table on the chart. The problem is that by default when cells are empty they are considered as points (0,0) on the chart. Here is code:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);        
        ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        ui->tableWidget->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    
        QChart* chart = new QChart();
        chart->legend()->hide();
        ui->chartView->setChart(chart);
    
        QLineSeries* series = new QLineSeries();
    
        QVXYModelMapper *mapper = new QVXYModelMapper(this);
        mapper->setXColumn(0);
        mapper->setYColumn(1);
        mapper->setSeries(series);
        mapper->setModel(ui->tableWidget->model());
        chart->addSeries(series);
    
        chart->createDefaultAxes();
    }
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>579</width>
        <height>407</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>Chart</string>
      </property>
      <widget class="QWidget" name="centralWidget">
       <layout class="QVBoxLayout" name="verticalLayout">
        <item>
         <widget class="QChartView" name="chartView"/>
        </item>
        <item>
         <widget class="QTableWidget" name="tableWidget">
          <property name="rowCount">
           <number>5</number>
          </property>
          <property name="columnCount">
           <number>2</number>
          </property>
          <row/>
          <row/>
          <row/>
          <row/>
          <row/>
          <column>
           <property name="text">
            <string>x</string>
           </property>
          </column>
          <column>
           <property name="text">
            <string>y</string>
           </property>
          </column>
         </widget>
        </item>
       </layout>
      </widget>
     </widget>
     <layoutdefault spacing="6" margin="11"/>
     <customwidgets>
      <customwidget>
       <class>QChartView</class>
       <extends>QGraphicsView</extends>
       <header>QtCharts</header>
      </customwidget>
     </customwidgets>
     <resources/>
     <connections/>
    </ui>
    
    

    Images how it looks like:
    Empty table - there is unwanted (0, 0) point on the chart by default
    0_1566994588701_empty_table.PNG
    Table with two points filled - unwanted point (0,0) causes bad results:
    0_1566994647509_table_two_points.PNG

    Is there any way to fix that?


Log in to reply