Results 1 to 3 of 3

Thread: Adding an item to a QListView

  1. #1
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Question Adding an item to a QListView

    Hello,

    In Qt examples, there are examples that usually go beyond my simple test program. I have a QListView in main windo. I don't use designer or Qt quick.

    Why do I loose the itemName value? My program crashes after I add a new item to my model and thanks to debugging I see that I indeed have 3 items in my vector but there is a blank string rather than expected value for last item (the one I created by clicking on the button).

    Thank you for pointing my learning coding errors.

    Here is some shortened source code, I attach the whole source code to this post.

    MyModel.h:
    Qt Code:
    1. class MyModel : public QAbstractTableModel
    2. {
    3. Q_OBJECT
    4. public:
    5. MyModel(QObject *parent = 0);
    6. int rowCount(const QModelIndex &parent = QModelIndex()) const ;
    7. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    8. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    9.  
    10. void addItem(TestItem item);
    11. private:
    12. std::vector<TestItem*> m_items;
    13. public slots:
    14. void onButtonClicked();
    15. };
    To copy to clipboard, switch view to plain text mode 

    TestItem.h:
    Qt Code:
    1. class TestItem {
    2.  
    3. public:
    4. TestItem();
    5. TestItem(QString itemName);
    6. QString getName() const;
    7. void setName(QString itemName);
    8.  
    9.  
    10. private :
    11. QString m_itemName;
    12. };
    To copy to clipboard, switch view to plain text mode 

    MyModel.cpp:
    Qt Code:
    1. QVariant MyModel::data(const QModelIndex &index, int role) const
    2. {
    3. int row = index.row();
    4. int col = index.column();
    5. // generate a log message when this method gets called
    6. qDebug() << QString("row %1, col%2, role %3")
    7. .arg(row).arg(col).arg(role);
    8.  
    9.  
    10. if(role == Qt::DisplayRole){
    11. if (col == 0) return (*m_items[row]).getName();
    12.  
    13. return QString("Row%1, Column%2")
    14. .arg(row + 1)
    15. .arg(col +1);
    16. }
    17.  
    18. return QVariant();
    19. }
    20.  
    21. void MyModel::addItem(TestItem item)
    22. {
    23. int nbRows = m_items.size();
    24. beginInsertRows(QModelIndex(), nbRows, nbRows);
    25. m_items.push_back(&item);
    26. endInsertRows();
    27. }
    28.  
    29. void MyModel::onButtonClicked(){
    30. TestItem newItem("new element");
    31. addItem(newItem);
    32. }
    To copy to clipboard, switch view to plain text mode 

    QtListViewDemoQt4.zip

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding an item to a QListView

    You are pushing a pointer to a local object which gets destroyed when you leave addItem() thus your list items point to invalid data. With this particular structure I'd advise to use QList<TestItem> rather than QList<TestItem*>.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    postb99 (19th February 2014)

  4. #3
    Join Date
    Feb 2014
    Posts
    13
    Thanks
    6
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: Adding an item to a QListView

    This solved, thank you for this quick answer :-)

Similar Threads

  1. QListView: how to adding column text?
    By richardander in forum Qt Programming
    Replies: 2
    Last Post: 31st December 2013, 10:40
  2. Adding QStringList to QListView
    By Tomasz in forum Newbie
    Replies: 5
    Last Post: 30th September 2010, 20:57
  3. QListView - adding items
    By creep33 in forum Qt Programming
    Replies: 3
    Last Post: 9th September 2010, 17:30
  4. Adding QPixmaps to QListView
    By ada10 in forum Newbie
    Replies: 3
    Last Post: 16th August 2010, 12:15
  5. Adding elements to a QListView
    By Arreis in forum Newbie
    Replies: 4
    Last Post: 30th June 2009, 06:56

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.