Display data from a text file into QAbstractTableModel

50 Views Asked by At

I have data that is stored in the a text file and I want to load the lines of the text file into a table model. The class that is derived from QAbstractTableModel uses the following functions:

    QVariant DataTableModel::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if(role != Qt::DisplayRole)
            return QVariant();
    
        if (orientation == Qt::Horizontal) {
            switch (section) {
            case DataTable::Name:
                return QString("Title1");
                break;
            case DataTable::Type:
                return QString("Title2");
                break;
            case DataTable::Did:
                return QString("Title3");
                break;
            }
        }
        return QVariant();
    }
    
    QVariant DataTableModel::data(const QModelIndex& index, int role) const
    {
        //validate if the table index is legal
        if (!index.isValid() || index.row() < 0 || index.row() > m_tableItems.count() || index.column() < 0)
            return QVariant();
    
        const DataItems& m_items = m_tableItems.at(index.row());
    
        //define data into the table
        if (role == Qt::DisplayRole) {
            switch (index.column()) {
            case DataTable::Name:
                return m_items.m_dataName;
                break;
            case DataTable::Type:
                return m_items.m_type;
                break;
            case DataTable::Did:
                return m_items.m_did;
                break;
            }
        }
        return QVariant();
    }

int DataTableModel::columnCount(const QModelIndex& index) const
{
    return index.isValid() ? 0 : 3;
}


int DataTableModel::rowCount(const QModelIndex& index) const
{
    return index.isValid() ? 0 : m_tableItems.count();
}

I created a own type DataItems. My idea is to store the elements of the string a this items.

void DataTableModel::addDataToTable(QString &name, int& type, QString& did)
{
    DataItems items;
    items.m_dataName = name;
    items.m_type = type;
    items.m_did = did;
    beginInsertRows(QModelIndex(), m_tableItems.count(), m_tableItems.count());
    m_tableItems.push_back(items);
    endInsertRows();
}

In another class I create a function to read the data from the stream. My idea is to store the fields of the splitted string into DataItems. But some columns should show a group of words, so my question is, what the best way is to set data from a text file into a table model.

bool FileManager::readDataFromFile()
{
    QString path = "C:/data.txt";
    QFile file(path);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) 
    {
        qDebug() << "file not found.";
        return false;
    }
    else {
        qDebug() << "file found.";
        QTextStream read(&file);
        while (!read.atEnd()) {
            QString line = read.readAll();
            QStringList fields = line.split(QRegularExpression("\\W+"), Qt::SkipEmptyParts);
            qDebug() << fields;
        }
        return true;
    }
}

The lines in the file I read from has the following format:

Title1: This is a sentence Title2: number Title3 word
0

There are 0 best solutions below