Results 1 to 5 of 5

Thread: QStringListModel doesn't update when QStringList is updated

  1. #1
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QStringListModel doesn't update when QStringList is updated

    Hi,
    I would like help about these points
    1/ I want to use the append in my method so i can add "Belgique" for ex. to my list but when i do like this nothing happens when i push the button Add....this my code and window new.jpg
    Qt Code:
    1. #include"scheduletable.h"
    2.  
    3. #include "ui_scheduletable.h"
    4. #include<QStringListModel>
    5. ScheduleTable::ScheduleTable(QWidget*parent):
    6. QWidget(parent),
    7. ui(newUi::ScheduleTable)
    8. {
    9. ui->setupUi(this);
    10. QStringListlistePays;
    11. listePays<<"France";
    12. QStringListModel*modele=newQStringListModel(listePays);
    13. ui->listView->setModel(modele);
    14. connect(ui->addFrame,SIGNAL(clicked()),this,SLOT(add_Frame()));
    15. }
    16. voidScheduleTable::add_Frame()
    17. {
    18. listePays.append("Belgique");
    19.  
    20. }
    21. ScheduleTable::~ScheduleTable()
    22. {
    23. deleteui;
    24. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt 5.0.1

    you can use model->setData() method

    Or you can use model->setStringList(listePays) after listePays.append() method

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

    nymar (7th March 2013)

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

    Default Re: Qt 5.0.1

    QStringListModel stores a copy of the string list you pass it. Thus if you add a new entry to the original string list, the model does not get updated. Since QStringListModel is read-only, the only solution is to set a new string list on it again but then you'll lose things such as selection and scrolling in the view because the model will be reset. A more proper solution would be to use QStandardItemModel and add a new entry directly to the model with QStandardItemModel::appendRow().
    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.


  5. #4
    Join Date
    Feb 2013
    Posts
    5
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QStringListModel doesn't update when QStringList is updated

    Thx for helping guyz the solution is
    Qt Code:
    1. void ScheduleTable::add_Frame()
    2. {
    3. listePays.append("Belgique");
    4. modele->insertRows(0, 1);
    5. modele->setData(modele->index(0), "Belgique");
    6. }
    To copy to clipboard, switch view to plain text mode 


    Added after 59 minutes:


    but i have a question how can i put the new item at the end of the list ? i couldn't use currentIndex()
    Last edited by nymar; 7th March 2013 at 15:20.

  6. #5
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QStringListModel doesn't update when QStringList is updated

    Plz see wysota post. For understanding concept of model/view in Qt see this: http://qt-project.org/doc/qt-4.8/mod...ogramming.html -> this is very helpful and complete

Similar Threads

  1. GUI doesn't update in QThreaded opengl application
    By bingofuel in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2012, 08:04
  2. QDataWidgetMapper doesn't always update field in table
    By marcvanriet in forum Qt Programming
    Replies: 4
    Last Post: 14th December 2011, 11:57
  3. QGraphicsScene's sceneRect() doesn't update
    By blooglet in forum Qt Programming
    Replies: 2
    Last Post: 17th February 2011, 09:11
  4. QGLWidget doesn't update
    By qtbnl in forum Qt Programming
    Replies: 12
    Last Post: 14th February 2011, 17:41
  5. QComboBox doesn't update when on hidden widget
    By martinb0820 in forum Qt Programming
    Replies: 1
    Last Post: 10th December 2010, 22:09

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.