Qt Plotbeispiel



  • Hallo zusammen,

    ich möchte gerne eine Anwendung mit Plots programmieren und habe mir dafür das modeldata Beispiel von Qt angeschaut und leicht verändert. Meine Anwendung soll in einem Event (in meinem Beispiel durch einen Knopf dargestellt) eine Liste mit Daten plotten.
    Der abgeänderte Code sieht nun so aus (im großen und ganzen der Beispielcode):

    tablewidget.cpp:

    #include "tablewidget.h"
    
    TableWidget::TableWidget(QWidget *parent)
        : QWidget(parent)
    {
        // create simple model for storing data
        // user's table data model
        //! [1]
        model = new CustomTableModel;
        //! [1]
    
        //! [2]
        // create table view and add model to it
        tableView = new QTableView;
        tableView->setModel(model);
        tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);
        //! [2]
    
        //! [3]
        QChart *chart = new QChart;
        chart->setAnimationOptions(QChart::AllAnimations);
        //! [3]
    
        // series 1
        //! [4]
        QLineSeries *series = new QLineSeries;
        series->setName("Line 1");
        QVXYModelMapper *mapper = new QVXYModelMapper(this);
        mapper->setXColumn(0);
        mapper->setYColumn(1);
        mapper->setSeries(series);
        mapper->setModel(model);
        chart->addSeries(series);
        //! [4]
    
        //! [5]
        // for storing color hex from the series
        QString seriesColorHex = "#000000";
    
        // get the color of the series and use it for showing the mapped area
        seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
        model->addMapping(seriesColorHex, QRect(0, 0, 2, model->rowCount()));
        //! [5]
    
    
        // series 2
        //! [6]
        series = new QLineSeries;
        series->setName("Line 2");
    
        mapper = new QVXYModelMapper(this);
        mapper->setXColumn(2);
        mapper->setYColumn(3);
        mapper->setSeries(series);
        mapper->setModel(model);
        chart->addSeries(series);
        //! [6]
    
        //! [7]
        // get the color of the series and use it for showing the mapped area
        seriesColorHex = "#" + QString::number(series->pen().color().rgb(), 16).right(6).toUpper();
        model->addMapping(seriesColorHex, QRect(2, 0, 2, model->rowCount()));
        //! [7]
    
        //! [8]
        chart->createDefaultAxes();
        chartView = new QChartView(chart);
        chartView->setRenderHint(QPainter::Antialiasing);
        chartView->setMinimumSize(640, 480);
        //! [8]
    
        //! [9]
        // create main layout
        QPushButton* button = new QPushButton;
        connect(button, &QPushButton::clicked, this, &TableWidget::clicked);
        QGridLayout *mainLayout = new QGridLayout;
        mainLayout->addWidget(tableView, 1, 0);
        mainLayout->addWidget(chartView, 1, 1);
        mainLayout->addWidget(button, 1, 2);
        mainLayout->setColumnStretch(1, 1);
        mainLayout->setColumnStretch(0, 0);
        setLayout(mainLayout);
        //! [9]
        //!
    }
    
    void TableWidget::clicked(void)
    {
        model->insertRows(0, 15, QModelIndex());
    
         QList<QVector<qreal>*> list;
        for (int i = 0; i < model->rowCount(); i++)
        {
            QVector<qreal>* dataVec = new QVector<qreal>(model->columnCount());
            for (int k = 0; k < model->columnCount(); k++)
            {
                if (k % 2 == 0)
                    dataVec->replace(k, i * 50 + qrand() % 20);
                else
                    dataVec->replace(k, qrand() % 100);
            }
    
            list.append(dataVec);
        }
    
        QModelIndex index = model->index(0,0);
        model->setData(index, list);
    
    }
    

    customtablemodel.cpp:

    
    #include "customtablemodel.h"
    #include <QtCore/QVector>
    #include <QtCore/QTime>
    #include <QtCore/QRect>
    #include <QtGui/QColor>
    #include <QDebug>
    
    CustomTableModel::CustomTableModel(QObject *parent) :
        QAbstractTableModel(parent)
    {
        /*
        qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
    
        // m_data
        for (int i = 0; i < m_rowCount; i++) {
            QVector<qreal>* dataVec = new QVector<qreal>(m_columnCount);
            for (int k = 0; k < dataVec->size(); k++) {
                if (k % 2 == 0)
                    dataVec->replace(k, i * 50 + qrand() % 20);
                else
                    dataVec->replace(k, qrand() % 100);
            }
            m_data.append(dataVec);
        }*/
    }
    
    int CustomTableModel::rowCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        return m_data.count();
    }
    
    int CustomTableModel::columnCount(const QModelIndex &parent) const
    {
        Q_UNUSED(parent)
        return m_columnCount;
    }
    
    bool CustomTableModel::insertRows( int position, int rows, const QModelIndex& index )
    {
        Q_UNUSED( index );
    
        // Add a new row to the table
        // Note: First add rows to the table and add the items after, to speed
        // up the process when adding a big bunch of rows (e. g. 5000 rows)
        beginInsertRows( QModelIndex(), position, position + rows - 1 );
        endInsertRows();
    
        for( int row = 0; row < rows; row++ )
        {
           m_data.append(QVector<qreal>(m_columnCount) );
        }
    
        return true;
    }
    
    
    QVariant CustomTableModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if (role != Qt::DisplayRole)
            return QVariant();
    
        if (orientation == Qt::Horizontal) {
            if (section % 2 == 0)
                return "x";
            else
                return "y";
        } else {
            return QString("%1").arg(section + 1);
        }
    }
    
    QVariant CustomTableModel::data(const QModelIndex &index, int role) const
    {
        if (role == Qt::DisplayRole) {
            return m_data[index.row()].at(index.column());
        } else if (role == Qt::EditRole) {
            return m_data[index.row()].at(index.column());
        } else if (role == Qt::BackgroundRole) {
            foreach (QRect rect, m_mapping) {
                if (rect.contains(index.column(), index.row()))
                    return QColor(m_mapping.key(rect));
            }
            // cell not mapped return white color
            return QColor(Qt::white);
        }
        return QVariant();
    }
    
    bool CustomTableModel::setData(const QModelIndex &index, const QList<QVector<qreal>*> &value, int role)
    {
        if (index.isValid() && role == Qt::EditRole)
        {
            for(int i = 0; i < value.size(); i++)
            {
                for(int j = 0; j < m_columnCount; j++)
                {
                   m_data[i].replace(j, value.at(i)->at(j));
                }
            }
            emit dataChanged(index, this->index(m_rowCount, m_columnCount));
            return true;
        }
        return false;
    }
    
    Qt::ItemFlags CustomTableModel::flags(const QModelIndex &index) const
    {
        return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    }
    
    void CustomTableModel::addMapping(QString color, QRect area)
    {
        m_mapping.insertMulti(color, area);
    }
    
    

    Wenn ich nun den Knopf drücke, wird die Liste mit den Daten in dem TableView angezeigt, aber es werden keine Plots gezeichnet.
    Warum nicht und wie kann ich das beheben?

    Vielen Dank für die Hilfe.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.