QTableWidget, Save works, Load crashes



  • hey guys, im just trying to learn qt but i got some problems.
    i have a qttablewidget with some numbers in it, i can save it but if im trying to load my programm crashes.
    i searched for hours but havent found a solution yet, heres my code
    Save:

    void MainWindow::saveMatrix()
    {
        QString fileName = QFileDialog::getSaveFileName(this,
                     tr("Save Matrix"), "",
                     tr("Matrix (*.gja);;All Data (*)"));
             if (fileName.isEmpty())
                      return;
                  else {
                      QFile file(fileName);
                      if (!file.open(QIODevice::WriteOnly)) {
                          QMessageBox::information(this, tr("Unable to open file"),
                              file.errorString());
                          return;
                      }
               QDataStream out(&file);
               out.setVersion(QDataStream::Qt_5_4);
               int rowCount, columnCount;
               rowCount = table->rowCount();
               columnCount = table->columnCount();
               out << QString::number(rowCount);
               out << QString::number(columnCount);
               for(int row=0;row<rowCount;row++)
               {
                    for(int col=0;col<columnCount;col++)
                    {
                        QString stream;
                        stream = table->item(row,col)->text();
                        out << stream;
                    }
               }
             }
    }
    

    and my
    load code:

    void MainWindow::loadMatrix()
    {
        QString fileName = QFileDialog::getOpenFileName(this,
                    tr("Open Matrix"), "",
                    tr("Matrix (*.gja);;All Files (*)"));
            if (fileName.isEmpty())
                     return;
                 else {
    
                     QFile file(fileName);
    
                     if (!file.open(QIODevice::ReadOnly)) {
                         QMessageBox::information(this, tr("Unable to open file"),
                             file.errorString());
                         return;
                     }
    
                     QDataStream in(&file);
                     in.setVersion(QDataStream::Qt_5_4);
                     QString buf;
                     int rowCount, columnCount;
                     in >> buf;
                     rowCount = buf.toInt();
                     in >> buf;
                     columnCount = buf.toInt();
                     table->setRowCount(rowCount);
                     table->setColumnCount(columnCount);
                     for(int x=0;x<rowCount;x++)
                     {
                         for(int y=0;y<columnCount;y++)
                         {
                             in >> buf;
                             QTableWidgetItem *item = new QTableWidgetItem;
                             item->setText(buf);
                             table->setItem(x,y, item);
                         }
    
                     }
            }
    }
    

    what can i do? im just saving the size of my table and its content, then i restore it..
    EDIT:now it works :D can be closed..strange
    [edit: Added missing coding tags ``` SGaist]


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What did happen in between ?


Log in to reply
 

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