Results 1 to 15 of 15

Thread: simple MVC question!

  1. #1
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default simple MVC question!

    I have a simple MVC code where I update a model (QStringList) after the initial display.
    I expected the view to change automatically (as in C#.NET).

    I did the following to update the view! What other method of update have I missed.

    I am reading about MVC but haven't gotten that far yet!
    Qt Code:
    1. #include <QApplication>
    2. #include <QSplitter>
    3. #include <QTreeView>
    4. #include <QListView>
    5. #include <QListWidget>
    6. #include <QDirModel>
    7. #include <QStringList>
    8. #include <QStringListModel>
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. QSplitter *splitter = new QSplitter;
    13. qsl.append(QString("abcd1234"));
    14. QStringListModel *qStringListModel;
    15. qStringListModel = new QStringListModel;
    16. qStringListModel->setStringList(qsl);
    17.  
    18. QListView *qlw = new QListView(splitter);
    19. // add a string
    20. qsl.append(QString("acd134"));
    21. qStringListModel->setStringList(qsl); // this displays the second string
    22. qlw->setModel(qStringListModel);
    23. splitter->show();
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: simple MVC question!

    The code seems correct. You should be seeing two items in your list - abcd1234 and acd134. Isn't it the case?

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

    landonmkelsey (18th August 2008)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: simple MVC question!

    Probably he means that why is he required to do additional tricks after modifying the string list. If you want updates to happen automatically, you should use the model API (insertRow() + setData()) to change the list.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    landonmkelsey (18th August 2008)

  6. #4
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: simple MVC question!

    Thanks! That is it!!!

    I'll read up on those calls and try it!

    It is a large convenience to be able to connect a container to a view and have automatic view updates without an update().

    Repeating myself, in C#.NET one need only update the container!

    I always thought that was neat!

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

    Default Re: simple MVC question!

    Quote Originally Posted by landonmkelsey View Post
    Repeating myself, in C#.NET one need only update the container!
    It's the same here. The model is the container, not the string list. The latter is only an internal representation detail of the model.

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

    landonmkelsey (18th August 2008)

  9. #6
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: simple MVC question!

    Saw your post after I posted this!

    Looking at the API's for QStringListModel (insertRows() setData()),

    the code below seems simpler! I don't need a rowCount or model index!

    I'll work out the example with those APIs and see what I learn!
    The following works! All 3 strings appear!

    All this using command line Qt4.3 under Fedora 9

    Qt Code:
    1. #include <QApplication>
    2. #include <QSplitter>
    3. #include <QTreeView>
    4. #include <QListView>
    5. #include <QListWidget>
    6. #include <QDirModel>
    7. #include <QStringList>
    8. #include <QStringListModel>
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. QSplitter *splitter = new QSplitter;
    13. qsl.append(QString("abcd1234"));
    14. QStringListModel *qStringListModel;
    15. qStringListModel = new QStringListModel;
    16. qStringListModel->setStringList(qsl);
    17.  
    18. QListView *qlw = new QListView(splitter);
    19.  
    20. qsl.append(QString("abc123"));
    21. qStringListModel->setStringList(qsl);
    22. qlw->setModel(qStringListModel);
    23.  
    24. qsl.append(QString("ab12"));
    25. qStringListModel->setStringList(qsl);
    26. qlw->setModel(qStringListModel);
    27.  
    28. splitter->show();
    29. return app.exec();
    30. }
    To copy to clipboard, switch view to plain text mode 

    I tried to do the same thing with QListWidget but ran into a private function compiler error! QListWidget inherits from QListView!

    Out of chaos comes understanding!

    Research continues!
    Last edited by landonmkelsey; 18th August 2008 at 19:34. Reason: synchronization

  10. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple MVC question!

    You must be getting error while setting the model ...
    QListWidget, QTreeWidget and QTableWidget are wrappers around the model/view , and maintain their own internal model/view. They work with QListWidgetItem, QTreeWidgetItem, and QTableWIdgetItem respectively.

  11. #8
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: simple MVC question!

    Question is "insert rows from where?"

    The parent?

    What is the parent here? The QStringList must be the answer!

    The following from the Qt4 docs!
    Qt Code:
    1. bool QStringListModel::insertRows ( int row, int count, const QModelIndex & parent = QModelIndex() ) [virtual]
    To copy to clipboard, switch view to plain text mode 

    Inserts count rows into the model beginning at the given row.

    The parent index of the rows is optional and is only used for consistency with QAbstractItemModel. By default, a null index is specified, indicating that the rows are inserted in the top level of the model.

    Reimplemented from QAbstractItemModel.

    See also QAbstractItemModel::insertRows().

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

    Default Re: simple MVC question!

    Quote Originally Posted by landonmkelsey View Post
    Research continues!
    I suggest you take a look at QStandardItemModel.

    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. class Inserter : public QObject {
    5. public:
    6. Inserter(QStandardItemModel *m) : QObject() {
    7. model = m;
    8. }
    9. protected:
    10. void timerEvent(QTimerEvent *e){
    11. QString str = QDateTime::currentDateTime().toString();
    12. model->appendRow(new QStandardItem(str));
    13. }
    14. private:
    15. };
    16.  
    17. int main(int argc, char **argv){
    18. QApplication app(argc, argv);
    19. model.setColumnCount(1);
    20. lv.setModel(&model);
    21. lv.show();
    22. Inserter ins(&model);
    23. ins.startTimer(1000);
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

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

    landonmkelsey (18th August 2008)

  14. #10
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: simple MVC question!

    Thanks! That is a perfect example!

    I'll bet you have many pertinent examples. If you ever need a great sax example, I wrote the absolute best ever! Straightforward!

    It combines:
    (1) sax
    (2) graphics
    (3) container storage of xml data
    Last edited by landonmkelsey; 18th August 2008 at 22:17. Reason: wordage

  15. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple MVC question!

    Quote Originally Posted by landonmkelsey View Post
    I'll bet you have many pertinent examples. If you ever need a great sax example, I wrote the absolute best ever!
    Maybe you could add it to our wiki?

  16. #12
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: simple MVC question!

    I love the example you gave QStandardItemModel

    but why is it missing from

    http://doc.trolltech.com/4.0/qstanda...l-members.html

    Maybe it is also missing from QStringListModel!
    Last edited by landonmkelsey; 18th August 2008 at 23:48. Reason: found it

  17. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: simple MVC question!

    Quote Originally Posted by landonmkelsey View Post
    Where is the wiki you spoke of? On this site?
    Yes, Qt Centre has a wiki: http://wiki.qtcentre.org

  18. The following user says thank you to jacek for this useful post:

    landonmkelsey (19th August 2008)

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

    Default Re: simple MVC question!

    Quote Originally Posted by landonmkelsey View Post
    I love the example you gave QStandardItemModel

    but why is it missing from

    http://doc.trolltech.com/4.0/qstanda...l-members.html

    Maybe it is also missing from QStringListModel!
    You might want to try something less... ancient...

    http://doc.trolltech.com/latest/qsta...l-members.html

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

    landonmkelsey (19th August 2008)

  21. #15
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: simple MVC question!

    How did I get the ancient version? I surfed correctly on trolltech.com!

    Trolltech->Qt4->docs

    This "less ancient" version will be a blessing!

    Just noticed your link is for 4.4! I have 4.3!

    Thanks for a great link! I will use it!

    Maybe a later Fedora update will deliver 4.4!
    Last edited by landonmkelsey; 19th August 2008 at 13:26. Reason: eureka

Similar Threads

  1. Simple RegExp Question...
    By jared.carlson23 in forum Newbie
    Replies: 1
    Last Post: 4th July 2008, 14:10
  2. a simple noob question
    By wrproject in forum Newbie
    Replies: 1
    Last Post: 1st July 2008, 23:25
  3. QTextEdit simple question
    By Marcopolo in forum Qt Tools
    Replies: 4
    Last Post: 11th October 2007, 00:01
  4. simple question on Class-Members
    By mickey in forum General Programming
    Replies: 7
    Last Post: 4th February 2006, 22:37
  5. QTextEdit Qt4: simple question
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 18th January 2006, 12:03

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.