0

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
taathy
  • 155
  • 1
  • 1
  • 9
  • You just need to split your lines into three parts by using more sophisticated regular expression. – vahancho Oct 05 '22 at 14:45
  • Please remove the irrelevant information (see also [mcve]). You talk a lot about your `QAbstractTableModel`, but in the end, your question seems to be about parsing a text file and splitting each line into 3 parts. – m7913d Oct 05 '22 at 20:30
  • It seems to me that you want to split each line based on the position of "Title" or ("Title1", "Title2" and "Title3"). This should be possible using regex or manually looking up its positions (see [`QString::indexOf`](https://doc.qt.io/qt-6/qstring.html#indexOf-2)) and taking the correct substring (see [`QString::mid`](https://doc.qt.io/qt-6/qstring.html#mid)) – m7913d Oct 05 '22 at 20:32

0 Answers0