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. [SOLVED] Program crashes when loading data from a .bin file to QTableView when a button is pressed

[SOLVED] Program crashes when loading data from a .bin file to QTableView when a button is pressed

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 4.0k Views 1 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    You set the row and column count but that doesn't create the item in there.

    Have a look at the "QTableWidget's documentation":http://qt-project.org/doc/qt-4.8/qtablewidget.html#details

    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
    • F Offline
      F Offline
      fs_tigre
      wrote on last edited by
      #3

      First of all thank you for your reply. I'm reading the documentation but it doesn't click.

      bq. Items are created ouside the table (with no parent widget) and inserted into the table with setItem():

      Can you give me another clue?

      FYI,
      I'm using a QTableView not a QTableWidget

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #4

        Sorry I misread, but I think the answer is still relevant, you modify the row & column count of the model and try to access the items without creating them first.

        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
        • F Offline
          F Offline
          fs_tigre
          wrote on last edited by
          #5

          bq. you modify the row & column count of the model and try to access the items without creating them

          Sorry but I don't get this, the items were created here by streaming data from the file, no?
          @stream >> n >> m;@

          Sorry if I don't see the obvious but this is the first time I use MV.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #6

            When you write the item to the file you are writing the data and the flags of the item.

            When you want to read, you have to provide an item to read these data and flags.

            @model->item(i,j)@

            points to a null item

            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
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #7

              [quote author="fs_tigre" date="1375973884"]bq. you modify the row & column count of the model and try to access the items without creating them

              Sorry but I don't get this, the items were created here by streaming data from the file, no?
              @stream >> n >> m;@

              Sorry if I don't see the obvious but this is the first time I use MV.[/quote]

              What SGaist is referring to is that you are assuming in your load function that the model is already completely initialized/populated.

              Let's assume you have a model of size 10 rows and 20 cols. If you write those and load them again without destroying model, it will work. If you load a smaller model, let's say 8 rows and 12 cols, it will work.

              If you load a larger model (or when have not a model initialized already) you will fail. Let's assume 12 rows and 20 cols, when you had previously a 10 by 20 model. Where should row 11 and 12 come from?

              You use item(i,j) but you did not assign memory in the section you are showing us. Certainly you may do this somewhere else, but is creating in general a problem.

              [edit, was a bit slow, but basically the same answer as SGaist, but giving you also the hint, why it does work sometimes]

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • F Offline
                F Offline
                fs_tigre
                wrote on last edited by
                #8

                Ok, make sense. I basically added a for loop to create empty rows and it works fine, but for some reason I have a filling that this can be optimized.

                Is this how you would do it?

                Load Funtion:

                @QFile file("/Users/UserName/Practicing/Resource_Files/someFile.bin");
                if (file.open(QIODevice::ReadOnly))
                {
                   QDataStream stream(&file);
                    qint32 n, m;
                    stream >> n >> m;
                
                    // new for loop starts here
                    for (int e=0; e<n; ++e&#41;
                        {
                           for (int e=0; e<m; e++)
                           {
                               QStandardItem *item1 = new QStandardItem("");
                               QStandardItem *item2 = new QStandardItem("");
                
                               QList<QStandardItem*> row;
                               row <<item1 << item2;
                               model->appendRow(row);
                           }
                       }
                     // new for loop ends here
                
                    model->setRowCount(n);
                    model->setColumnCount(m);
                
                 for (int i=0; i<n; ++i)
                     {
                        for (int j=0; j<m; j++)
                        {
                            model->item(i,j)->read(stream);
                        }
                    }
                
                 file.close();
                }@
                
                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  Something like that ?
                  @
                  QDataStream stream(&file);
                  qint32 row, col;
                  stream >> row >> col;
                  model->setRowCount(row);
                  model->setColumnCount(col);

                      // new for loop starts here
                      for (int i = 0; i < row ; ++i) {
                             for (int j = 0; j < col; j++) {
                                 QStandardItem *item = new QStandardItem;
                                 item->read(stream);
                                 model->setItem(i, j, item);
                             }
                         }
                  

                  @

                  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
                  • F Offline
                    F Offline
                    fs_tigre
                    wrote on last edited by
                    #10

                    I knew that the way I did it wasn't the best way. Thanks a lot for your help!

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #11

                      You're welcome !

                      If everything if fine now, can you update the thread's title to solved ? So other forum users may know a solution has been found :)

                      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

                      • Login

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