Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget, Save works, Load crashes
QtWS25 Last Chance

QTableWidget, Save works, Load crashes

Scheduled Pinned Locked Moved General and Desktop
save load
2 Posts 2 Posters 1.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    haxxe
    wrote on 14 Jun 2015, 09:58 last edited by SGaist
    #1

    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]

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 14 Jun 2015, 20:54 last edited by
      #2

      Hi and welcome to devnet,

      What did happen in between ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0

      1/2

      14 Jun 2015, 09:58

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved