Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Copy model to a QTableView

Copy model to a QTableView

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 3 Posters 5.9k 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.
  • P Offline
    P Offline
    Paco
    wrote on last edited by
    #1

    Hi,

    I have a problem when I want to save a model come from a QTableView.

    I need to save it because I change the table view model and I want to restore it after with the saved model.
    I use a QStandardItemModel to make a copy of the QAbstractItemModel that is return by model() method.

    I take the current QAbstractItemModel and I change it, when I want to restore the old one I apply setModel() with my QStandardItemModel and I have a segmentation fault. I know that QStrandardItemModel inherits by QAbstractItemModel.

    I can't use QAbstractItemModel for my copy because it's abstract.

    Someone can tell me how can I copy a model or how can I restore my old model?

    Thank's for all,
    Best regards

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Paco
      wrote on last edited by
      #2

      @
      QStandardItemModel saved_model(this->ui->tableView->model());

      ...

      QAbstractItemModel *model = &saved;_model;
      this->ui->tableView.setModel(model);
      @

      I have a segmentation fault at the last line

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

        At #1 you create a QStandardItemModel with tableView->model() as parent, not a copy of tableView->model().
        As it is created on the stack it might have been gone out of scope at #6 or will do so as soon as the current block is left.

        There is no convenient way of cloning a model I know of - you will have to provide that functionality by yourself.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          Hi,

          by default, QObjects are not copiable, so also not models.
          This was also discussed in this forum some times.

          Why should any model (you have no idea of the implementation in this code snippet) be copiable to a QStandardItemModel? You try to copy some interface (QAbstractItemModel) to a specific sub class.

          It woudl be the same as:

          @
          class A{
          public:
          int a;
          };

          class B : public A{
          public:
          int b;
          int c:
          }

          class C : public B{
          public:
          int d;
          int e:
          }

          foo(A* p)
          {
          C myData=*p; // this would be your line #1
          }
          @

          This can't work as you don't know if p is a C!

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          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