PDA

View Full Version : Use of QListView



BrainStorm
2nd August 2010, 14:52
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):



class UsersListView: public QListView
{
private:
QStringList userNames;
void refresh()
{
((QStringListModel*)model())->setStringList(userNames);
}

public:
UsersListView(QWidget* parent)
:QListView(parent)
{
setModel(new QStringListModel(userNames));
}
void appendUser(BSUser* user)
{
userNames << user->getNick();
refresh();
}
void removeUser(BSUser* user)
{
userNames.removeAll(user->getNick());
refresh();
}
};


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.

Lykurg
2nd August 2010, 15:27
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.

borisbn
2nd August 2010, 17:34
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

BrainStorm
2nd August 2010, 17:36
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;)

BrainStorm
2nd August 2010, 17:38
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

borisbn
3rd August 2010, 04:39
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 :)