Qt World Summit: Register Today!

Copy model to a QTableView

  • 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

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


    QAbstractItemModel *model = &saved;_model;

    I have a segmentation fault at the last line

  • 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.

  • 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{
    int a;

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

    class C : public B{
    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!

Log in to reply