Results 1 to 6 of 6

Thread: Use of QListView

  1. #1
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Talking Use of QListView

    Hi everyone, first I must say that my english is not very good, i'm still learning...

    Well, I started learning QT a few weeks ago, and I'm now working on a chat project, it's very simple, "first project" style. The server uses a QListView to show online users. My "problem" is very simple, as I'd like to learn things right, and I'm being forced to use these lines of code to add and remove items (i'll paste the full class):

    Qt Code:
    1. class UsersListView: public QListView
    2. {
    3. private:
    4. QStringList userNames;
    5. void refresh()
    6. {
    7. ((QStringListModel*)model())->setStringList(userNames);
    8. }
    9.  
    10. public:
    11. UsersListView(QWidget* parent)
    12. :QListView(parent)
    13. {
    14. setModel(new QStringListModel(userNames));
    15. }
    16. void appendUser(BSUser* user)
    17. {
    18. userNames << user->getNick();
    19. refresh();
    20. }
    21. void removeUser(BSUser* user)
    22. {
    23. userNames.removeAll(user->getNick());
    24. refresh();
    25. }
    26. };
    To copy to clipboard, switch view to plain text mode 

    Is this the right way? i really need to use my "refresh()" function to update items? and this is the right way to add and remove?
    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Use of QListView

    Hi,

    first better use QAbstractItemModel::removeRow() and QAbstractItemModel::insertRow() thus you don't have to reset the whole model every time you change something. Then subclassing the view looks a little bit strange to me but it is ok. E.g. you have a text edit to display messages and a view for displaying your users, I would put both widgets in one and add there your functions like add and remove user. But as told, subclassing is also fine.


    EDIT: Also better store a pointer to your model, then you don't have all the time the function call "model()" and you don't have to cast.
    Last edited by Lykurg; 2nd August 2010 at 15:28. Reason: updated contents

  3. #3
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use of QListView

    Your subclassing of a QTreeView instead of subclassing of a model breaks Qt's model/view architecture. See their examples of it and you'll see the right way

  4. #4
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of QListView

    Wonderful, thanks for the reply, as you may have predicted, it helps me a lot, I'm really interested in QT, but the official reference is a little bit... dry, anyway, thanks again, cya

  5. #5
    Join Date
    Aug 2010
    Posts
    26
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use of QListView

    Quote Originally Posted by Lykurg View Post
    Hi,

    first better use QAbstractItemModel::removeRow() and QAbstractItemModel::insertRow() thus you don't have to reset the whole model every time you change something. Then subclassing the view looks a little bit strange to me but it is ok. E.g. you have a text edit to display messages and a view for displaying your users, I would put both widgets in one and add there your functions like add and remove user. But as told, subclassing is also fine.


    EDIT: Also better store a pointer to your model, then you don't have all the time the function call "model()" and you don't have to cast.
    Wonderful, thanks for the reply, as you may have predicted, it helps me a lot, I'm really interested in QT, but the official reference is a little bit... dry, anyway, thanks again, cya

  6. #6
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Use of QListView

    see this example: doc.qt.nokia.com/4.7-snapshot/itemviews-simpletreemodel.html
    And by the way, QT means Quick Time and Qt means Qt framework

Similar Threads

  1. QListView
    By Yayati.Ekbote in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2010, 11:41
  2. QListView
    By Yayati.Ekbote in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2010, 18:50
  3. QListView
    By moowy in forum Qt Programming
    Replies: 2
    Last Post: 2nd October 2006, 14:14
  4. [QT3] QListView
    By incapacitant in forum Qt Programming
    Replies: 5
    Last Post: 29th January 2006, 14:31
  5. QListView
    By dragon in forum Newbie
    Replies: 1
    Last Post: 25th January 2006, 21:08

Tags for this Thread

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.