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. Problem with QTableWidget as input for QChart

Problem with QTableWidget as input for QChart

Scheduled Pinned Locked Moved Unsolved General and Desktop
1 Posts 1 Posters 145 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.
  • H Offline
    H Offline
    Huberti
    wrote on 28 Aug 2019, 12:22 last edited by
    #1

    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?

    1 Reply Last reply
    0

    1/1

    28 Aug 2019, 12:22

    • Login

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