V
V
Viktor2020-03-04 14:41:07
Qt
Viktor, 2020-03-04 14:41:07

How can I implement the opening of xlsx table through QTreeWidget/View?

Good afternoon. I have such a problem. The task is to create a program that would open xlsx files and display tables in the form of a tree, through TreeWidget or View. In Qt, I'm still green and stupid, but the task has been set and it needs to be done by hook or by crook. I ask the community for help.
I was able to implement the proofreading of the xlsx file using the Qt xlsx libraries. But, here's how to then pull out the read data, and implement their output in the tree, I've been fighting for almost a month. There is a critical lack of experience, because this is my first program. I suppose that you need to push the received data into QTreeModel and then display it on the screen. But I have no idea how to implement it. I took standard examples, but I could not dock them there. In general, I would be extremely grateful for any advice (except: "read the documentation, everything is clear there", because in my case it is not at all clear), although preferably laid out in a style for tight-fitting people)
Here is an example of code that I use to read xlsx:

Document doc(fileName);
 
    int sheetIndexNumber = 0;
    foreach( QString curretnSheetName, doc.sheetNames() )
    {
        // get current sheet
        AbstractSheet* currentSheet = doc.sheet( curretnSheetName );
        if ( NULL == currentSheet )
            continue;
 
        // get full cells of current sheet
        int maxRow = -1;
        int maxCol = -1;
        currentSheet->workbook()->setActiveSheet( sheetIndexNumber );
        Worksheet* wsheet = (Worksheet*) currentSheet->workbook()->activeSheet();
        if ( NULL == wsheet )
            continue;
 
        QString strSheetName = wsheet->sheetName(); // sheet name
        qDebug() << strSheetName;
 
        QVector<CellLocation> clList = wsheet->getFullCells( &maxRow, &maxCol );
 
        QVector< QVector<QString> > cellValues;
        for (int rc = 0; rc < maxRow; rc++)
        {
            QVector<QString> tempValue;
 
            for (int cc = 0; cc < maxCol; cc++)
            {
                tempValue.push_back(QString(""));
            }
            cellValues.push_back(tempValue);
        }
 
        for ( int ic = 0; ic < clList.size(); ++ic )
        {
            CellLocation cl = clList.at(ic); // cell location
 
            int row = cl.row - 1;
            int col = cl.col - 1;
 
            QSharedPointer<Cell> ptrCell = cl.cell; // cell pointer
 
            // value of cell
            QVariant var = cl.cell.data()->value();
            QString str = var.toString();
 
            cellValues[row][col] = str;
 
        for (int rc = 0; rc < maxRow; rc++)
        {
            for (int cc = 0; cc < maxCol; cc++)
            {
                QString strCell = cellValues[rc][cc];
                qDebug() << "( row : " << rc
                         << ", col : " << cc
                         << ") " << strCell; // display cell value
            }
        }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question