Results 1 to 2 of 2

Thread: Change the Model via the View

  1. #1
    Join Date
    Sep 2006
    Dresden, Germany
    Qt products

    Default Change the Model via the View

    Hello all together,

    currently I'm just learning programming with Qt. Today I found a problem that I cannot solve by myself

    I have an (empty) QStandardItemModel that is shown with a QTableView. After the user did something, I want to fill the model with some data, that means I want to change it (e.g. add some more rows or set new data). Because I don't have the pointer to the model itself anymore, I want to get the pointer out of the TableView:
    QStandardItemModel* model=myTable->model();
    Now I only get a pointer with type QAbstractItemModel, which is an abstract class and misses some members of QStandardItemView. Casting it to QStandardItemView* seems to work: QStandardItemModel* model=(QStandardItemView*)myTable->model();
    But now my program crashes with a segmentation fault on every call like
    QModelIndex index=model->index(0, 0, QModelIndex());
    model->insertRows(0, Count(), QModelIndex());

    What am I doing wrong? Is the crash related to the cast or to the invalid QModelIndex?

    Thank you for help,

  2. #2
    Join Date
    Jan 2006
    Warsaw, Poland
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits

    Default Re: Change the Model via the View

    Qt Code:
    1. QModelIndex index=model->index(0, 0, QModelIndex());
    To copy to clipboard, switch view to plain text mode 
    This shouldn't crash, no matter what, provided that model!=0 (and it shouldn't be, if you set the model for the view using setModel).

    BTW. You don't need to have access to any of QStandardItemModel methods, you can operate directly on QAbstractItemModel.

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Model - View Programming doubt.
    By munna in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2006, 13:01
  3. Replies: 6
    Last Post: 20th April 2006, 10:23
  4. Model, view, resize to show all data
    By jcr in forum Qt Programming
    Replies: 2
    Last Post: 17th April 2006, 20:59
  5. Table model / view editing issue
    By Caius Aérobus in forum Qt Programming
    Replies: 9
    Last Post: 7th April 2006, 11:03


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.